1 | package fr.sii.ogham.email.sendgrid.builder; | |
2 | ||
3 | import java.io.InputStream; | |
4 | import java.net.MalformedURLException; | |
5 | import java.net.URL; | |
6 | import java.nio.file.Files; | |
7 | ||
8 | import javax.activation.MimetypesFileTypeMap; | |
9 | ||
10 | import org.apache.http.client.HttpClient; | |
11 | import org.apache.http.impl.client.CloseableHttpClient; | |
12 | ||
13 | import com.sendgrid.SendGrid; | |
14 | ||
15 | import fr.sii.ogham.core.builder.Builder; | |
16 | import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilder; | |
17 | import fr.sii.ogham.core.builder.configuration.ConfigurationValueBuilderHelper; | |
18 | import fr.sii.ogham.core.builder.configurer.Configurer; | |
19 | import fr.sii.ogham.core.builder.context.BuildContext; | |
20 | import fr.sii.ogham.core.builder.mimetype.MimetypeDetectionBuilder; | |
21 | import fr.sii.ogham.core.builder.mimetype.MimetypeDetectionBuilderDelegate; | |
22 | import fr.sii.ogham.core.builder.mimetype.SimpleMimetypeDetectionBuilder; | |
23 | import fr.sii.ogham.core.fluent.AbstractParent; | |
24 | import fr.sii.ogham.email.sendgrid.sender.SendGridSender; | |
25 | ||
26 | @SuppressWarnings("squid:S00119") | |
27 | public abstract class AbstractSendGridBuilder<MYSELF extends AbstractSendGridBuilder<MYSELF, EmailBuilder>, EmailBuilder> extends AbstractParent<EmailBuilder> implements Builder<SendGridSender> { | |
28 | protected final MYSELF myself; | |
29 | protected final BuildContext buildContext; | |
30 | protected MimetypeDetectionBuilder<MYSELF> mimetypeBuilder; | |
31 | protected final ConfigurationValueBuilderHelper<MYSELF, String> apiKeyValueBuilder; | |
32 | protected final ConfigurationValueBuilderHelper<MYSELF, URL> urlValueBuilder; | |
33 | protected CloseableHttpClient httpClient; | |
34 | ||
35 | @SuppressWarnings("unchecked") | |
36 | protected AbstractSendGridBuilder(Class<?> selfType, EmailBuilder parent, BuildContext buildContext, MimetypeDetectionBuilder<?> mimetypeBuilder) { | |
37 | super(parent); | |
38 | myself = (MYSELF) selfType.cast(this); | |
39 | this.buildContext = buildContext; | |
40 | apiKeyValueBuilder = buildContext.newConfigurationValueBuilder(myself, String.class); | |
41 | urlValueBuilder = buildContext.newConfigurationValueBuilder(myself, URL.class); | |
42 |
1
1. <init> : negated conditional → SURVIVED |
if (mimetypeBuilder != null) { |
43 | mimetype(mimetypeBuilder); | |
44 | } | |
45 | } | |
46 | ||
47 | public AbstractSendGridBuilder(Class<?> selfType, EmailBuilder parent, BuildContext buildContext) { | |
48 | this(selfType, parent, buildContext, null); | |
49 | mimetype(); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Set SendGrid <a href= | |
54 | * "https://sendgrid.com/docs/Classroom/Send/How_Emails_Are_Sent/api_keys.html">API | |
55 | * key</a>. | |
56 | * | |
57 | * <p> | |
58 | * The value set using this method takes precedence over any property and | |
59 | * default value configured using {@link #apiKey()}. | |
60 | * | |
61 | * <pre> | |
62 | * .apiKey("my-key") | |
63 | * .apiKey() | |
64 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
65 | * .defaultValue("default-key") | |
66 | * </pre> | |
67 | * | |
68 | * <pre> | |
69 | * .apiKey("my-key") | |
70 | * .apiKey() | |
71 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
72 | * .defaultValue("default-key") | |
73 | * </pre> | |
74 | * | |
75 | * In both cases, {@code apiKey("my-key")} is used. | |
76 | * | |
77 | * <p> | |
78 | * If this method is called several times, only the last value is used. | |
79 | * | |
80 | * <p> | |
81 | * If {@code null} value is set, it is like not setting a value at all. The | |
82 | * property/default value configuration is applied. | |
83 | * | |
84 | * @param apiKey | |
85 | * the API key to use | |
86 | * @return this instance for fluent chaining | |
87 | */ | |
88 | public MYSELF apiKey(String apiKey) { | |
89 |
2
1. apiKey : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE 2. apiKey : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → KILLED |
apiKeyValueBuilder.setValue(apiKey); |
90 |
2
1. apiKey : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::apiKey → NO_COVERAGE 2. apiKey : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::apiKey → SURVIVED |
return myself; |
91 | } | |
92 | ||
93 | /** | |
94 | * Set SendGrid <a href= | |
95 | * "https://sendgrid.com/docs/Classroom/Send/How_Emails_Are_Sent/api_keys.html">API | |
96 | * key</a>. | |
97 | * | |
98 | * <p> | |
99 | * This method is mainly used by {@link Configurer}s to register some | |
100 | * property keys and/or a default value. The aim is to let developer be able | |
101 | * to externalize its configuration (using system properties, configuration | |
102 | * file or anything else). If the developer doesn't configure any value for | |
103 | * the registered properties, the default value is used (if set). | |
104 | * | |
105 | * <pre> | |
106 | * .apiKey() | |
107 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
108 | * .defaultValue("default-key") | |
109 | * </pre> | |
110 | * | |
111 | * <p> | |
112 | * Non-null value set using {@link #apiKey(String)} takes precedence over | |
113 | * property values and default value. | |
114 | * | |
115 | * <pre> | |
116 | * .apiKey("my-key") | |
117 | * .apiKey() | |
118 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
119 | * .defaultValue("default-key") | |
120 | * </pre> | |
121 | * | |
122 | * The value {@code "my-key"} is used regardless of the value of the | |
123 | * properties and default value. | |
124 | * | |
125 | * <p> | |
126 | * See {@link ConfigurationValueBuilder} for more information. | |
127 | * | |
128 | * | |
129 | * @return the builder to configure property keys/default value | |
130 | */ | |
131 | public ConfigurationValueBuilder<MYSELF, String> apiKey() { | |
132 |
3
1. apiKey : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::apiKey → KILLED 2. apiKey : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::apiKey → KILLED 3. apiKey : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::apiKey → KILLED |
return apiKeyValueBuilder; |
133 | } | |
134 | ||
135 | /** | |
136 | * Set username for SendGrid HTTP API. | |
137 | * | |
138 | * <p> | |
139 | * <strong>WARNING:</strong> SendGrid v4 doesn't use username/password | |
140 | * anymore. You must use an {@link #apiKey(String)}. | |
141 | * | |
142 | * <p> | |
143 | * The value set using this method takes precedence over any property and | |
144 | * default value configured using {@link #username()}. | |
145 | * | |
146 | * <pre> | |
147 | * .username("my-username") | |
148 | * .username() | |
149 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
150 | * .defaultValue("default-username") | |
151 | * </pre> | |
152 | * | |
153 | * <pre> | |
154 | * .username("my-username") | |
155 | * .username() | |
156 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
157 | * .defaultValue("default-username") | |
158 | * </pre> | |
159 | * | |
160 | * In both cases, {@code username("my-username")} is used. | |
161 | * | |
162 | * <p> | |
163 | * If this method is called several times, only the last value is used. | |
164 | * | |
165 | * <p> | |
166 | * If {@code null} value is set, it is like not setting a value at all. The | |
167 | * property/default value configuration is applied. | |
168 | * | |
169 | * @param username | |
170 | * the user name for SendGrid HTTP API | |
171 | * @return this instance for fluent chaining | |
172 | * | |
173 | */ | |
174 | public abstract MYSELF username(String username); | |
175 | ||
176 | /** | |
177 | * Set username for SendGrid HTTP API. | |
178 | * | |
179 | * <p> | |
180 | * <strong>WARNING:</strong> SendGrid v4 doesn't use username/password | |
181 | * anymore. You must use an {@link #apiKey(String)}. | |
182 | * | |
183 | * <p> | |
184 | * This method is mainly used by {@link Configurer}s to register some | |
185 | * property keys and/or a default value. The aim is to let developer be able | |
186 | * to externalize its configuration (using system properties, configuration | |
187 | * file or anything else). If the developer doesn't configure any value for | |
188 | * the registered properties, the default value is used (if set). | |
189 | * | |
190 | * <pre> | |
191 | * .username() | |
192 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
193 | * .defaultValue("default-username") | |
194 | * </pre> | |
195 | * | |
196 | * <p> | |
197 | * Non-null value set using {@link #username(String)} takes precedence over | |
198 | * property values and default value. | |
199 | * | |
200 | * <pre> | |
201 | * .username("my-username") | |
202 | * .username() | |
203 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
204 | * .defaultValue("default-username") | |
205 | * </pre> | |
206 | * | |
207 | * The value {@code "my-username"} is used regardless of the value of the | |
208 | * properties and default value. | |
209 | * | |
210 | * <p> | |
211 | * See {@link ConfigurationValueBuilder} for more information. | |
212 | * | |
213 | * @return the builder to configure property keys/default value | |
214 | */ | |
215 | public abstract ConfigurationValueBuilder<MYSELF, String> username(); | |
216 | ||
217 | /** | |
218 | * Set password for SendGrid HTTP API. | |
219 | * | |
220 | * <p> | |
221 | * <strong>WARNING:</strong> SendGrid v4 doesn't use username/password | |
222 | * anymore. You must use an {@link #apiKey(String)}. | |
223 | * | |
224 | * <p> | |
225 | * The value set using this method takes precedence over any property and | |
226 | * default value configured using {@link #password()}. | |
227 | * | |
228 | * <pre> | |
229 | * .password("my-password") | |
230 | * .password() | |
231 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
232 | * .defaultValue("default-password") | |
233 | * </pre> | |
234 | * | |
235 | * <pre> | |
236 | * .password("my-password") | |
237 | * .password() | |
238 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
239 | * .defaultValue("default-password") | |
240 | * </pre> | |
241 | * | |
242 | * In both cases, {@code password("my-password")} is used. | |
243 | * | |
244 | * <p> | |
245 | * If this method is called several times, only the last value is used. | |
246 | * | |
247 | * <p> | |
248 | * If {@code null} value is set, it is like not setting a value at all. The | |
249 | * property/default value configuration is applied. | |
250 | * | |
251 | * @param password | |
252 | * the password value | |
253 | * @return this instance for fluent chaining | |
254 | */ | |
255 | public abstract MYSELF password(String password); | |
256 | ||
257 | /** | |
258 | * Set password for SendGrid HTTP API. | |
259 | * | |
260 | * <p> | |
261 | * <strong>WARNING:</strong> SendGrid v4 doesn't use username/password | |
262 | * anymore. You must use an {@link #apiKey(String)}. | |
263 | * | |
264 | * <p> | |
265 | * This method is mainly used by {@link Configurer}s to register some | |
266 | * property keys and/or a default value. The aim is to let developer be able | |
267 | * to externalize its configuration (using system properties, configuration | |
268 | * file or anything else). If the developer doesn't configure any value for | |
269 | * the registered properties, the default value is used (if set). | |
270 | * | |
271 | * <pre> | |
272 | * .password() | |
273 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
274 | * .defaultValue("default-password") | |
275 | * </pre> | |
276 | * | |
277 | * <p> | |
278 | * Non-null value set using {@link #password(String)} takes precedence over | |
279 | * property values and default value. | |
280 | * | |
281 | * <pre> | |
282 | * .password("my-password") | |
283 | * .password() | |
284 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
285 | * .defaultValue("default-password") | |
286 | * </pre> | |
287 | * | |
288 | * The value {@code "my-password"} is used regardless of the value of the | |
289 | * properties and default value. | |
290 | * | |
291 | * <p> | |
292 | * See {@link ConfigurationValueBuilder} for more information. | |
293 | * | |
294 | * @return the builder to configure property keys/default value | |
295 | */ | |
296 | public abstract ConfigurationValueBuilder<MYSELF, String> password(); | |
297 | ||
298 | /** | |
299 | * Set SendGrid API base URL. | |
300 | * | |
301 | * <p> | |
302 | * The value set using this method takes precedence over any property and | |
303 | * default value configured using {@link #url()}. | |
304 | * | |
305 | * <pre> | |
306 | * .url("http://localhost/sendgrid") | |
307 | * .url() | |
308 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
309 | * .defaultValue("http://api.sendgrid.com") | |
310 | * </pre> | |
311 | * | |
312 | * <pre> | |
313 | * .url("http://localhost/sendgrid") | |
314 | * .url() | |
315 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
316 | * .defaultValue("http://api.sendgrid.com") | |
317 | * </pre> | |
318 | * | |
319 | * In both cases, {@code url("http://localhost/sendgrid")} is used. | |
320 | * | |
321 | * <p> | |
322 | * If this method is called several times, only the last value is used. | |
323 | * | |
324 | * <p> | |
325 | * If {@code null} value is set, it is like not setting a value at all. The | |
326 | * property/default value configuration is applied. | |
327 | * | |
328 | * @param url | |
329 | * the base URL for SendGrid HTTP API | |
330 | * @return this instance for fluent chaining | |
331 | */ | |
332 | public MYSELF url(URL url) { | |
333 |
1
1. url : removed call to fr/sii/ogham/core/builder/configuration/ConfigurationValueBuilderHelper::setValue → NO_COVERAGE |
urlValueBuilder.setValue(url); |
334 |
1
1. url : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::url → NO_COVERAGE |
return myself; |
335 | } | |
336 | ||
337 | /** | |
338 | * Set SendGrid API base URL. | |
339 | * | |
340 | * <p> | |
341 | * The value set using this method takes precedence over any property and | |
342 | * default value configured using {@link #url()}. | |
343 | * | |
344 | * <pre> | |
345 | * .url("http://localhost/sendgrid") | |
346 | * .url() | |
347 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
348 | * .defaultValue("http://api.sendgrid.com") | |
349 | * </pre> | |
350 | * | |
351 | * <pre> | |
352 | * .url("http://localhost/sendgrid") | |
353 | * .url() | |
354 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
355 | * .defaultValue("http://api.sendgrid.com") | |
356 | * </pre> | |
357 | * | |
358 | * In both cases, {@code url("http://localhost/sendgrid")} is used. | |
359 | * | |
360 | * <p> | |
361 | * If this method is called several times, only the last value is used. | |
362 | * | |
363 | * <p> | |
364 | * If {@code null} value is set, it is like not setting a value at all. The | |
365 | * property/default value configuration is applied. | |
366 | * | |
367 | * @param url | |
368 | * the base URL for SendGrid HTTP API | |
369 | * @return this instance for fluent chaining | |
370 | * @throws IllegalArgumentException | |
371 | * if URL is malformed | |
372 | */ | |
373 | public MYSELF url(String url) { | |
374 | try { | |
375 |
1
1. url : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::url → NO_COVERAGE |
return url(new URL(url)); |
376 | } catch (MalformedURLException e) { | |
377 | throw new IllegalArgumentException("Invalid URL " + url, e); | |
378 | } | |
379 | } | |
380 | ||
381 | /** | |
382 | * Set SendGrid API base URL. | |
383 | * | |
384 | * <p> | |
385 | * This method is mainly used by {@link Configurer}s to register some | |
386 | * property keys and/or a default value. The aim is to let developer be able | |
387 | * to externalize its configuration (using system properties, configuration | |
388 | * file or anything else). If the developer doesn't configure any value for | |
389 | * the registered properties, the default value is used (if set). | |
390 | * | |
391 | * <pre> | |
392 | * .url() | |
393 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
394 | * .defaultValue("http://api.sendgrid.com") | |
395 | * </pre> | |
396 | * | |
397 | * <p> | |
398 | * Non-null value set using {@link #url(URL)} takes precedence over property | |
399 | * values and default value. | |
400 | * | |
401 | * <pre> | |
402 | * .url("http://localhost/sendgrid") | |
403 | * .url() | |
404 | * .properties("${custom.property.high-priority}", "${custom.property.low-priority}") | |
405 | * .defaultValue("http://api.sendgrid.com") | |
406 | * </pre> | |
407 | * | |
408 | * The value {@code "http://localhost/sendgrid"} is used regardless of the | |
409 | * value of the properties and default value. | |
410 | * | |
411 | * <p> | |
412 | * See {@link ConfigurationValueBuilder} for more information. | |
413 | * | |
414 | * | |
415 | * @return the builder to configure property keys/default value | |
416 | */ | |
417 | public ConfigurationValueBuilder<MYSELF, URL> url() { | |
418 |
3
1. url : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::url → KILLED 2. url : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::url → KILLED 3. url : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::url → KILLED |
return urlValueBuilder; |
419 | } | |
420 | ||
421 | /** | |
422 | * By default, calling SendGrid HTTP API is done through the default | |
423 | * {@link SendGrid} implementation that uses default {@link HttpClient} | |
424 | * (calling {@code HttpClientBuilder.create().build()}). If you want to use | |
425 | * another HTTP client implementation, you can extend the | |
426 | * {@link CloseableHttpClient} class and provide it: | |
427 | * | |
428 | * <pre> | |
429 | * .client(new MyCustomHttpClient()) | |
430 | * </pre> | |
431 | * | |
432 | * @param httpClient | |
433 | * the custom implementation of {@link HttpClient} used to call | |
434 | * SendGrid HTTP API. SendGrid requires a | |
435 | * {@link CloseableHttpClient}. | |
436 | * @return this instance for fluent chaining | |
437 | */ | |
438 | public MYSELF httpClient(CloseableHttpClient httpClient) { | |
439 | this.httpClient = httpClient; | |
440 |
1
1. httpClient : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::httpClient → NO_COVERAGE |
return myself; |
441 | } | |
442 | ||
443 | /** | |
444 | * Builder that configures mimetype detection. | |
445 | * | |
446 | * There exists several implementations to provide the mimetype: | |
447 | * <ul> | |
448 | * <li>Using Java {@link MimetypesFileTypeMap}</li> | |
449 | * <li>Using Java 7 {@link Files#probeContentType(java.nio.file.Path)}</li> | |
450 | * <li>Using <a href="http://tika.apache.org/">Apache Tika</a></li> | |
451 | * <li>Using | |
452 | * <a href="https://github.com/arimus/jmimemagic">JMimeMagic</a></li> | |
453 | * </ul> | |
454 | * | |
455 | * <p> | |
456 | * Both implementations provided by Java are based on file extensions. This | |
457 | * can't be used in most cases as we often handle {@link InputStream}s. | |
458 | * </p> | |
459 | * | |
460 | * <p> | |
461 | * In previous version of Ogham, JMimeMagic was used and was working quite | |
462 | * well. Unfortunately, the library is no more maintained. | |
463 | * </p> | |
464 | * | |
465 | * <p> | |
466 | * You can configure how Tika will detect mimetype: | |
467 | * | |
468 | * <pre> | |
469 | * .mimetype() | |
470 | * .tika() | |
471 | * ... | |
472 | * </pre> | |
473 | * | |
474 | * <p> | |
475 | * This builder allows to use several providers. It will chain them until | |
476 | * one can find a valid mimetype. If none is found, you can explicitly | |
477 | * provide the default one: | |
478 | * | |
479 | * <pre> | |
480 | * .mimetype() | |
481 | * .defaultMimetype("text/html") | |
482 | * </pre> | |
483 | * | |
484 | * <p> | |
485 | * If no mimetype detector was previously defined, it creates a new one. | |
486 | * Then each time you call {@link #mimetype()}, the same instance is used. | |
487 | * </p> | |
488 | * | |
489 | * @return the builder to configure mimetype detection | |
490 | */ | |
491 | public MimetypeDetectionBuilder<MYSELF> mimetype() { | |
492 |
1
1. mimetype : negated conditional → SURVIVED |
if (mimetypeBuilder == null) { |
493 | mimetypeBuilder = new SimpleMimetypeDetectionBuilder<>(myself, buildContext); | |
494 | } | |
495 |
2
1. mimetype : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::mimetype → SURVIVED 2. mimetype : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::mimetype → TIMED_OUT |
return mimetypeBuilder; |
496 | } | |
497 | ||
498 | /** | |
499 | * NOTE: this is mostly for advance usage (when creating a custom module). | |
500 | * | |
501 | * Inherits mimetype configuration from another builder. This is useful for | |
502 | * configuring independently different parts of Ogham but keeping a whole | |
503 | * coherence. | |
504 | * | |
505 | * The same instance is shared meaning that all changes done here will also | |
506 | * impact the other builder. | |
507 | * | |
508 | * <p> | |
509 | * If a previous builder was defined (by calling {@link #mimetype()} for | |
510 | * example), the new builder will override it. | |
511 | * | |
512 | * @param builder | |
513 | * the builder to inherit | |
514 | * @return this instance for fluent chaining | |
515 | */ | |
516 | public MYSELF mimetype(MimetypeDetectionBuilder<?> builder) { | |
517 | mimetypeBuilder = new MimetypeDetectionBuilderDelegate<>(myself, builder); | |
518 |
1
1. mimetype : replaced return value with null for fr/sii/ogham/email/sendgrid/builder/AbstractSendGridBuilder::mimetype → SURVIVED |
return myself; |
519 | } | |
520 | ||
521 | } | |
Mutations | ||
42 |
1.1 |
|
89 |
1.1 2.2 |
|
90 |
1.1 2.2 |
|
132 |
1.1 2.2 3.3 |
|
333 |
1.1 |
|
334 |
1.1 |
|
375 |
1.1 |
|
418 |
1.1 2.2 3.3 |
|
440 |
1.1 |
|
492 |
1.1 |
|
495 |
1.1 2.2 |
|
518 |
1.1 |