1 | package fr.sii.ogham.template.freemarker.configurer; | |
2 | ||
3 | import static fr.sii.ogham.core.builder.configuration.MayOverride.overrideIfNotSet; | |
4 | import static fr.sii.ogham.template.freemarker.FreemarkerConstants.DEFAULT_FREEMARKER_EMAIL_CONFIGURER_PRIORITY; | |
5 | ||
6 | import org.slf4j.Logger; | |
7 | import org.slf4j.LoggerFactory; | |
8 | ||
9 | import fr.sii.ogham.core.builder.MessagingBuilder; | |
10 | import fr.sii.ogham.core.builder.configurer.ConfigurerFor; | |
11 | import fr.sii.ogham.core.builder.configurer.DefaultMessagingConfigurer; | |
12 | import fr.sii.ogham.core.builder.configurer.MessagingConfigurer; | |
13 | import fr.sii.ogham.core.builder.configurer.MessagingConfigurerAdapter; | |
14 | import fr.sii.ogham.core.builder.context.BuildContext; | |
15 | import fr.sii.ogham.core.builder.resolution.ResourceResolutionBuilder; | |
16 | import fr.sii.ogham.core.message.content.EmailVariant; | |
17 | import fr.sii.ogham.core.util.ClasspathUtils; | |
18 | import fr.sii.ogham.template.freemarker.FreeMarkerTemplateDetector; | |
19 | import fr.sii.ogham.template.freemarker.builder.FreemarkerEmailBuilder; | |
20 | import freemarker.template.TemplateExceptionHandler; | |
21 | ||
22 | /** | |
23 | * Default configurer for Freemarker template engine that is automatically | |
24 | * applied every time a {@link MessagingBuilder} instance is created through | |
25 | * {@link MessagingBuilder#standard()} or {@link MessagingBuilder#minimal()}. | |
26 | * | |
27 | * <p> | |
28 | * The configurer has a priority of 80000 in order to be applied after global | |
29 | * configurer but before any sender implementation. | |
30 | * </p> | |
31 | * | |
32 | * This configurer is applied only if {@code freemarker.template.Configuration} | |
33 | * and {@code freemarker.template.Template} are present in the classpath. If not | |
34 | * present, template engine is not registered at all. | |
35 | * | |
36 | * <p> | |
37 | * This configurer inherits environment configuration (see | |
38 | * {@link BuildContext}). | |
39 | * </p> | |
40 | * <p> | |
41 | * It also copies resource resolution configuration of | |
42 | * {@link DefaultMessagingConfigurer} to inherit resource resolution lookups | |
43 | * (see {@link ResourceResolutionBuilder}). | |
44 | * </p> | |
45 | * | |
46 | * <p> | |
47 | * This configurer applies the following configuration: | |
48 | * <ul> | |
49 | * <li>Configures template prefix/suffix paths: | |
50 | * <ul> | |
51 | * <li>Uses the first property that has a value for classpath resolution prefix: | |
52 | * <ol> | |
53 | * <li>"ogham.email.freemarker.classpath.path-prefix"</li> | |
54 | * <li>"ogham.email.template.classpath.path-prefix"</li> | |
55 | * <li>"ogham.email.freemarker.path-prefix"</li> | |
56 | * <li>"ogham.email.template.path-prefix"</li> | |
57 | * <li>"ogham.template.path-prefix"</li> | |
58 | * </ol> | |
59 | * </li> | |
60 | * <li>Uses the first property that has a value for classpath resolution suffix: | |
61 | * <ol> | |
62 | * <li>"ogham.email.freemarker.classpath.path-suffix"</li> | |
63 | * <li>"ogham.email.template.classpath.path-suffix"</li> | |
64 | * <li>"ogham.email.freemarker.path-suffix"</li> | |
65 | * <li>"ogham.email.template.path-suffix"</li> | |
66 | * <li>"ogham.template.path-suffix"</li> | |
67 | * </ol> | |
68 | * </li> | |
69 | * <li>Uses the first property that has a value for file resolution prefix: | |
70 | * <ol> | |
71 | * <li>"ogham.email.freemarker.file.path-prefix"</li> | |
72 | * <li>"ogham.email.template.file.path-prefix"</li> | |
73 | * <li>"ogham.email.freemarker.path-prefix"</li> | |
74 | * <li>"ogham.email.template.path-prefix"</li> | |
75 | * <li>"ogham.template.path-prefix"</li> | |
76 | * </ol> | |
77 | * </li> | |
78 | * <li>Uses the first property that has a value for file resolution suffix: | |
79 | * <ol> | |
80 | * <li>"ogham.email.freemarker.file.path-suffix"</li> | |
81 | * <li>"ogham.email.template.file.path-suffix"</li> | |
82 | * <li>"ogham.email.freemarker.path-suffix"</li> | |
83 | * <li>"ogham.email.template.path-suffix"</li> | |
84 | * <li>"ogham.template.path-suffix"</li> | |
85 | * </ol> | |
86 | * </li> | |
87 | * </ul> | |
88 | * </li> | |
89 | * <li>Configures email alternative content: | |
90 | * <ul> | |
91 | * <li>Automatically loads HTML template if extension is .html.ftl</li> | |
92 | * <li>Automatically loads text template if extension is .txt.ftl</li> | |
93 | * </ul> | |
94 | * </li> | |
95 | * <li>Configures encoding: | |
96 | * <ul> | |
97 | * <li>It uses "ogham.freemarker.default-encoding" property value as charset for | |
98 | * template parsing if defined. Default charset is UTF-8</li> | |
99 | * </ul> | |
100 | * </li> | |
101 | * <li>Configures template detection: | |
102 | * <ul> | |
103 | * <li>Uses {@link FreeMarkerTemplateDetector} to detect if templates are | |
104 | * parseable by Freemarker</li> | |
105 | * </ul> | |
106 | * </li> | |
107 | * <li>Configures static method access from templates: | |
108 | * <ul> | |
109 | * <li>Uses property value of ${ogham.freemarker.static-method-access.enable} if | |
110 | * provided to enable/disable static method access from templates (default is | |
111 | * enabled is nothing is configured)</li> | |
112 | * <li>Uses property value of | |
113 | * ${ogham.freemarker.static-method-access.variable-name} if provided to set the | |
114 | * name used to access static methods from templates (default is 'statics')</li> | |
115 | * </ul> | |
116 | * </li> | |
117 | * </ul> | |
118 | * | |
119 | * @author Aurélien Baudet | |
120 | * | |
121 | */ | |
122 | public final class DefaultFreemarkerEmailConfigurer { | |
123 | private static final Logger LOG = LoggerFactory.getLogger(DefaultFreemarkerEmailConfigurer.class); | |
124 | ||
125 | @ConfigurerFor(targetedBuilder = { "minimal", "standard" }, priority = DEFAULT_FREEMARKER_EMAIL_CONFIGURER_PRIORITY) | |
126 | public static class FreemakerConfigurer implements MessagingConfigurer { | |
127 | private final MessagingConfigurerAdapter delegate; | |
128 | ||
129 | public FreemakerConfigurer() { | |
130 | this(new DefaultMessagingConfigurer()); | |
131 | } | |
132 | ||
133 | public FreemakerConfigurer(MessagingConfigurerAdapter delegate) { | |
134 | super(); | |
135 | this.delegate = delegate; | |
136 | } | |
137 | ||
138 | @Override | |
139 | public void configure(MessagingBuilder msgBuilder) { | |
140 |
4
1. configure : negated conditional → NO_COVERAGE 2. configure : negated conditional → KILLED 3. configure : negated conditional → KILLED 4. configure : negated conditional → KILLED |
if (!canUseFreemaker()) { |
141 | LOG.debug("[{}] skip configuration", this); | |
142 | return; | |
143 | } | |
144 | LOG.debug("[{}] apply configuration", this); | |
145 | FreemarkerEmailBuilder builder = msgBuilder.email().template(FreemarkerEmailBuilder.class); | |
146 | // apply default resource resolution configuration | |
147 |
4
1. configure : negated conditional → NO_COVERAGE 2. configure : negated conditional → TIMED_OUT 3. configure : negated conditional → KILLED 4. configure : negated conditional → KILLED |
if (delegate != null) { |
148 |
4
1. configure : removed call to fr/sii/ogham/core/builder/configurer/MessagingConfigurerAdapter::configure → NO_COVERAGE 2. configure : removed call to fr/sii/ogham/core/builder/configurer/MessagingConfigurerAdapter::configure → KILLED 3. configure : removed call to fr/sii/ogham/core/builder/configurer/MessagingConfigurerAdapter::configure → KILLED 4. configure : removed call to fr/sii/ogham/core/builder/configurer/MessagingConfigurerAdapter::configure → KILLED |
delegate.configure(builder); |
149 | } | |
150 | // @formatter:off | |
151 | builder | |
152 | .classpath() | |
153 | .pathPrefix() | |
154 | .properties("${ogham.email.freemarker.classpath.path-prefix}", | |
155 | "${ogham.email.template.classpath.path-prefix}", | |
156 | "${ogham.email.freemarker.path-prefix}", | |
157 | "${ogham.email.template.path-prefix}", | |
158 | "${ogham.template.path-prefix}") | |
159 | .and() | |
160 | .pathSuffix() | |
161 | .properties("${ogham.email.freemarker.classpath.path-suffix}", | |
162 | "${ogham.email.template.classpath.path-suffix}", | |
163 | "${ogham.email.freemarker.path-suffix}", | |
164 | "${ogham.email.template.path-suffix}", | |
165 | "${ogham.template.path-suffix}") | |
166 | .and() | |
167 | .and() | |
168 | .file() | |
169 | .pathPrefix() | |
170 | .properties("${ogham.email.freemarker.file.path-prefix}", | |
171 | "${ogham.email.template.file.path-prefix}", | |
172 | "${ogham.email.freemarker.path-prefix}", | |
173 | "${ogham.email.template.path-prefix}", | |
174 | "${ogham.template.path-prefix}") | |
175 | .and() | |
176 | .pathSuffix() | |
177 | .properties("${ogham.email.freemarker.file.path-suffix}", | |
178 | "${ogham.email.template.file.path-suffix}", | |
179 | "${ogham.email.freemarker.path-suffix}", | |
180 | "${ogham.email.template.path-suffix}", | |
181 | "${ogham.template.path-suffix}") | |
182 | .and() | |
183 | .and() | |
184 | .string() | |
185 | .and() | |
186 | .variant(EmailVariant.HTML, "html.ftl") | |
187 | .variant(EmailVariant.HTML, "html.ftlh") | |
188 | .variant(EmailVariant.TEXT, "txt.ftl") | |
189 | .variant(EmailVariant.TEXT, "txt.ftlh") | |
190 | .configuration() | |
191 | .defaultEncoding().properties("${ogham.freemarker.default-encoding}").defaultValue(overrideIfNotSet("UTF-8")).and() | |
192 | .templateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER) | |
193 | .enableStaticMethodAccess().properties("${ogham.freemarker.static-method-access.enable}").defaultValue(overrideIfNotSet(true)).and() | |
194 | .staticMethodAccessVariableName().properties("${ogham.freemarker.static-method-access.variable-name}").defaultValue(overrideIfNotSet("statics")); | |
195 | // @formatter:on | |
196 | } | |
197 | ||
198 | private static boolean canUseFreemaker() { | |
199 |
10
1. canUseFreemaker : replaced boolean return with true for fr/sii/ogham/template/freemarker/configurer/DefaultFreemarkerEmailConfigurer$FreemakerConfigurer::canUseFreemaker → SURVIVED 2. canUseFreemaker : replaced boolean return with true for fr/sii/ogham/template/freemarker/configurer/DefaultFreemarkerEmailConfigurer$FreemakerConfigurer::canUseFreemaker → NO_COVERAGE 3. canUseFreemaker : negated conditional → NO_COVERAGE 4. canUseFreemaker : negated conditional → NO_COVERAGE 5. canUseFreemaker : negated conditional → KILLED 6. canUseFreemaker : negated conditional → KILLED 7. canUseFreemaker : negated conditional → KILLED 8. canUseFreemaker : negated conditional → KILLED 9. canUseFreemaker : negated conditional → KILLED 10. canUseFreemaker : negated conditional → KILLED |
return ClasspathUtils.exists("freemarker.template.Configuration") && ClasspathUtils.exists("freemarker.template.Template"); |
200 | } | |
201 | } | |
202 | ||
203 | private DefaultFreemarkerEmailConfigurer() { | |
204 | super(); | |
205 | } | |
206 | } | |
Mutations | ||
140 |
1.1 2.2 3.3 4.4 |
|
147 |
1.1 2.2 3.3 4.4 |
|
148 |
1.1 2.2 3.3 4.4 |
|
199 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10 |