Revision | 220 (tree) |
---|---|
Zeit | 2020-07-12 09:36:44 |
Autor | hirukawa_ryo |
* jersey-util-ext-freemarker 0.1.3
getConfigurationをpublic staticにしてConfigurationを外部利用できるようにしました。
@@ -17,6 +17,7 @@ | ||
17 | 17 | import java.util.HashMap; |
18 | 18 | import java.util.List; |
19 | 19 | import java.util.Map; |
20 | +import java.util.function.UnaryOperator; | |
20 | 21 | |
21 | 22 | import javax.servlet.ServletContext; |
22 | 23 | import javax.ws.rs.Produces; |
@@ -27,6 +28,7 @@ | ||
27 | 28 | import javax.ws.rs.ext.MessageBodyWriter; |
28 | 29 | import javax.ws.rs.ext.Provider; |
29 | 30 | |
31 | +import freemarker.ext.servlet.FreemarkerServlet; | |
30 | 32 | import freemarker.template.Configuration; |
31 | 33 | import freemarker.template.DefaultObjectWrapperBuilder; |
32 | 34 | import freemarker.template.ObjectWrapper; |
@@ -37,10 +39,19 @@ | ||
37 | 39 | public class FreeMarkerMessageBodyWriter implements MessageBodyWriter<Object> { |
38 | 40 | |
39 | 41 | private static final String TEMPLATES_PATH = "/WEB-INF/templates"; |
40 | - | |
42 | + | |
41 | 43 | 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 | + | |
44 | 55 | @Context |
45 | 56 | protected ServletContext context; |
46 | 57 |
@@ -57,7 +68,7 @@ | ||
57 | 68 | @Override |
58 | 69 | public void writeTo(Object obj, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws WebApplicationException { |
59 | 70 | try { |
60 | - Configuration config = getConfiguration(); | |
71 | + Configuration config = getConfiguration(context); | |
61 | 72 | String templateName = getTemplateName(annotations); |
62 | 73 | freemarker.template.Template template = config.getTemplate(templateName); |
63 | 74 |
@@ -83,13 +94,13 @@ | ||
83 | 94 | } |
84 | 95 | } |
85 | 96 | |
86 | - protected Configuration getConfiguration() throws IOException { | |
97 | + public static Configuration getConfiguration(ServletContext context) throws IOException { | |
87 | 98 | Configuration config = configurations.get(context); |
88 | 99 | if(config == null) { |
89 | 100 | synchronized(configurations) { |
90 | 101 | config = configurations.get(context); |
91 | 102 | if(config == null) { |
92 | - config = createConfiguration(); | |
103 | + config = createConfiguration(context); | |
93 | 104 | configurations.put(context, config); |
94 | 105 | } |
95 | 106 | } |
@@ -96,16 +107,24 @@ | ||
96 | 107 | } |
97 | 108 | return config; |
98 | 109 | } |
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); | |
102 | 113 | builder.setExposeFields(true); |
103 | 114 | ObjectWrapper objectWrapper = builder.build(); |
104 | 115 | |
105 | - Configuration config = new Configuration(Configuration.VERSION_2_3_29); | |
116 | + Configuration config = new Configuration(Configuration.VERSION_2_3_30); | |
106 | 117 | config.setDirectoryForTemplateLoading(new File(context.getRealPath(TEMPLATES_PATH))); |
107 | 118 | config.setDefaultEncoding("UTF-8"); |
108 | 119 | config.setObjectWrapper(objectWrapper); |
120 | + | |
121 | + if(configurationUnaryOperator != null) { | |
122 | + Configuration c = configurationUnaryOperator.apply(config); | |
123 | + if(c != null) { | |
124 | + config = c; | |
125 | + } | |
126 | + } | |
127 | + | |
109 | 128 | return config; |
110 | 129 | } |
111 | 130 |