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