| 1 | package fr.sii.ogham.spring.template; | |
| 2 | ||
| 3 | import static fr.sii.ogham.core.util.ConfigurationValueUtils.firstValue; | |
| 4 | import static java.util.Optional.ofNullable; | |
| 5 | ||
| 6 | import org.slf4j.Logger; | |
| 7 | import org.slf4j.LoggerFactory; | |
| 8 | import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties; | |
| 9 | ||
| 10 | import fr.sii.ogham.core.builder.configurer.MessagingConfigurerAdapter; | |
| 11 | import fr.sii.ogham.email.builder.EmailBuilder; | |
| 12 | import fr.sii.ogham.sms.builder.SmsBuilder; | |
| 13 | import fr.sii.ogham.spring.common.OghamTemplateProperties; | |
| 14 | import fr.sii.ogham.spring.common.SpringMessagingConfigurer; | |
| 15 | import fr.sii.ogham.spring.email.OghamEmailProperties; | |
| 16 | import fr.sii.ogham.spring.sms.OghamSmsProperties; | |
| 17 | import fr.sii.ogham.spring.template.thymeleaf.TemplateEngineSupplier; | |
| 18 | import fr.sii.ogham.template.thymeleaf.common.ThymeleafConstants; | |
| 19 | import fr.sii.ogham.template.thymeleaf.common.ThymeleafContextConverter; | |
| 20 | import fr.sii.ogham.template.thymeleaf.common.ThymeleafParser; | |
| 21 | import fr.sii.ogham.template.thymeleaf.common.buider.AbstractThymeleafBuilder; | |
| 22 | ||
| 23 | /** | |
| 24 | * Integrates with Spring templating system by using SpringTemplateEngine object | |
| 25 | * provided by Spring and by using Spring properties defined with prefix | |
| 26 | * {@code spring.thymeleaf} (see {@link ThymeleafProperties}). | |
| 27 | * | |
| 28 | * If both Spring property and Ogham property is defined, Ogham property is | |
| 29 | * used. | |
| 30 | * | |
| 31 | * For example, if the file application.properties contains the following | |
| 32 | * configuration: | |
| 33 | * | |
| 34 | * <pre> | |
| 35 | * spring.thymeleaf.prefix=/email/ | |
| 36 | * ogham.email.thymeleaf.path-prefix=/foo/ | |
| 37 | * </pre> | |
| 38 | * | |
| 39 | * The {@link ThymeleafParser} will use the templates in "/foo/". | |
| 40 | * | |
| 41 | * <p> | |
| 42 | * This configurer is also useful to support property naming variants (see | |
| 43 | * <a href= | |
| 44 | * "https://github.com/spring-projects/spring-boot/wiki/relaxed-binding-2.0">Relaxed | |
| 45 | * Binding</a>). | |
| 46 | * | |
| 47 | * | |
| 48 | * @author Aurélien Baudet | |
| 49 | * | |
| 50 | */ | |
| 51 | public class ThymeLeafConfigurer extends MessagingConfigurerAdapter implements SpringMessagingConfigurer { | |
| 52 | private static final Logger LOG = LoggerFactory.getLogger(ThymeLeafConfigurer.class); | |
| 53 | ||
| 54 | private final TemplateEngineSupplier springTemplateEngineSupplier; | |
| 55 | private final ThymeleafContextConverter contextConverter; | |
| 56 | private final OghamCommonTemplateProperties templateProperties; | |
| 57 | private final OghamEmailProperties emailProperties; | |
| 58 | private final OghamSmsProperties smsProperties; | |
| 59 | private final ThymeleafProperties springProperties; | |
| 60 | private final Class<? extends AbstractThymeleafBuilder<?, ?, ?>> emailBuilderClass; | |
| 61 | private final Class<? extends AbstractThymeleafBuilder<?, ?, ?>> smsBuilderClass; | |
| 62 | ||
| 63 | public ThymeLeafConfigurer(TemplateEngineSupplier springTemplateEngineSupplier, ThymeleafContextConverter contextConverter, OghamCommonTemplateProperties templateProperties, | |
| 64 | OghamEmailProperties emailProperties, OghamSmsProperties smsProperties, ThymeleafProperties springProperties, Class<? extends AbstractThymeleafBuilder<?, ?, ?>> emailBuilderClass, | |
| 65 | Class<? extends AbstractThymeleafBuilder<?, ?, ?>> smsBuilderClass) { | |
| 66 | super(); | |
| 67 | this.springTemplateEngineSupplier = springTemplateEngineSupplier; | |
| 68 | this.contextConverter = contextConverter; | |
| 69 | this.templateProperties = templateProperties; | |
| 70 | this.emailProperties = emailProperties; | |
| 71 | this.smsProperties = smsProperties; | |
| 72 | this.springProperties = springProperties; | |
| 73 | this.emailBuilderClass = emailBuilderClass; | |
| 74 | this.smsBuilderClass = smsBuilderClass; | |
| 75 | } | |
| 76 | ||
| 77 | @Override | |
| 78 | public void configure(EmailBuilder emailBuilder) { | |
| 79 | AbstractThymeleafBuilder<?, ?, ?> builder = emailBuilder.template(emailBuilderClass); | |
| 80 |
2
1. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::configureSpringEngine → KILLED 2. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::configureSpringEngine → KILLED |
configureSpringEngine(builder); |
| 81 | // specific Ogham properties explicitly take precedence over Spring | |
| 82 | // properties | |
| 83 |
2
1. configure : negated conditional → KILLED 2. configure : negated conditional → KILLED |
if (springProperties != null) { |
| 84 |
1
1. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::applySpringConfiguration → SURVIVED |
applySpringConfiguration(builder); |
| 85 | } | |
| 86 |
1
1. configure : negated conditional → SURVIVED |
if (emailProperties != null) { |
| 87 |
1
1. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::applyOghamConfiguration → SURVIVED |
applyOghamConfiguration(builder, emailProperties); |
| 88 | } | |
| 89 | } | |
| 90 | ||
| 91 | @Override | |
| 92 | public void configure(SmsBuilder smsBuilder) { | |
| 93 | AbstractThymeleafBuilder<?, ?, ?> builder = smsBuilder.template(smsBuilderClass); | |
| 94 |
2
1. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::configureSpringEngine → KILLED 2. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::configureSpringEngine → KILLED |
configureSpringEngine(builder); |
| 95 | // specific Ogham properties explicitly take precedence over Spring | |
| 96 | // properties | |
| 97 |
2
1. configure : negated conditional → KILLED 2. configure : negated conditional → KILLED |
if (springProperties != null) { |
| 98 |
1
1. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::applySpringConfiguration → SURVIVED |
applySpringConfiguration(builder); |
| 99 | } | |
| 100 |
1
1. configure : negated conditional → SURVIVED |
if (smsProperties != null) { |
| 101 |
1
1. configure : removed call to fr/sii/ogham/spring/template/ThymeLeafConfigurer::applyOghamConfiguration → SURVIVED |
applyOghamConfiguration(builder, smsProperties); |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | @Override | |
| 106 | public int getOrder() { | |
| 107 |
1
1. getOrder : replaced int return with 0 for fr/sii/ogham/spring/template/ThymeLeafConfigurer::getOrder → SURVIVED |
return ThymeleafConstants.DEFAULT_THYMELEAF_EMAIL_CONFIGURER_PRIORITY + 1000; |
| 108 | } | |
| 109 | ||
| 110 | private void configureSpringEngine(AbstractThymeleafBuilder<?, ?, ?> builder) { | |
| 111 |
2
1. configureSpringEngine : negated conditional → KILLED 2. configureSpringEngine : negated conditional → KILLED |
if (springTemplateEngineSupplier != null) { |
| 112 | builder.engine(springTemplateEngineSupplier.get()); | |
| 113 | } | |
| 114 |
2
1. configureSpringEngine : negated conditional → KILLED 2. configureSpringEngine : negated conditional → KILLED |
if (contextConverter != null) { |
| 115 | builder.contextConverter(contextConverter); | |
| 116 | } | |
| 117 | } | |
| 118 | ||
| 119 | private void applyOghamConfiguration(AbstractThymeleafBuilder<?, ?, ?> builder, OghamTemplateProperties props) { | |
| 120 | LOG.debug("[{}] apply ogham configuration properties to {}", this, builder); | |
| 121 | // @formatter:off | |
| 122 | builder | |
| 123 | .classpath() | |
| 124 | .pathPrefix() | |
| 125 | .value(ofNullable(firstValue(props.getThymeleaf().getClasspath().getPathPrefix(), | |
| 126 | props.getTemplate().getClasspath().getPathPrefix(), | |
| 127 | props.getThymeleaf().getPathPrefix(), | |
| 128 | props.getTemplate().getPathPrefix(), | |
| 129 | templateProperties.getPathPrefix()))) | |
| 130 | .and() | |
| 131 | .pathSuffix() | |
| 132 | .value(ofNullable(firstValue(props.getThymeleaf().getClasspath().getPathSuffix(), | |
| 133 | props.getTemplate().getClasspath().getPathSuffix(), | |
| 134 | props.getThymeleaf().getPathSuffix(), | |
| 135 | props.getTemplate().getPathSuffix(), | |
| 136 | templateProperties.getPathSuffix()))) | |
| 137 | .and() | |
| 138 | .and() | |
| 139 | .file() | |
| 140 | .pathPrefix() | |
| 141 | .value(ofNullable(firstValue(props.getThymeleaf().getFile().getPathPrefix(), | |
| 142 | props.getTemplate().getFile().getPathPrefix(), | |
| 143 | props.getThymeleaf().getPathPrefix(), | |
| 144 | props.getTemplate().getPathPrefix(), | |
| 145 | templateProperties.getPathPrefix()))) | |
| 146 | .and() | |
| 147 | .pathSuffix() | |
| 148 | .value(ofNullable(firstValue(props.getThymeleaf().getFile().getPathSuffix(), | |
| 149 | props.getTemplate().getFile().getPathSuffix(), | |
| 150 | props.getThymeleaf().getPathSuffix(), | |
| 151 | props.getTemplate().getPathSuffix(), | |
| 152 | templateProperties.getPathSuffix()))) | |
| 153 | .and() | |
| 154 | .and() | |
| 155 | .cache().value(ofNullable(firstValue(props.getThymeleaf().getCache(), | |
| 156 | props.getTemplate().getCache(), | |
| 157 | templateProperties.getCache()))); | |
| 158 | // @formatter:on | |
| 159 | } | |
| 160 | ||
| 161 | private void applySpringConfiguration(AbstractThymeleafBuilder<?, ?, ?> builder) { | |
| 162 | LOG.debug("[{}] apply spring configuration properties to {}", this, builder); | |
| 163 | // @formatter:off | |
| 164 | builder | |
| 165 | .classpath() | |
| 166 | .pathPrefix().value(ofNullable(springProperties.getPrefix())).and() | |
| 167 | .pathSuffix().value(ofNullable(springProperties.getSuffix())).and() | |
| 168 | .and() | |
| 169 | .file() | |
| 170 | .pathPrefix().value(ofNullable(springProperties.getPrefix())).and() | |
| 171 | .pathSuffix().value(ofNullable(springProperties.getSuffix())) | |
| 172 | .and() | |
| 173 | .and() | |
| 174 | .cache().value(ofNullable(springProperties.isCache())); | |
| 175 | // @formatter:on | |
| 176 | } | |
| 177 | ||
| 178 | } | |
Mutations | ||
| 80 |
1.1 2.2 |
|
| 83 |
1.1 2.2 |
|
| 84 |
1.1 |
|
| 86 |
1.1 |
|
| 87 |
1.1 |
|
| 94 |
1.1 2.2 |
|
| 97 |
1.1 2.2 |
|
| 98 |
1.1 |
|
| 100 |
1.1 |
|
| 101 |
1.1 |
|
| 107 |
1.1 |
|
| 111 |
1.1 2.2 |
|
| 114 |
1.1 2.2 |