1 | package fr.sii.ogham.email.sendgrid.v2.builder.sendgrid; | |
2 | ||
3 | import java.net.URL; | |
4 | ||
5 | import org.slf4j.Logger; | |
6 | import org.slf4j.LoggerFactory; | |
7 | ||
8 | import com.sendgrid.SendGrid; | |
9 | ||
10 | import fr.sii.ogham.core.builder.MessagingBuilder; | |
11 | import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilder; | |
12 | import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilderHelper; | |
13 | import fr.sii.ogham.core.builder.context.BuildContext; | |
14 | import fr.sii.ogham.core.builder.context.DefaultBuildContext; | |
15 | import fr.sii.ogham.core.message.content.MayHaveStringContent; | |
16 | import fr.sii.ogham.core.message.content.MultiContent; | |
17 | import fr.sii.ogham.core.mimetype.MimeTypeProvider; | |
18 | import fr.sii.ogham.email.builder.EmailBuilder; | |
19 | import fr.sii.ogham.email.message.Email; | |
20 | import fr.sii.ogham.email.message.content.ContentWithAttachments; | |
21 | import fr.sii.ogham.email.sendgrid.builder.AbstractSendGridBuilder; | |
22 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.SendGridV2Sender; | |
23 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.client.DelegateSendGridClient; | |
24 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.client.SendGridClient; | |
25 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.client.SendGridInterceptor; | |
26 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.ContentWithAttachmentsHandler; | |
27 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.MultiContentHandler; | |
28 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.PriorizedContentHandler; | |
29 | import fr.sii.ogham.email.sendgrid.v2.sender.impl.sendgrid.handler.StringContentHandler; | |
30 | ||
31 | /** | |
32 | * Configures how SendGrid implementation will send {@link Email}s. | |
33 | * | |
34 | * This implementation uses SendGrid HTTP API. | |
35 | * | |
36 | * <p> | |
37 | * To send {@link Email} using SendGrid, you need to register this builder into | |
38 | * a {@link MessagingBuilder} like this: | |
39 | * | |
40 | * <pre> | |
41 | * <code> | |
42 | * MessagingBuilder msgBuilder = ... | |
43 | * msgBuilder.email() | |
44 | * .sender(SendGridV2Builder.class) // registers the builder and accesses to that builder for configuring it | |
45 | * </code> | |
46 | * </pre> | |
47 | * | |
48 | * Once the builder is registered, sending email through SendGrid requires | |
49 | * either an API key or a username/password pair. You can define it using: | |
50 | * | |
51 | * <pre> | |
52 | * <code> | |
53 | * msgBuilder.email() | |
54 | * .sender(SendGridV2Builder.class) // registers the builder and accesses to that builder for configuring it | |
55 | * .apiKey("foo") | |
56 | * </code> | |
57 | * </pre> | |
58 | * | |
59 | * Or you can also use property keys (using interpolation): | |
60 | * | |
61 | * <pre> | |
62 | * <code> | |
63 | * msgBuilder | |
64 | * .environment() | |
65 | * .properties() | |
66 | * .set("custom.property.for.api-key", "foo") | |
67 | * .and() | |
68 | * .and() | |
69 | * .email() | |
70 | * .sender(SendGridV2Builder.class) // registers the builder and accesses to that builder for configuring it | |
71 | * .apiKey("${custom.property.for.api-key}") | |
72 | * </code> | |
73 | * </pre> | |
74 | * | |
75 | * <p> | |
76 | * Finally, Ogham will transform general {@link Email} object into | |
77 | * {@link SendGrid}.Email object. This transformation will fit almost all use | |
78 | * cases but you may need to customize a part of the SendGrid message. Instead | |
79 | * of doing again the same work Ogham does, this builder allows you to intercept | |
80 | * the message to modify it just before sending it: | |
81 | * | |
82 | * <pre> | |
83 | * <code> | |
84 | * .sender(SendGridV2Builder.class) | |
85 | * .intercept(new MyCustomInterceptor()) | |
86 | * </code> | |
87 | * </pre> | |
88 | * | |
89 | * See {@link SendGridInterceptor} for more information. | |
90 | * | |
91 | * @author Aurélien Baudet | |
92 | * | |
93 | */ | |
94 | public class SendGridV2Builder extends AbstractSendGridBuilder<SendGridV2Builder, EmailBuilder> { | |
95 | private static final Logger LOG = LoggerFactory.getLogger(SendGridV2Builder.class); | |
96 | ||
97 | private final ConfigurationValueBuilderHelper<SendGridV2Builder, String> usernameValueBuilder; | |
98 | private final ConfigurationValueBuilderHelper<SendGridV2Builder, String> passwordValueBuilder; | |
99 | private SendGridClient client; | |
100 | private SendGridInterceptor interceptor; | |
101 | ||
102 | /** | |
103 | * Default constructor when using SendGrid sender without all Ogham work. | |
104 | * | |
105 | * <strong>WARNING: use is only if you know what you are doing !</strong> | |
106 | */ | |
107 | public SendGridV2Builder() { | |
108 | this(null, new DefaultBuildContext()); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Constructor that is called when using Ogham builder: | |
113 | * | |
114 | * <pre> | |
115 | * MessagingBuilder msgBuilder = ... | |
116 | * msgBuilder | |
117 | * .email() | |
118 | * .sender(SendGridV2Builder.class) | |
119 | * </pre> | |
120 | * | |
121 | * @param parent | |
122 | * the parent builder instance for fluent chaining | |
123 | * @param buildContext | |
124 | * for registering instances and property evaluation | |
125 | */ | |
126 | public SendGridV2Builder(EmailBuilder parent, BuildContext buildContext) { | |
127 | super(SendGridV2Builder.class, parent, buildContext); | |
128 | usernameValueBuilder = buildContext.newConfigurationValueBuilder(this, String.class); | |
129 | passwordValueBuilder = buildContext.newConfigurationValueBuilder(this, String.class); | |
130 | } | |
131 | ||
132 | @Override | |
133 | public SendGridV2Builder username(String username) { | |
134 |
3
1. username : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE 2. username : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → SURVIVED 3. username : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → KILLED |
usernameValueBuilder.setValue(username); |
135 |
3
1. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → NO_COVERAGE 2. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → SURVIVED 3. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → KILLED |
return this; |
136 | } | |
137 | ||
138 | @Override | |
139 | public ConfigurationValueBuilder<SendGridV2Builder, String> username() { | |
140 |
3
1. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → NO_COVERAGE 2. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → KILLED 3. username : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::username → KILLED |
return usernameValueBuilder; |
141 | } | |
142 | ||
143 | @Override | |
144 | public SendGridV2Builder password(String password) { | |
145 |
3
1. password : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE 2. password : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → SURVIVED 3. password : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → KILLED |
passwordValueBuilder.setValue(password); |
146 |
2
1. password : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → SURVIVED 2. password : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → NO_COVERAGE |
return this; |
147 | } | |
148 | ||
149 | @Override | |
150 | public ConfigurationValueBuilder<SendGridV2Builder, String> password() { | |
151 |
3
1. password : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → NO_COVERAGE 2. password : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → KILLED 3. password : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::password → KILLED |
return passwordValueBuilder; |
152 | } | |
153 | ||
154 | /** | |
155 | * By default, calling SendGrid HTTP API is done through the default | |
156 | * {@link SendGrid} implementation. If you want to use another client | |
157 | * implementation (creating your custom HTTP API caller for example), you | |
158 | * can implement the {@link SendGridClient} interface and provide it: | |
159 | * | |
160 | * <pre> | |
161 | * .client(new MyCustomHttpApiCaller()) | |
162 | * </pre> | |
163 | * | |
164 | * NOTE: if you provide your custom implementation, any defined properties | |
165 | * and values using {@link #apiKey(String)}, {@link #username(String)} or | |
166 | * {@link #password(String)} won't be used at all. You then have to handle | |
167 | * it by yourself. | |
168 | * | |
169 | * @param client | |
170 | * the custom client used to call SendGrid HTTP API | |
171 | * @return this instance for fluent chaining | |
172 | */ | |
173 | public SendGridV2Builder client(SendGridClient client) { | |
174 | this.client = client; | |
175 |
3
1. client : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::client → SURVIVED 2. client : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::client → NO_COVERAGE 3. client : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::client → KILLED |
return this; |
176 | } | |
177 | ||
178 | /** | |
179 | * Ogham will transform general {@link Email} object into | |
180 | * {@link SendGrid}.Email objects. This transformation will fit almost all | |
181 | * use cases but you may need to customize a part of the SendGrid message. | |
182 | * Instead of doing again the same work Ogham does, this builder allows you | |
183 | * to intercept the message to modify it just before sending it: | |
184 | * | |
185 | * <pre> | |
186 | * .sender(SendGridV2Builder.class) | |
187 | * .intercept(new MyCustomInterceptor()) | |
188 | * </pre> | |
189 | * | |
190 | * See {@link SendGridInterceptor} for more information. | |
191 | * | |
192 | * @param interceptor | |
193 | * the custom interceptor used to modify {@link SendGrid}.Email | |
194 | * @return this instance for fluent chaining | |
195 | */ | |
196 | public SendGridV2Builder intercept(SendGridInterceptor interceptor) { | |
197 | this.interceptor = interceptor; | |
198 |
1
1. intercept : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::intercept → NO_COVERAGE |
return this; |
199 | } | |
200 | ||
201 | @Override | |
202 | public SendGridV2Sender build() { | |
203 | String apiKey = apiKeyValueBuilder.getValue(); | |
204 | String username = usernameValueBuilder.getValue(); | |
205 | String password = passwordValueBuilder.getValue(); | |
206 | URL url = urlValueBuilder.getValue(); | |
207 | SendGridClient builtClient = buildClient(apiKey, username, password, url); | |
208 |
3
1. build : negated conditional → NO_COVERAGE 2. build : negated conditional → KILLED 3. build : negated conditional → KILLED |
if (builtClient == null) { |
209 | return null; | |
210 | } | |
211 | LOG.info("Sending email using SendGrid API is registered"); | |
212 | LOG.debug("SendGrid account: apiKey={}, username={}", apiKey, username); | |
213 |
3
1. build : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::build → NO_COVERAGE 2. build : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::build → KILLED 3. build : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::build → KILLED |
return buildContext.register(new SendGridV2Sender(builtClient, buildContentHandler(), interceptor)); |
214 | } | |
215 | ||
216 | private SendGridClient buildClient(String apiKey, String username, String password, URL url) { | |
217 |
3
1. buildClient : negated conditional → NO_COVERAGE 2. buildClient : negated conditional → KILLED 3. buildClient : negated conditional → KILLED |
if (client != null) { |
218 |
3
1. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → NO_COVERAGE 2. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → KILLED 3. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → KILLED |
return client; |
219 | } | |
220 |
8
1. buildClient : negated conditional → NO_COVERAGE 2. buildClient : negated conditional → SURVIVED 3. buildClient : negated conditional → NO_COVERAGE 4. buildClient : negated conditional → NO_COVERAGE 5. buildClient : negated conditional → KILLED 6. buildClient : negated conditional → KILLED 7. buildClient : negated conditional → KILLED 8. buildClient : negated conditional → KILLED |
if (apiKey != null || (username != null && password != null)) { |
221 |
3
1. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → NO_COVERAGE 2. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → KILLED 3. buildClient : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildClient → KILLED |
return buildContext.register(new DelegateSendGridClient(buildSendGrid(apiKey, username, password, url))); |
222 | } | |
223 | return null; | |
224 | } | |
225 | ||
226 | private SendGrid buildSendGrid(String apiKey, String username, String password, URL url) { | |
227 | SendGrid sendGrid = newSendGrid(apiKey, username, password); | |
228 |
3
1. buildSendGrid : negated conditional → NO_COVERAGE 2. buildSendGrid : negated conditional → KILLED 3. buildSendGrid : negated conditional → KILLED |
if (url != null) { |
229 | sendGrid.setUrl(url.toString()); | |
230 | } | |
231 |
3
1. buildSendGrid : negated conditional → SURVIVED 2. buildSendGrid : negated conditional → NO_COVERAGE 3. buildSendGrid : negated conditional → KILLED |
if (httpClient != null) { |
232 | sendGrid.setClient(httpClient); | |
233 | } | |
234 |
3
1. buildSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildSendGrid → NO_COVERAGE 2. buildSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildSendGrid → KILLED 3. buildSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildSendGrid → KILLED |
return sendGrid; |
235 | } | |
236 | ||
237 | private SendGrid newSendGrid(String apiKey, String username, String password) { | |
238 |
3
1. newSendGrid : negated conditional → SURVIVED 2. newSendGrid : negated conditional → NO_COVERAGE 3. newSendGrid : negated conditional → KILLED |
if (apiKey != null) { |
239 |
3
1. newSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → NO_COVERAGE 2. newSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → KILLED 3. newSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → KILLED |
return buildContext.register(new SendGrid(apiKey)); |
240 | } | |
241 |
2
1. newSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → NO_COVERAGE 2. newSendGrid : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::newSendGrid → KILLED |
return buildContext.register(new SendGrid(username, password)); |
242 | } | |
243 | ||
244 | private PriorizedContentHandler buildContentHandler() { | |
245 | MimeTypeProvider mimetypeProvider = mimetypeBuilder.build(); | |
246 | PriorizedContentHandler contentHandler = buildContext.register(new PriorizedContentHandler()); | |
247 |
3
1. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → NO_COVERAGE 2. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → SURVIVED 3. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → KILLED |
contentHandler.register(MultiContent.class, buildContext.register(new MultiContentHandler(contentHandler))); |
248 |
2
1. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → NO_COVERAGE 2. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → SURVIVED |
contentHandler.register(ContentWithAttachments.class, buildContext.register(new ContentWithAttachmentsHandler(contentHandler))); |
249 |
3
1. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → SURVIVED 2. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → NO_COVERAGE 3. buildContentHandler : removed call to fr/sii/ogham/email/sendgrid/v2/sender/impl/sendgrid/handler/PriorizedContentHandler::register → KILLED |
contentHandler.register(MayHaveStringContent.class, buildContext.register(new StringContentHandler(mimetypeProvider))); |
250 |
3
1. buildContentHandler : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildContentHandler → NO_COVERAGE 2. buildContentHandler : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildContentHandler → KILLED 3. buildContentHandler : replaced return value with null for fr/sii/ogham/email/sendgrid/v2/builder/sendgrid/SendGridV2Builder::buildContentHandler → KILLED |
return contentHandler; |
251 | } | |
252 | } | |
Mutations | ||
134 |
1.1 2.2 3.3 |
|
135 |
1.1 2.2 3.3 |
|
140 |
1.1 2.2 3.3 |
|
145 |
1.1 2.2 3.3 |
|
146 |
1.1 2.2 |
|
151 |
1.1 2.2 3.3 |
|
175 |
1.1 2.2 3.3 |
|
198 |
1.1 |
|
208 |
1.1 2.2 3.3 |
|
213 |
1.1 2.2 3.3 |
|
217 |
1.1 2.2 3.3 |
|
218 |
1.1 2.2 3.3 |
|
220 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
221 |
1.1 2.2 3.3 |
|
228 |
1.1 2.2 3.3 |
|
231 |
1.1 2.2 3.3 |
|
234 |
1.1 2.2 3.3 |
|
238 |
1.1 2.2 3.3 |
|
239 |
1.1 2.2 3.3 |
|
241 |
1.1 2.2 |
|
247 |
1.1 2.2 3.3 |
|
248 |
1.1 2.2 |
|
249 |
1.1 2.2 3.3 |
|
250 |
1.1 2.2 3.3 |