1 | package fr.sii.ogham.spring.email; | |
2 | ||
3 | import static java.util.Optional.ofNullable; | |
4 | ||
5 | import org.slf4j.Logger; | |
6 | import org.slf4j.LoggerFactory; | |
7 | import org.springframework.boot.autoconfigure.mail.MailProperties; | |
8 | ||
9 | import fr.sii.ogham.core.builder.MessagingBuilder; | |
10 | import fr.sii.ogham.email.JavaMailConstants; | |
11 | import fr.sii.ogham.email.builder.javamail.JavaMailBuilder; | |
12 | import fr.sii.ogham.email.sender.impl.JavaMailSender; | |
13 | import fr.sii.ogham.spring.common.SpringMessagingConfigurer; | |
14 | ||
15 | /** | |
16 | * Integrates with Spring Mail by using Spring properties defined with prefix | |
17 | * {@code spring.mail} (see {@link MailProperties}). | |
18 | * | |
19 | * If both Spring property and Ogham property is defined, Ogham property is | |
20 | * used. | |
21 | * | |
22 | * For example, if the file application.properties contains the following | |
23 | * configuration: | |
24 | * | |
25 | * <pre> | |
26 | * spring.mail.host=localhost | |
27 | * ogham.email.javamail.port=3025 | |
28 | * </pre> | |
29 | * | |
30 | * The {@link JavaMailSender} will use the address "localhost:3025" to connect | |
31 | * to the SMTP server. | |
32 | * | |
33 | * <p> | |
34 | * This configurer is also useful to support property naming variants (see | |
35 | * <a href= | |
36 | * "https://github.com/spring-projects/spring-boot/wiki/relaxed-binding-2.0">Relaxed | |
37 | * Binding</a>). | |
38 | * | |
39 | * @author Aurélien Baudet | |
40 | * | |
41 | */ | |
42 | public class SpringMailConfigurer implements SpringMessagingConfigurer { | |
43 | private static final Logger LOG = LoggerFactory.getLogger(SpringMailConfigurer.class); | |
44 | ||
45 | private final OghamJavaMailProperties properties; | |
46 | private final MailProperties springMailProperties; | |
47 | ||
48 | public SpringMailConfigurer(OghamJavaMailProperties properties, MailProperties springMailProperties) { | |
49 | super(); | |
50 | this.properties = properties; | |
51 | this.springMailProperties = springMailProperties; | |
52 | } | |
53 | ||
54 | @Override | |
55 | public void configure(MessagingBuilder builder) { | |
56 | LOG.debug("[{}] apply configuration", this); | |
57 | // Ogham specific properties take precedence over Spring properties if | |
58 | // specified | |
59 |
2
1. configure : negated conditional → KILLED 2. configure : negated conditional → KILLED |
if (springMailProperties != null) { |
60 |
2
1. configure : removed call to fr/sii/ogham/spring/email/SpringMailConfigurer::applySpringMailConfiguration → KILLED 2. configure : removed call to fr/sii/ogham/spring/email/SpringMailConfigurer::applySpringMailConfiguration → KILLED |
applySpringMailConfiguration(builder); |
61 | } | |
62 |
2
1. configure : negated conditional → KILLED 2. configure : negated conditional → KILLED |
if (properties != null) { |
63 |
2
1. configure : removed call to fr/sii/ogham/spring/email/SpringMailConfigurer::applyOghamConfiguration → KILLED 2. configure : removed call to fr/sii/ogham/spring/email/SpringMailConfigurer::applyOghamConfiguration → KILLED |
applyOghamConfiguration(builder); |
64 | } | |
65 | } | |
66 | ||
67 | private void applyOghamConfiguration(MessagingBuilder builder) { | |
68 | LOG.debug("[{}] apply ogham configuration properties to {}", this, builder); | |
69 | // @formatter:off | |
70 | builder.email() | |
71 | .sender(JavaMailBuilder.class) | |
72 | .authenticator() | |
73 | .username().value(ofNullable(properties.getAuthenticator().getUsername())).and() | |
74 | .password().value(ofNullable(properties.getAuthenticator().getPassword())).and() | |
75 | .and() | |
76 | .charset().value(ofNullable(properties.getBody().getCharset())).and() | |
77 | .host().value(ofNullable(properties.getHost())).and() | |
78 | .port().value(ofNullable(properties.getPort())); | |
79 | // @formatter:on | |
80 | } | |
81 | ||
82 | private void applySpringMailConfiguration(MessagingBuilder builder) { | |
83 | LOG.debug("[{}] apply spring mail configuration properties to {}", this, builder); | |
84 | // @formatter:off | |
85 | builder.email() | |
86 | .sender(JavaMailBuilder.class) | |
87 | .authenticator() | |
88 | .username().value(ofNullable(springMailProperties.getUsername())).and() | |
89 | .password().value(ofNullable(springMailProperties.getPassword())).and() | |
90 | .and() | |
91 | .charset().value(ofNullable(springMailProperties.getDefaultEncoding())).and() | |
92 | .host().value(ofNullable(springMailProperties.getHost())).and() | |
93 | .port().value(ofNullable(springMailProperties.getPort())).and() | |
94 | .properties(springMailProperties.getProperties()); | |
95 | // @formatter:on | |
96 | } | |
97 | | |
98 | ||
99 | @Override | |
100 | public int getOrder() { | |
101 |
1
1. getOrder : replaced int return with 0 for fr/sii/ogham/spring/email/SpringMailConfigurer::getOrder → SURVIVED |
return JavaMailConstants.DEFAULT_JAVAMAIL_CONFIGURER_PRIORITY + 1000; |
102 | } | |
103 | ||
104 | } | |
Mutations | ||
59 |
1.1 2.2 |
|
60 |
1.1 2.2 |
|
62 |
1.1 2.2 |
|
63 |
1.1 2.2 |
|
101 |
1.1 |