1 | package fr.sii.ogham.email.message.fluent; | |
2 | ||
3 | import static fr.sii.ogham.email.attachment.ContentDisposition.INLINE; | |
4 | ||
5 | import java.io.File; | |
6 | import java.io.IOException; | |
7 | import java.io.InputStream; | |
8 | import java.nio.file.Path; | |
9 | import java.util.ArrayList; | |
10 | import java.util.List; | |
11 | ||
12 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
13 | import fr.sii.ogham.core.resource.path.UnresolvedPath; | |
14 | import fr.sii.ogham.email.attachment.Attachment; | |
15 | import fr.sii.ogham.email.attachment.ContentDisposition; | |
16 | import fr.sii.ogham.email.message.Email; | |
17 | ||
18 | /** | |
19 | * Fluent API to embed a file to the email (mainly used for images). The file | |
20 | * must be referenced in the email body using a | |
21 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID (or | |
22 | * CID)</a>. | |
23 | * | |
24 | * @author Aurélien Baudet | |
25 | * @since 3.0.0 | |
26 | */ | |
27 | public class EmbedBuilder { | |
28 | private final Email parent; | |
29 | private final List<Attachment> attachments; | |
30 | ||
31 | /** | |
32 | * Initializes with the parent to go back to. | |
33 | * | |
34 | * @param parent | |
35 | * the parent instance | |
36 | */ | |
37 | public EmbedBuilder(Email parent) { | |
38 | super(); | |
39 | this.parent = parent; | |
40 | this.attachments = new ArrayList<>(); | |
41 | } | |
42 | ||
43 | /** | |
44 | * Embed a file to the email. The file must be referenced in the email body | |
45 | * using a | |
46 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
47 | * (or CID)</a>. | |
48 | * | |
49 | * <p> | |
50 | * The disposition of the attachment is set to | |
51 | * {@link ContentDisposition#INLINE}. | |
52 | * | |
53 | * <p> | |
54 | * The Content-Type of the attachment is automatically determined. | |
55 | * | |
56 | * @param contentId | |
57 | * the Content-ID used to reference the embedded attachment in | |
58 | * the email body | |
59 | * @param file | |
60 | * the file to attach | |
61 | * @return the email instance for fluent chaining | |
62 | */ | |
63 | public Email file(String contentId, File file) { | |
64 |
2
1. file : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::file → NO_COVERAGE 2. file : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::file → KILLED |
return file(contentId, file, null); |
65 | } | |
66 | ||
67 | /** | |
68 | * Embed a file to the email. The file must be referenced in the email body | |
69 | * using a | |
70 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
71 | * (or CID)</a>. | |
72 | * | |
73 | * <p> | |
74 | * The disposition of the attachment is set to | |
75 | * {@link ContentDisposition#INLINE}. | |
76 | * | |
77 | * <p> | |
78 | * The Content-Type of the attachment is explicitly set. | |
79 | * | |
80 | * @param contentId | |
81 | * the Content-ID used to reference the embedded attachment in | |
82 | * the email body | |
83 | * @param file | |
84 | * the file to attach | |
85 | * @param contentType | |
86 | * the Content-Type of the attachment | |
87 | * @return the email instance for fluent chaining | |
88 | */ | |
89 | public Email file(String contentId, File file, String contentType) { | |
90 |
2
1. file : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → NO_COVERAGE 2. file : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → KILLED |
embed(new Attachment(file), contentId, contentType); |
91 |
2
1. file : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::file → NO_COVERAGE 2. file : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::file → KILLED |
return parent; |
92 | } | |
93 | ||
94 | /** | |
95 | * Embed a file to the email. The file must be referenced in the email body | |
96 | * using a | |
97 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
98 | * (or CID)</a>. | |
99 | * | |
100 | * <p> | |
101 | * The disposition of the attachment is set to | |
102 | * {@link ContentDisposition#INLINE}. | |
103 | * | |
104 | * <p> | |
105 | * The Content-Type of the attachment is automatically determined. | |
106 | * | |
107 | * @param contentId | |
108 | * the Content-ID used to reference the embedded attachment in | |
109 | * the email body | |
110 | * @param path | |
111 | * the path to the file to attach | |
112 | * @return the email instance for fluent chaining | |
113 | */ | |
114 | public Email file(String contentId, Path path) { | |
115 |
1
1. file : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::file → NO_COVERAGE |
return file(contentId, path, null); |
116 | } | |
117 | ||
118 | /** | |
119 | * Embed a file to the email. The file must be referenced in the email body | |
120 | * using a | |
121 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
122 | * (or CID)</a>. | |
123 | * | |
124 | * <p> | |
125 | * The disposition of the attachment is set to | |
126 | * {@link ContentDisposition#INLINE}. | |
127 | * | |
128 | * <p> | |
129 | * The Content-Type of the attachment is explicitly set. | |
130 | * | |
131 | * @param contentId | |
132 | * the Content-ID used to reference the embedded attachment in | |
133 | * the email body | |
134 | * @param path | |
135 | * the path to the file to attach | |
136 | * @param contentType | |
137 | * the Content-Type of the attachment | |
138 | * @return the email instance for fluent chaining | |
139 | */ | |
140 | public Email file(String contentId, Path path, String contentType) { | |
141 |
1
1. file : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::file → NO_COVERAGE |
return file(contentId, path.toFile(), contentType); |
142 | } | |
143 | ||
144 | /** | |
145 | * Embed a resource loaded from a path to the email. The resource must be | |
146 | * referenced in the email body using a | |
147 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
148 | * (or CID)</a>. | |
149 | * | |
150 | * <p> | |
151 | * The path may contain the lookup prefix (a prefix that indicates where to | |
152 | * find the resource). For example: | |
153 | * <ul> | |
154 | * <li><code>"classpath:/path/to/file.pdf"</code> references a resource that | |
155 | * is located at "path/to/file.pdf" in the classpath (from the root)</li> | |
156 | * <li><code>"file:/path/to/file.pdf"</code> references a resource that is | |
157 | * located at "/path/to/file.pdf" in the file system</li> | |
158 | * </ul> | |
159 | * By default, if the path doesn't contain any lookup prefix then the | |
160 | * resource is loaded from the root of the classpath. | |
161 | * | |
162 | * <p> | |
163 | * The disposition of the attachment is set to | |
164 | * {@link ContentDisposition#INLINE}. | |
165 | * | |
166 | * <p> | |
167 | * The Content-Type of the attachment is automatically determined. | |
168 | * | |
169 | * @param contentId | |
170 | * the Content-ID used to reference the embedded attachment in | |
171 | * the email body | |
172 | * @param path | |
173 | * the path to the resource to attach | |
174 | * @return the email instance for fluent chaining | |
175 | */ | |
176 | public Email resource(String contentId, String path) { | |
177 |
2
1. resource : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::resource → NO_COVERAGE 2. resource : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::resource → KILLED |
return resource(contentId, path, null); |
178 | } | |
179 | ||
180 | /** | |
181 | * Embed a resource loaded from a path to the email. The resource must be | |
182 | * referenced in the email body using a | |
183 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
184 | * (or CID)</a>. | |
185 | * | |
186 | * <p> | |
187 | * The path may contain the lookup prefix (a prefix that indicates where to | |
188 | * find the resource). For example: | |
189 | * <ul> | |
190 | * <li><code>"classpath:/path/to/file.pdf"</code> references a resource that | |
191 | * is located at "path/to/file.pdf" in the classpath (from the root)</li> | |
192 | * <li><code>"file:/path/to/file.pdf"</code> references a resource that is | |
193 | * located at "/path/to/file.pdf" in the file system</li> | |
194 | * </ul> | |
195 | * By default, if the path doesn't contain any lookup prefix then the | |
196 | * resource is loaded from the root of the classpath. | |
197 | * | |
198 | * <p> | |
199 | * The disposition of the attachment is set to | |
200 | * {@link ContentDisposition#INLINE}. | |
201 | * | |
202 | * <p> | |
203 | * The Content-Type of the attachment is explicitly set. | |
204 | * | |
205 | * @param contentId | |
206 | * the Content-ID used to reference the embedded attachment in | |
207 | * the email body | |
208 | * @param path | |
209 | * the path to the resource to attach | |
210 | * @param contentType | |
211 | * the Content-Type of the attachment | |
212 | * @return the email instance for fluent chaining | |
213 | */ | |
214 | public Email resource(String contentId, String path, String contentType) { | |
215 |
2
1. resource : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::resource → NO_COVERAGE 2. resource : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::resource → KILLED |
return resource(contentId, new UnresolvedPath(path), contentType); |
216 | } | |
217 | ||
218 | /** | |
219 | * Embed a resource loaded from a path to the email. The resource must be | |
220 | * referenced in the email body using a | |
221 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
222 | * (or CID)</a>. | |
223 | * | |
224 | * <p> | |
225 | * The path may contain the lookup prefix (a prefix that indicates where to | |
226 | * find the resource). For example: | |
227 | * <ul> | |
228 | * <li><code>"classpath:/path/to/file.pdf"</code> references a resource that | |
229 | * is located at "path/to/file.pdf" in the classpath (from the root)</li> | |
230 | * <li><code>"file:/path/to/file.pdf"</code> references a resource that is | |
231 | * located at "/path/to/file.pdf" in the file system</li> | |
232 | * </ul> | |
233 | * By default, if the path doesn't contain any lookup prefix then the | |
234 | * resource is loaded from the root of the classpath. | |
235 | * | |
236 | * <p> | |
237 | * The disposition of the attachment is set to | |
238 | * {@link ContentDisposition#INLINE}. | |
239 | * | |
240 | * <p> | |
241 | * The Content-Type of the attachment is automatically determined. | |
242 | * | |
243 | * @param contentId | |
244 | * the Content-ID used to reference the embedded attachment in | |
245 | * the email body | |
246 | * @param path | |
247 | * the path to the resource to attach | |
248 | * @return the email instance for fluent chaining | |
249 | */ | |
250 | public Email resource(String contentId, ResourcePath path) { | |
251 |
1
1. resource : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::resource → NO_COVERAGE |
return resource(contentId, path, null); |
252 | } | |
253 | ||
254 | /** | |
255 | * Embed a resource loaded from a path to the email. The resource must be | |
256 | * referenced in the email body using a | |
257 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
258 | * (or CID)</a>. | |
259 | * | |
260 | * <p> | |
261 | * The path may contain the lookup prefix (a prefix that indicates where to | |
262 | * find the resource). For example: | |
263 | * <ul> | |
264 | * <li><code>"classpath:/path/to/file.pdf"</code> references a resource that | |
265 | * is located at "path/to/file.pdf" in the classpath (from the root)</li> | |
266 | * <li><code>"file:/path/to/file.pdf"</code> references a resource that is | |
267 | * located at "/path/to/file.pdf" in the file system</li> | |
268 | * </ul> | |
269 | * By default, if the path doesn't contain any lookup prefix then the | |
270 | * resource is loaded from the root of the classpath. | |
271 | * | |
272 | * <p> | |
273 | * The disposition of the attachment is set to | |
274 | * {@link ContentDisposition#INLINE}. | |
275 | * | |
276 | * <p> | |
277 | * The Content-Type of the attachment is explicitly set. | |
278 | * | |
279 | * @param contentId | |
280 | * the Content-ID used to reference the embedded attachment in | |
281 | * the email body | |
282 | * @param path | |
283 | * the path to the resource to attach | |
284 | * @param contentType | |
285 | * the Content-Type of the attachment | |
286 | * @return the email instance for fluent chaining | |
287 | */ | |
288 | public Email resource(String contentId, ResourcePath path, String contentType) { | |
289 |
2
1. resource : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → NO_COVERAGE 2. resource : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → KILLED |
embed(new Attachment(path), contentId, contentType); |
290 |
2
1. resource : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::resource → NO_COVERAGE 2. resource : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::resource → KILLED |
return parent; |
291 | } | |
292 | ||
293 | /** | |
294 | * Embed a resource to the email directly using the bytes. The embedded | |
295 | * resource must be referenced in the email body using a | |
296 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
297 | * (or CID)</a>. | |
298 | * | |
299 | * <p> | |
300 | * The disposition of the attachment is set to | |
301 | * {@link ContentDisposition#INLINE}. | |
302 | * | |
303 | * <p> | |
304 | * The Content-Type of the attachment is automatically determined. | |
305 | * | |
306 | * @param contentId | |
307 | * the Content-ID used to reference the embedded attachment in | |
308 | * the email body | |
309 | * @param bytes | |
310 | * the content of the attachment | |
311 | * @return the email instance for fluent chaining | |
312 | */ | |
313 | public Email bytes(String contentId, byte[] bytes) { | |
314 |
3
1. bytes : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::bytes → NO_COVERAGE 2. bytes : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::bytes → KILLED 3. bytes : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::bytes → KILLED |
return bytes(contentId, bytes, null); |
315 | } | |
316 | ||
317 | /** | |
318 | * Embed a resource to the email directly using the bytes. The embedded | |
319 | * resource must be referenced in the email body using a | |
320 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
321 | * (or CID)</a>. | |
322 | * | |
323 | * <p> | |
324 | * The disposition of the attachment is set to | |
325 | * {@link ContentDisposition#INLINE}. | |
326 | * | |
327 | * <p> | |
328 | * The Content-Type of the attachment is explicitly set. | |
329 | * | |
330 | * @param contentId | |
331 | * the Content-ID used to reference the embedded attachment in | |
332 | * the email body | |
333 | * @param bytes | |
334 | * the content of the attachment | |
335 | * @param contentType | |
336 | * the Content-Type of the attachment | |
337 | * @return the email instance for fluent chaining | |
338 | */ | |
339 | public Email bytes(String contentId, byte[] bytes, String contentType) { | |
340 |
3
1. bytes : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → NO_COVERAGE 2. bytes : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → KILLED 3. bytes : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → KILLED |
embed(new Attachment(contentId, bytes), contentId, contentType); |
341 |
3
1. bytes : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::bytes → NO_COVERAGE 2. bytes : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::bytes → KILLED 3. bytes : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::bytes → KILLED |
return parent; |
342 | } | |
343 | ||
344 | /** | |
345 | * Attach a resource to the email directly using the bytes read from the | |
346 | * {@link InputStream}. The {@link InputStream} is immediately read and | |
347 | * converted to a byte array. The embedded | |
348 | * resource must be referenced in the email body using a | |
349 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
350 | * (or CID)</a>. | |
351 | * | |
352 | * <p> | |
353 | * <strong>IMPORTANT:</strong> You need to manually close the stream. | |
354 | * | |
355 | * <p> | |
356 | * The disposition of the attachment is set to | |
357 | * {@link ContentDisposition#INLINE}. | |
358 | * | |
359 | * <p> | |
360 | * The Content-Type of the attachment is automatically determined. | |
361 | * | |
362 | * @param contentId | |
363 | * the Content-ID used to reference the embedded attachment in | |
364 | * the email body | |
365 | * @param stream | |
366 | * the content of the attachment | |
367 | * @return the email instance for fluent chaining | |
368 | * @throws IOException | |
369 | * when the {@link InputStream} can't be read | |
370 | */ | |
371 | public Email stream(String contentId, InputStream stream) throws IOException { | |
372 |
2
1. stream : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::stream → NO_COVERAGE 2. stream : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::stream → KILLED |
return stream(contentId, stream, null); |
373 | } | |
374 | ||
375 | /** | |
376 | * Attach a resource to the email directly using the bytes read from the | |
377 | * {@link InputStream}. The {@link InputStream} is immediately read and | |
378 | * converted to a byte array. The embedded | |
379 | * resource must be referenced in the email body using a | |
380 | * <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID | |
381 | * (or CID)</a>. | |
382 | * | |
383 | * <p> | |
384 | * <strong>IMPORTANT:</strong> You need to manually close the stream. | |
385 | * | |
386 | * <p> | |
387 | * The disposition of the attachment is set to | |
388 | * {@link ContentDisposition#INLINE}. | |
389 | * | |
390 | * <p> | |
391 | * The Content-Type of the attachment is explicitly set. | |
392 | * | |
393 | * @param contentId | |
394 | * the Content-ID used to reference the embedded attachment in | |
395 | * the email body | |
396 | * @param stream | |
397 | * the content of the attachment | |
398 | * @param contentType | |
399 | * the Content-Type of the attachment | |
400 | * @return the email instance for fluent chaining | |
401 | * @throws IOException | |
402 | * when the {@link InputStream} can't be read | |
403 | */ | |
404 | public Email stream(String contentId, InputStream stream, String contentType) throws IOException { | |
405 |
2
1. stream : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → NO_COVERAGE 2. stream : removed call to fr/sii/ogham/email/message/fluent/EmbedBuilder::embed → KILLED |
embed(new Attachment(contentId, stream), contentId, contentType); |
406 |
2
1. stream : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::stream → NO_COVERAGE 2. stream : replaced return value with null for fr/sii/ogham/email/message/fluent/EmbedBuilder::stream → KILLED |
return parent; |
407 | } | |
408 | ||
409 | /** | |
410 | * Provides the list of attachments. | |
411 | * | |
412 | * @return the list of attachments | |
413 | */ | |
414 | public List<Attachment> build() { | |
415 |
5
1. build : replaced return value with Collections.emptyList for fr/sii/ogham/email/message/fluent/EmbedBuilder::build → SURVIVED 2. build : replaced return value with Collections.emptyList for fr/sii/ogham/email/message/fluent/EmbedBuilder::build → NO_COVERAGE 3. build : replaced return value with Collections.emptyList for fr/sii/ogham/email/message/fluent/EmbedBuilder::build → TIMED_OUT 4. build : replaced return value with Collections.emptyList for fr/sii/ogham/email/message/fluent/EmbedBuilder::build → KILLED 5. build : replaced return value with Collections.emptyList for fr/sii/ogham/email/message/fluent/EmbedBuilder::build → KILLED |
return attachments; |
416 | } | |
417 | ||
418 | private void embed(Attachment attachment, String contentId, String contentType) { | |
419 |
3
1. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setContentId → SURVIVED 2. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setContentId → NO_COVERAGE 3. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setContentId → KILLED |
attachment.setContentId(contentId); |
420 |
3
1. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setContentType → NO_COVERAGE 2. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setContentType → SURVIVED 3. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setContentType → KILLED |
attachment.setContentType(contentType); |
421 |
3
1. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setDisposition → NO_COVERAGE 2. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setDisposition → KILLED 3. embed : removed call to fr/sii/ogham/email/attachment/Attachment::setDisposition → KILLED |
attachment.setDisposition(INLINE); |
422 | attachments.add(attachment); | |
423 | } | |
424 | } | |
Mutations | ||
64 |
1.1 2.2 |
|
90 |
1.1 2.2 |
|
91 |
1.1 2.2 |
|
115 |
1.1 |
|
141 |
1.1 |
|
177 |
1.1 2.2 |
|
215 |
1.1 2.2 |
|
251 |
1.1 |
|
289 |
1.1 2.2 |
|
290 |
1.1 2.2 |
|
314 |
1.1 2.2 3.3 |
|
340 |
1.1 2.2 3.3 |
|
341 |
1.1 2.2 3.3 |
|
372 |
1.1 2.2 |
|
405 |
1.1 2.2 |
|
406 |
1.1 2.2 |
|
415 |
1.1 2.2 3.3 4.4 5.5 |
|
419 |
1.1 2.2 3.3 |
|
420 |
1.1 2.2 3.3 |
|
421 |
1.1 2.2 3.3 |