| 1 | package fr.sii.ogham.template.thymeleaf.common.configure; | |
| 2 | ||
| 3 | import org.slf4j.Logger; | |
| 4 | ||
| 5 | import fr.sii.ogham.core.builder.MessagingBuilder; | |
| 6 | import fr.sii.ogham.core.builder.configurer.DefaultMessagingConfigurer; | |
| 7 | import fr.sii.ogham.core.builder.configurer.MessagingConfigurer; | |
| 8 | import fr.sii.ogham.core.builder.configurer.MessagingConfigurerAdapter; | |
| 9 | import fr.sii.ogham.core.builder.env.EnvironmentBuilder; | |
| 10 | import fr.sii.ogham.core.builder.resolution.ResourceResolutionBuilder; | |
| 11 | import fr.sii.ogham.core.message.content.EmailVariant; | |
| 12 | import fr.sii.ogham.template.thymeleaf.common.buider.AbstractThymeleafMultiContentBuilder; | |
| 13 | ||
| 14 | /** | |
| 15 | * Default configurer for Thymeleaf template engine that is automatically | |
| 16 | * applied every time a {@link MessagingBuilder} instance is created through | |
| 17 | * {@link MessagingBuilder#standard()} or {@link MessagingBuilder#minimal()}. | |
| 18 | * | |
| 19 | * <p> | |
| 20 | * The configurer has a priority of 90000 in order to be applied after global | |
| 21 | * configurer but before any sender implementation. | |
| 22 | * </p> | |
| 23 | * | |
| 24 | * This configurer is applied only if {@code org.thymeleaf.TemplateEngine} is | |
| 25 | * present in the classpath. If not present, template engine is not registered | |
| 26 | * at all. | |
| 27 | * | |
| 28 | * <p> | |
| 29 | * This configurer inherits environment configuration (see | |
| 30 | * {@link EnvironmentBuilder}) | |
| 31 | * </p> | |
| 32 | * <p> | |
| 33 | * It also copies resource resolution configuration of | |
| 34 | * {@link DefaultMessagingConfigurer} to inherit resource resolution lookups | |
| 35 | * (see {@link ResourceResolutionBuilder}). | |
| 36 | * </p> | |
| 37 | * | |
| 38 | * <p> | |
| 39 | * This configurer applies the following configuration: | |
| 40 | * <ul> | |
| 41 | * <li>Configures template prefix/suffix paths: | |
| 42 | * <ul> | |
| 43 | * <li>Uses the first property that has a value for classpath resolution prefix: | |
| 44 | * <ol> | |
| 45 | * <li>"ogham.email.thymeleaf.classpath.path-prefix"</li> | |
| 46 | * <li>"ogham.email.template.classpath.path-prefix"</li> | |
| 47 | * <li>"ogham.email.thymeleaf.path-prefix"</li> | |
| 48 | * <li>"ogham.email.template.path-prefix"</li> | |
| 49 | * <li>"ogham.template.path-prefix"</li> | |
| 50 | * </ol> | |
| 51 | * </li> | |
| 52 | * <li>Uses the first property that has a value for classpath resolution suffix: | |
| 53 | * <ol> | |
| 54 | * <li>"ogham.email.thymeleaf.classpath.path-suffix"</li> | |
| 55 | * <li>"ogham.email.template.classpath.path-suffix"</li> | |
| 56 | * <li>"ogham.email.thymeleaf.path-suffix"</li> | |
| 57 | * <li>"ogham.email.template.path-suffix"</li> | |
| 58 | * <li>"ogham.template.path-suffix"</li> | |
| 59 | * </ol> | |
| 60 | * </li> | |
| 61 | * <li>Uses the first property that has a value for file resolution prefix: | |
| 62 | * <ol> | |
| 63 | * <li>"ogham.email.thymeleaf.file.path-prefix"</li> | |
| 64 | * <li>"ogham.email.template.file.path-prefix"</li> | |
| 65 | * <li>"ogham.email.thymeleaf.path-prefix"</li> | |
| 66 | * <li>"ogham.email.template.path-prefix"</li> | |
| 67 | * <li>"ogham.template.path-prefix"</li> | |
| 68 | * </ol> | |
| 69 | * </li> | |
| 70 | * <li>Uses the first property that has a value for file resolution suffix: | |
| 71 | * <ol> | |
| 72 | * <li>"ogham.email.thymeleaf.file.path-suffix"</li> | |
| 73 | * <li>"ogham.email.template.file.path-suffix"</li> | |
| 74 | * <li>"ogham.email.thymeleaf.path-suffix"</li> | |
| 75 | * <li>"ogham.email.template.path-suffix"</li> | |
| 76 | * <li>"ogham.template.path-suffix"</li> | |
| 77 | * </ol> | |
| 78 | * </li> | |
| 79 | * </ul> | |
| 80 | * </li> | |
| 81 | * <li>Configures email alternative content: | |
| 82 | * <ul> | |
| 83 | * <li>Automatically loads HTML template if extension is .html</li> | |
| 84 | * <li>Automatically loads text template if extension is .txt</li> | |
| 85 | * </ul> | |
| 86 | * </li> | |
| 87 | * <li>Configures template detection: | |
| 88 | * <ul> | |
| 89 | * <li>Uses ThymeleafTemplateDetector to detect if templates are | |
| 90 | * parseable by Thymeleaf</li> | |
| 91 | * </ul> | |
| 92 | * </li> | |
| 93 | * </ul> | |
| 94 | * | |
| 95 | * @author Aurélien Baudet | |
| 96 | * | |
| 97 | */ | |
| 98 | public abstract class AbstractDefaultThymeleafEmailConfigurer implements MessagingConfigurer { | |
| 99 | private final Logger log; | |
| 100 | private final MessagingConfigurerAdapter delegate; | |
| 101 | ||
| 102 | public AbstractDefaultThymeleafEmailConfigurer(Logger log) { | |
| 103 | this(log, new DefaultMessagingConfigurer()); | |
| 104 | } | |
| 105 | ||
| 106 | public AbstractDefaultThymeleafEmailConfigurer(Logger log, MessagingConfigurerAdapter delegate) { | |
| 107 | super(); | |
| 108 | this.log = log; | |
| 109 | this.delegate = delegate; | |
| 110 | } | |
| 111 | ||
| 112 | @Override | |
| 113 | public void configure(MessagingBuilder msgBuilder) { | |
| 114 |
4
1. configure : negated conditional → NO_COVERAGE 2. configure : negated conditional → TIMED_OUT 3. configure : negated conditional → KILLED 4. configure : negated conditional → KILLED |
if (!canUseThymeleaf()) { |
| 115 | log.debug("[{}] skip configuration", this); | |
| 116 | return; | |
| 117 | } | |
| 118 | log.debug("[{}] apply configuration", this); | |
| 119 | AbstractThymeleafMultiContentBuilder<?, ?, ?> builder = msgBuilder.email().template(getBuilderClass()); | |
| 120 | // apply default resource resolution configuration | |
| 121 |
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) { |
| 122 |
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); |
| 123 | } | |
| 124 | // @formatter:off | |
| 125 | builder | |
| 126 | .classpath() | |
| 127 | .pathPrefix() | |
| 128 | .properties("${ogham.email.thymeleaf.classpath.path-prefix}", | |
| 129 | "${ogham.email.template.classpath.path-prefix}", | |
| 130 | "${ogham.email.thymeleaf.path-prefix}", | |
| 131 | "${ogham.email.template.path-prefix}", | |
| 132 | "${ogham.template.path-prefix}") | |
| 133 | .and() | |
| 134 | .pathSuffix() | |
| 135 | .properties("${ogham.email.thymeleaf.classpath.path-suffix}", | |
| 136 | "${ogham.email.template.classpath.path-suffix}", | |
| 137 | "${ogham.email.thymeleaf.path-suffix}", | |
| 138 | "${ogham.email.template.path-suffix}", | |
| 139 | "${ogham.template.path-suffix}") | |
| 140 | .and() | |
| 141 | .and() | |
| 142 | .file() | |
| 143 | .pathPrefix() | |
| 144 | .properties("${ogham.email.thymeleaf.file.path-prefix}", | |
| 145 | "${ogham.email.template.file.path-prefix}", | |
| 146 | "${ogham.email.thymeleaf.path-prefix}", | |
| 147 | "${ogham.email.template.path-prefix}", | |
| 148 | "${ogham.template.path-prefix}") | |
| 149 | .and() | |
| 150 | .pathSuffix() | |
| 151 | .properties("${ogham.email.thymeleaf.file.path-suffix}", | |
| 152 | "${ogham.email.template.file.path-suffix}", | |
| 153 | "${ogham.email.thymeleaf.path-suffix}", | |
| 154 | "${ogham.email.template.path-suffix}", | |
| 155 | "${ogham.template.path-suffix}") | |
| 156 | .and() | |
| 157 | .and() | |
| 158 | .string() | |
| 159 | .and() | |
| 160 | .variant(EmailVariant.HTML, "html") | |
| 161 | .variant(EmailVariant.HTML, "xhtml") | |
| 162 | .variant(EmailVariant.TEXT, "txt") | |
| 163 | .cache() | |
| 164 | .properties("${ogham.email.thymeleaf.cache}", | |
| 165 | "${ogham.email.template.cache}", | |
| 166 | "${ogham.template.cache}"); | |
| 167 | // @formatter:on | |
| 168 | } | |
| 169 | ||
| 170 | protected abstract Class<? extends AbstractThymeleafMultiContentBuilder<?, ?, ?>> getBuilderClass(); | |
| 171 | ||
| 172 | protected abstract boolean canUseThymeleaf(); | |
| 173 | ||
| 174 | } | |
Mutations | ||
| 114 |
1.1 2.2 3.3 4.4 |
|
| 121 |
1.1 2.2 3.3 4.4 |
|
| 122 |
1.1 2.2 3.3 4.4 |