• R/O
  • SSH
  • HTTPS

util: Commit


Commit MetaInfo

Revision220 (tree)
Zeit2020-07-12 09:36:44
Autorhirukawa_ryo

Log Message

* jersey-util-ext-freemarker 0.1.3
getConfigurationをpublic staticにしてConfigurationを外部利用できるようにしました。

Ändern Zusammenfassung

Diff

--- jersey-util/trunk/jersey-util-ext-freemarker/src/main/java/net/osdn/util/jersey/ext/freemarker/FreeMarkerMessageBodyWriter.java (revision 219)
+++ jersey-util/trunk/jersey-util-ext-freemarker/src/main/java/net/osdn/util/jersey/ext/freemarker/FreeMarkerMessageBodyWriter.java (revision 220)
@@ -17,6 +17,7 @@
1717 import java.util.HashMap;
1818 import java.util.List;
1919 import java.util.Map;
20+import java.util.function.UnaryOperator;
2021
2122 import javax.servlet.ServletContext;
2223 import javax.ws.rs.Produces;
@@ -27,6 +28,7 @@
2728 import javax.ws.rs.ext.MessageBodyWriter;
2829 import javax.ws.rs.ext.Provider;
2930
31+import freemarker.ext.servlet.FreemarkerServlet;
3032 import freemarker.template.Configuration;
3133 import freemarker.template.DefaultObjectWrapperBuilder;
3234 import freemarker.template.ObjectWrapper;
@@ -37,10 +39,19 @@
3739 public class FreeMarkerMessageBodyWriter implements MessageBodyWriter<Object> {
3840
3941 private static final String TEMPLATES_PATH = "/WEB-INF/templates";
40-
42+
4143 private static Map<ServletContext, Configuration> configurations = new HashMap<ServletContext, Configuration>();
42-
43-
44+
45+ private static UnaryOperator<Configuration> configurationUnaryOperator;
46+
47+ public static void setConfigurationUnaryOperator(UnaryOperator<Configuration> operator) {
48+ configurationUnaryOperator = operator;
49+ }
50+
51+ public static UnaryOperator<Configuration> getConfigurationUnaryOperator() {
52+ return configurationUnaryOperator;
53+ }
54+
4455 @Context
4556 protected ServletContext context;
4657
@@ -57,7 +68,7 @@
5768 @Override
5869 public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws WebApplicationException {
5970 try {
60- Configuration config = getConfiguration();
71+ Configuration config = getConfiguration(context);
6172 String templateName = getTemplateName(annotations);
6273 freemarker.template.Template template = config.getTemplate(templateName);
6374
@@ -83,13 +94,13 @@
8394 }
8495 }
8596
86- protected Configuration getConfiguration() throws IOException {
97+ public static Configuration getConfiguration(ServletContext context) throws IOException {
8798 Configuration config = configurations.get(context);
8899 if(config == null) {
89100 synchronized(configurations) {
90101 config = configurations.get(context);
91102 if(config == null) {
92- config = createConfiguration();
103+ config = createConfiguration(context);
93104 configurations.put(context, config);
94105 }
95106 }
@@ -96,16 +107,24 @@
96107 }
97108 return config;
98109 }
99-
100- protected Configuration createConfiguration() throws IOException {
101- DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_29);
110+
111+ private static Configuration createConfiguration(ServletContext context) throws IOException {
112+ DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_30);
102113 builder.setExposeFields(true);
103114 ObjectWrapper objectWrapper = builder.build();
104115
105- Configuration config = new Configuration(Configuration.VERSION_2_3_29);
116+ Configuration config = new Configuration(Configuration.VERSION_2_3_30);
106117 config.setDirectoryForTemplateLoading(new File(context.getRealPath(TEMPLATES_PATH)));
107118 config.setDefaultEncoding("UTF-8");
108119 config.setObjectWrapper(objectWrapper);
120+
121+ if(configurationUnaryOperator != null) {
122+ Configuration c = configurationUnaryOperator.apply(config);
123+ if(c != null) {
124+ config = c;
125+ }
126+ }
127+
109128 return config;
110129 }
111130
Show on old repository browser