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.sendgrid.SendGridProperties; | |
8 | ||
9 | import com.sendgrid.SendGrid; | |
10 | ||
11 | import fr.sii.ogham.core.builder.MessagingBuilder; | |
12 | import fr.sii.ogham.email.sendgrid.SendGridConstants; | |
13 | import fr.sii.ogham.email.sendgrid.builder.AbstractSendGridBuilder; | |
14 | import fr.sii.ogham.email.sendgrid.sender.SendGridSender; | |
15 | import fr.sii.ogham.spring.common.SpringMessagingConfigurer; | |
16 | ||
17 | /** | |
18 | * Integrates with Spring SendGrid by using Spring properties defined with | |
19 | * prefix {@code spring.sendgrid} (see {@link SendGridProperties}). | |
20 | * | |
21 | * If both Spring property and Ogham property is defined, Ogham property is | |
22 | * used. | |
23 | * | |
24 | * For example, if the file application.properties contains the following | |
25 | * configuration: | |
26 | * | |
27 | * <pre> | |
28 | * spring.sendgrid.api-key=foo | |
29 | * ogham.email.sendgrid.api-key=bar | |
30 | * </pre> | |
31 | * | |
32 | * The {@link SendGridSender} implementation will use foo to connect to the | |
33 | * SendGrid service. | |
34 | * | |
35 | * <p> | |
36 | * This configurer is also useful to support property naming variants (see | |
37 | * <a href= | |
38 | * "https://github.com/spring-projects/spring-boot/wiki/relaxed-binding-2.0">Relaxed | |
39 | * Binding</a>). | |
40 | * | |
41 | * @author Aurélien Baudet | |
42 | * | |
43 | */ | |
44 | public abstract class AbstractSpringSendGridConfigurer implements SpringMessagingConfigurer { | |
45 | private static final int SPRING_CONFIGURER_PRIORITY_OFFSET = 1000; | |
46 | ||
47 | private static final Logger LOG = LoggerFactory.getLogger(AbstractSpringSendGridConfigurer.class); | |
48 | | |
49 | protected final OghamSendGridProperties properties; | |
50 | protected final SendGridProperties springProperties; | |
51 | protected final SendGrid sendGrid; | |
52 | ||
53 | public AbstractSpringSendGridConfigurer(OghamSendGridProperties properties, SendGridProperties springProperties, SendGrid sendGrid) { | |
54 | super(); | |
55 | this.properties = properties; | |
56 | this.springProperties = springProperties; | |
57 | this.sendGrid = sendGrid; | |
58 | } | |
59 | ||
60 | @Override | |
61 | public void configure(MessagingBuilder builder) { | |
62 | LOG.debug("[{}] apply configuration", this); | |
63 | // Ogham specific properties take precedence over Spring properties if specified | |
64 |
2
1. configure : negated conditional → KILLED 2. configure : negated conditional → KILLED |
if (springProperties != null) { |
65 |
2
1. configure : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::applySpringConfiguration → TIMED_OUT 2. configure : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::applySpringConfiguration → KILLED |
applySpringConfiguration(builder); |
66 | } | |
67 |
2
1. configure : negated conditional → TIMED_OUT 2. configure : negated conditional → KILLED |
if (properties != null) { |
68 |
2
1. configure : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::applyOghamConfiguration → KILLED 2. configure : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::applyOghamConfiguration → KILLED |
applyOghamConfiguration(builder); |
69 | } | |
70 | } | |
71 | ||
72 | @SuppressWarnings("java:S1452") | |
73 | protected abstract Class<? extends AbstractSendGridBuilder<?, ?>> getSendGridBuilderClass(); | |
74 | ||
75 | @SuppressWarnings("deprecation") | |
76 | protected void applyOghamConfiguration(MessagingBuilder builder) { | |
77 | LOG.debug("[{}] apply ogham configuration properties to {}", this, builder); | |
78 | // @formatter:off | |
79 | builder.email() | |
80 | .sender(getSendGridBuilderClass()) | |
81 | .apiKey().value(ofNullable(properties.getApiKey())).and() | |
82 | .url().value(ofNullable(properties.getUrl())).and() | |
83 | .username().value(ofNullable(properties.getUsername())).and() | |
84 | .password().value(ofNullable(properties.getPassword())); | |
85 | // @formatter:on | |
86 | } | |
87 | ||
88 | protected void applySpringConfiguration(MessagingBuilder builder) { | |
89 | LOG.debug("[{}] apply spring configuration properties to {}", this, builder); | |
90 |
2
1. applySpringConfiguration : negated conditional → KILLED 2. applySpringConfiguration : negated conditional → KILLED |
if (sendGrid != null) { |
91 |
2
1. applySpringConfiguration : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::useSpringSendGridClient → KILLED 2. applySpringConfiguration : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::useSpringSendGridClient → KILLED |
useSpringSendGridClient(builder); |
92 | } else { | |
93 | // This is a special case that is not really reachable with Spring Boot: | |
94 | // - it means that SendGridProperties bean is available | |
95 | // - but SendGrid bean is not | |
96 | // As both beans are created by SendGridAutoConfiguration, either none is available or both are available. | |
97 | // We keep it in the case where a developer would use a @Configuration class with only @EnableConfigurationProperties(SendGridProperties.class) | |
98 |
2
1. applySpringConfiguration : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::useOghamSendGridClientWithSpringProperties → KILLED 2. applySpringConfiguration : removed call to fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::useOghamSendGridClientWithSpringProperties → KILLED |
useOghamSendGridClientWithSpringProperties(builder); |
99 | } | |
100 | } | |
101 | ||
102 | protected abstract void useSpringSendGridClient(MessagingBuilder builder); | |
103 | ||
104 | protected void useOghamSendGridClientWithSpringProperties(MessagingBuilder builder) { | |
105 | // @formatter:off | |
106 | builder.email() | |
107 | .sender(getSendGridBuilderClass()) | |
108 | .apiKey().value(ofNullable(springProperties.getApiKey())); | |
109 | // @formatter:on | |
110 | } | |
111 | ||
112 | @Override | |
113 | public int getOrder() { | |
114 |
1
1. getOrder : replaced int return with 0 for fr/sii/ogham/spring/email/AbstractSpringSendGridConfigurer::getOrder → SURVIVED |
return SendGridConstants.DEFAULT_SENDGRID_CONFIGURER_PRIORITY + SPRING_CONFIGURER_PRIORITY_OFFSET; |
115 | } | |
116 | ||
117 | } | |
Mutations | ||
64 |
1.1 2.2 |
|
65 |
1.1 2.2 |
|
67 |
1.1 2.2 |
|
68 |
1.1 2.2 |
|
90 |
1.1 2.2 |
|
91 |
1.1 2.2 |
|
98 |
1.1 2.2 |
|
114 |
1.1 |