DefaultFreemarkerSmsConfigurer.java

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

Mutations

130

1.1
Location : configure
Killed by :
removed call to fr/sii/ogham/template/freemarker/configurer/DefaultFreemarkerSmsConfigurer$FreemakerConfigurer::checkCanUseFreemarker → RUN_ERROR

134

1.1
Location : configure
Killed by :
negated conditional → RUN_ERROR

135

1.1
Location : configure
Killed by :
removed call to fr/sii/ogham/core/builder/configurer/MessagingConfigurerAdapter::configure → RUN_ERROR

180

1.1
Location : checkCanUseFreemarker
Killed by :
negated conditional → RUN_ERROR

186

1.1
Location : isFreemarkerPresent
Killed by :
negated conditional → RUN_ERROR

2.2
Location : isFreemarkerPresent
Killed by :
replaced boolean return with true for fr/sii/ogham/template/freemarker/configurer/DefaultFreemarkerSmsConfigurer$FreemakerConfigurer::isFreemarkerPresent → RUN_ERROR

3.3
Location : isFreemarkerPresent
Killed by :
negated conditional → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1