1 | package fr.sii.ogham.email.attachment; | |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | ||
7 | import fr.sii.ogham.core.resource.ByteResource; | |
8 | import fr.sii.ogham.core.resource.FileResource; | |
9 | import fr.sii.ogham.core.resource.LookupResource; | |
10 | import fr.sii.ogham.core.resource.NamedResource; | |
11 | import fr.sii.ogham.core.resource.path.ResourcePath; | |
12 | import fr.sii.ogham.core.resource.path.UnresolvedPath; | |
13 | import fr.sii.ogham.core.util.EqualsBuilder; | |
14 | import fr.sii.ogham.core.util.HashCodeBuilder; | |
15 | ||
16 | /** | |
17 | * Represents a content to attach to the email. Typically a file to join to the | |
18 | * email. An attachment is represented with the following information: | |
19 | * <ul> | |
20 | * <li>A content (the file to join to the email). See {@link NamedResource} for | |
21 | * more information.</li> | |
22 | * <li>A name (the name to display for the file). See {@link NamedResource} for | |
23 | * more information.</li> | |
24 | * <li>An optional description</li> | |
25 | * <li>An optional disposition ({@link ContentDisposition#ATTACHMENT} by | |
26 | * default)</li> | |
27 | * </ul> | |
28 | * | |
29 | * @author Aurélien Baudet | |
30 | * | |
31 | */ | |
32 | public class Attachment { | |
33 | /** | |
34 | * The resource used to access the content of the attachment | |
35 | */ | |
36 | private NamedResource resource; | |
37 | ||
38 | /** | |
39 | * The description for the attachment | |
40 | */ | |
41 | private String description; | |
42 | ||
43 | /** | |
44 | * How to attach the attachment to the email | |
45 | */ | |
46 | private String disposition; | |
47 | ||
48 | /** | |
49 | * The ID of the content | |
50 | */ | |
51 | private String contentId; | |
52 | ||
53 | /** | |
54 | * The content-type of the attachment (automatically guessed by default) | |
55 | */ | |
56 | private String contentType; | |
57 | ||
58 | /** | |
59 | * Initialize the attachment with the provided resource (content and name), | |
60 | * the description of the attachment and the disposition (how to include the | |
61 | * attachment into the mail). | |
62 | * | |
63 | * @param resource | |
64 | * the resource of the attachment (with name) | |
65 | * @param description | |
66 | * the description of the attachment (may be null) | |
67 | * @param disposition | |
68 | * the disposition of the attachment | |
69 | * @param contentId | |
70 | * the unique id of the content (may not null) | |
71 | */ | |
72 | public Attachment(NamedResource resource, String description, String disposition, String contentId) { | |
73 | super(); | |
74 | this.resource = resource; | |
75 | this.description = description; | |
76 | this.disposition = disposition; | |
77 | this.contentId = contentId; | |
78 | } | |
79 | ||
80 | /** | |
81 | * Initialize the attachment with the provided resource (content and name), | |
82 | * the description of the attachment and the disposition (how to include the | |
83 | * attachment into the mail). | |
84 | * | |
85 | * @param resource | |
86 | * the resource of the attachment (with name) | |
87 | * @param description | |
88 | * the description of the attachment (may be null) | |
89 | * @param disposition | |
90 | * the disposition of the attachment | |
91 | */ | |
92 | public Attachment(NamedResource resource, String description, String disposition) { | |
93 | this(resource, description, disposition, null); | |
94 | } | |
95 | ||
96 | /** | |
97 | * <p> | |
98 | * Initialize the attachment with the provided resource (content and name), | |
99 | * the description of the attachment. | |
100 | * </p> | |
101 | * <p> | |
102 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
103 | * </p> | |
104 | * | |
105 | * @param resource | |
106 | * the resource of the attachment | |
107 | * @param description | |
108 | * the description of the attachment (may be null) | |
109 | */ | |
110 | public Attachment(NamedResource resource, String description) { | |
111 | this(resource, description, ContentDisposition.ATTACHMENT); | |
112 | } | |
113 | ||
114 | /** | |
115 | * <p> | |
116 | * Initialize the attachment with the provided resource (content and name). | |
117 | * </p> | |
118 | * <p> | |
119 | * The description is not used (set to null) | |
120 | * </p> | |
121 | * <p> | |
122 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
123 | * </p> | |
124 | * | |
125 | * @param resource | |
126 | * the resource of the attachment | |
127 | */ | |
128 | public Attachment(NamedResource resource) { | |
129 | this(resource, null); | |
130 | } | |
131 | ||
132 | /** | |
133 | * <p> | |
134 | * Initialize the attachment with the provided path, description of the | |
135 | * attachment and disposition (how to include the attachment into the mail). | |
136 | * </p> | |
137 | * <p> | |
138 | * The path may contain a lookup. The lookup is case sensitive and must end | |
139 | * with a ':'. It must not contain another ':' character. | |
140 | * </p> | |
141 | * <p> | |
142 | * For example, a path could be "classpath:/email/hello.pdf". The lookup is | |
143 | * "classpath:". | |
144 | * </p> | |
145 | * | |
146 | * @param path | |
147 | * the path to the attachment | |
148 | * @param description | |
149 | * the description of the attachment (may be null) | |
150 | * @param disposition | |
151 | * the disposition of the attachment | |
152 | */ | |
153 | public Attachment(String path, String description, String disposition) { | |
154 | this(new UnresolvedPath(path), description, disposition); | |
155 | } | |
156 | ||
157 | /** | |
158 | * <p> | |
159 | * Initialize the attachment with the provided path and description of the | |
160 | * attachment. | |
161 | * </p> | |
162 | * <p> | |
163 | * The path may contain a lookup. The lookup is case sensitive and must end | |
164 | * with a ':'. It must not contain another ':' character. | |
165 | * </p> | |
166 | * <p> | |
167 | * For example, a path could be "classpath:/email/hello.pdf". The lookup is | |
168 | * "classpath:". | |
169 | * </p> | |
170 | * <p> | |
171 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
172 | * </p> | |
173 | * | |
174 | * @param path | |
175 | * the path to the attachment | |
176 | * @param description | |
177 | * the description of the attachment (may be null) | |
178 | */ | |
179 | public Attachment(String path, String description) { | |
180 | this(new UnresolvedPath(path), description); | |
181 | } | |
182 | ||
183 | /** | |
184 | * <p> | |
185 | * Initialize the attachment with the provided path. | |
186 | * </p> | |
187 | * <p> | |
188 | * The path may contain a lookup. The lookup is case sensitive and must end | |
189 | * with a ':'. It must not contain another ':' character. | |
190 | * </p> | |
191 | * <p> | |
192 | * For example, a path could be "classpath:/email/hello.pdf". The lookup is | |
193 | * "classpath:". | |
194 | * </p> | |
195 | * <p> | |
196 | * The description is not used (set to null) | |
197 | * </p> | |
198 | * <p> | |
199 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
200 | * </p> | |
201 | * | |
202 | * @param path | |
203 | * the path to the attachment | |
204 | */ | |
205 | public Attachment(String path) { | |
206 | this(new UnresolvedPath(path)); | |
207 | } | |
208 | ||
209 | /** | |
210 | * <p> | |
211 | * Initialize the attachment with the provided path, description of the | |
212 | * attachment and disposition (how to include the attachment into the mail). | |
213 | * </p> | |
214 | * <p> | |
215 | * The path may contain a lookup. The lookup is case sensitive and must end | |
216 | * with a ':'. It must not contain another ':' character. | |
217 | * </p> | |
218 | * <p> | |
219 | * For example, a path could be "classpath:/email/hello.pdf". The lookup is | |
220 | * "classpath:". | |
221 | * </p> | |
222 | * | |
223 | * @param path | |
224 | * the path to the attachment | |
225 | * @param description | |
226 | * the description of the attachment (may be null) | |
227 | * @param disposition | |
228 | * the disposition of the attachment | |
229 | */ | |
230 | public Attachment(ResourcePath path, String description, String disposition) { | |
231 | this(new LookupResource(path), description, disposition); | |
232 | } | |
233 | ||
234 | /** | |
235 | * <p> | |
236 | * Initialize the attachment with the provided path and description of the | |
237 | * attachment. | |
238 | * </p> | |
239 | * <p> | |
240 | * The path may contain a lookup. The lookup is case sensitive and must end | |
241 | * with a ':'. It must not contain another ':' character. | |
242 | * </p> | |
243 | * <p> | |
244 | * For example, a path could be "classpath:/email/hello.pdf". The lookup is | |
245 | * "classpath:". | |
246 | * </p> | |
247 | * <p> | |
248 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
249 | * </p> | |
250 | * | |
251 | * @param path | |
252 | * the path to the attachment | |
253 | * @param description | |
254 | * the description of the attachment (may be null) | |
255 | */ | |
256 | public Attachment(ResourcePath path, String description) { | |
257 | this(new LookupResource(path), description); | |
258 | } | |
259 | ||
260 | /** | |
261 | * <p> | |
262 | * Initialize the attachment with the provided path. | |
263 | * </p> | |
264 | * <p> | |
265 | * The path may contain a lookup. The lookup is case sensitive and must end | |
266 | * with a ':'. It must not contain another ':' character. | |
267 | * </p> | |
268 | * <p> | |
269 | * For example, a path could be "classpath:/email/hello.pdf". The lookup is | |
270 | * "classpath:". | |
271 | * </p> | |
272 | * <p> | |
273 | * The description is not used (set to null) | |
274 | * </p> | |
275 | * <p> | |
276 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
277 | * </p> | |
278 | * | |
279 | * @param path | |
280 | * the path to the attachment | |
281 | */ | |
282 | public Attachment(ResourcePath path) { | |
283 | this(new LookupResource(path)); | |
284 | } | |
285 | ||
286 | /** | |
287 | * Initialize the attachment with the provided content, name, description of | |
288 | * the attachment and disposition (how to include the attachment into the | |
289 | * mail). | |
290 | * | |
291 | * @param name | |
292 | * the name of the attachment | |
293 | * @param stream | |
294 | * the content of the attachment accessible trough the stream | |
295 | * @param description | |
296 | * the description of the attachment (may be null) | |
297 | * @param disposition | |
298 | * the disposition of the attachment | |
299 | * @throws IOException | |
300 | * when the content of the stream can't be read | |
301 | */ | |
302 | public Attachment(String name, InputStream stream, String description, String disposition) throws IOException { | |
303 | this(new ByteResource(name, stream), description, disposition); | |
304 | } | |
305 | ||
306 | /** | |
307 | * <p> | |
308 | * Initialize the attachment with the provided content, name and description | |
309 | * of the attachment. | |
310 | * </p> | |
311 | * <p> | |
312 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
313 | * </p> | |
314 | * | |
315 | * @param name | |
316 | * the name of the attachment | |
317 | * @param stream | |
318 | * the content of the attachment accessible trough the stream | |
319 | * @param description | |
320 | * the description of the attachment (may be null) | |
321 | * @throws IOException | |
322 | * when the content of the stream can't be read | |
323 | */ | |
324 | public Attachment(String name, InputStream stream, String description) throws IOException { | |
325 | this(new ByteResource(name, stream), description); | |
326 | } | |
327 | ||
328 | /** | |
329 | * <p> | |
330 | * Initialize the attachment with the provided content and name. | |
331 | * </p> | |
332 | * <p> | |
333 | * The description is not used (set to null) | |
334 | * </p> | |
335 | * <p> | |
336 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
337 | * </p> | |
338 | * | |
339 | * @param name | |
340 | * the name of the attachment | |
341 | * @param stream | |
342 | * the content of the attachment accessible trough the stream | |
343 | * @throws IOException | |
344 | * when the content of the stream can't be read | |
345 | */ | |
346 | public Attachment(String name, InputStream stream) throws IOException { | |
347 | this(new ByteResource(name, stream)); | |
348 | } | |
349 | ||
350 | /** | |
351 | * Initialize the attachment with the provided content, name, description of | |
352 | * the attachment and disposition (how to include the attachment into the | |
353 | * mail). | |
354 | * | |
355 | * @param name | |
356 | * the name of the attachment | |
357 | * @param content | |
358 | * the content of the attachment | |
359 | * @param description | |
360 | * the description of the attachment (may be null) | |
361 | * @param disposition | |
362 | * the disposition of the attachment | |
363 | */ | |
364 | public Attachment(String name, byte[] content, String description, String disposition) { | |
365 | this(new ByteResource(name, content), description, disposition); | |
366 | } | |
367 | ||
368 | /** | |
369 | * <p> | |
370 | * Initialize the attachment with the provided content, name and description | |
371 | * of the attachment. | |
372 | * </p> | |
373 | * <p> | |
374 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
375 | * </p> | |
376 | * | |
377 | * @param name | |
378 | * the name of the attachment | |
379 | * @param content | |
380 | * the content of the attachment | |
381 | * @param description | |
382 | * the description of the attachment (may be null) | |
383 | */ | |
384 | public Attachment(String name, byte[] content, String description) { | |
385 | this(new ByteResource(name, content), description); | |
386 | } | |
387 | ||
388 | /** | |
389 | * <p> | |
390 | * Initialize the attachment with the provided content and name. | |
391 | * </p> | |
392 | * <p> | |
393 | * The description is not used (set to null) | |
394 | * </p> | |
395 | * <p> | |
396 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
397 | * </p> | |
398 | * | |
399 | * @param name | |
400 | * the name of the attachment | |
401 | * @param content | |
402 | * the content of the attachment | |
403 | */ | |
404 | public Attachment(String name, byte[] content) { | |
405 | this(new ByteResource(name, content)); | |
406 | } | |
407 | ||
408 | /** | |
409 | * Initialize the attachment with the provided file, description of the | |
410 | * attachment and disposition (how to include the attachment into the mail). | |
411 | * <p> | |
412 | * The name of the attachment is the name of the file. | |
413 | * </p> | |
414 | * | |
415 | * @param file | |
416 | * the file to attach | |
417 | * @param description | |
418 | * the description of the attachment (may be null) | |
419 | * @param disposition | |
420 | * the disposition of the attachment | |
421 | */ | |
422 | public Attachment(File file, String description, String disposition) { | |
423 | this(new FileResource(file), description, disposition); | |
424 | } | |
425 | ||
426 | /** | |
427 | * <p> | |
428 | * Initialize the attachment with the provided file and description of the | |
429 | * attachment. | |
430 | * </p> | |
431 | * <p> | |
432 | * The name of the attachment is the name of the file. | |
433 | * </p> | |
434 | * <p> | |
435 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
436 | * </p> | |
437 | * | |
438 | * @param file | |
439 | * the file to attach | |
440 | * @param description | |
441 | * the description of the attachment (may be null) | |
442 | */ | |
443 | public Attachment(File file, String description) { | |
444 | this(new FileResource(file), description); | |
445 | } | |
446 | ||
447 | /** | |
448 | * <p> | |
449 | * Initialize the attachment with the provided file. | |
450 | * </p> | |
451 | * <p> | |
452 | * The name of the attachment is the name of the file. | |
453 | * </p> | |
454 | * <p> | |
455 | * The description is not used (set to null) | |
456 | * </p> | |
457 | * <p> | |
458 | * The disposition is set to {@link ContentDisposition#ATTACHMENT} | |
459 | * </p> | |
460 | * | |
461 | * @param file | |
462 | * the file to attach | |
463 | */ | |
464 | public Attachment(File file) { | |
465 | this(new FileResource(file)); | |
466 | } | |
467 | ||
468 | public String getDescription() { | |
469 |
3
1. getDescription : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getDescription → NO_COVERAGE 2. getDescription : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getDescription → SURVIVED 3. getDescription : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getDescription → KILLED |
return description; |
470 | } | |
471 | ||
472 | public void setDescription(String description) { | |
473 | this.description = description; | |
474 | } | |
475 | ||
476 | public String getDisposition() { | |
477 |
4
1. getDisposition : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getDisposition → NO_COVERAGE 2. getDisposition : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getDisposition → KILLED 3. getDisposition : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getDisposition → KILLED 4. getDisposition : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getDisposition → KILLED |
return disposition; |
478 | } | |
479 | ||
480 | public void setDisposition(String disposition) { | |
481 | this.disposition = disposition; | |
482 | } | |
483 | ||
484 | public NamedResource getResource() { | |
485 |
4
1. getResource : replaced return value with null for fr/sii/ogham/email/attachment/Attachment::getResource → NO_COVERAGE 2. getResource : replaced return value with null for fr/sii/ogham/email/attachment/Attachment::getResource → KILLED 3. getResource : replaced return value with null for fr/sii/ogham/email/attachment/Attachment::getResource → KILLED 4. getResource : replaced return value with null for fr/sii/ogham/email/attachment/Attachment::getResource → KILLED |
return resource; |
486 | } | |
487 | ||
488 | public void setResource(NamedResource resource) { | |
489 | this.resource = resource; | |
490 | } | |
491 | ||
492 | public String getContentId() { | |
493 |
4
1. getContentId : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentId → SURVIVED 2. getContentId : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentId → NO_COVERAGE 3. getContentId : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentId → KILLED 4. getContentId : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentId → KILLED |
return contentId; |
494 | } | |
495 | ||
496 | public void setContentId(String contentId) { | |
497 | this.contentId = contentId; | |
498 | } | |
499 | ||
500 | public String getContentType() { | |
501 |
4
1. getContentType : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentType → NO_COVERAGE 2. getContentType : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentType → KILLED 3. getContentType : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentType → KILLED 4. getContentType : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::getContentType → KILLED |
return contentType; |
502 | } | |
503 | ||
504 | public void setContentType(String contentType) { | |
505 | this.contentType = contentType; | |
506 | } | |
507 | ||
508 | @Override | |
509 | public String toString() { | |
510 | StringBuilder builder = new StringBuilder(); | |
511 | builder.append("<").append(resource.getName()); | |
512 |
2
1. toString : negated conditional → NO_COVERAGE 2. toString : negated conditional → SURVIVED |
if (contentType != null) { |
513 | builder.append(" [").append(contentType).append("]"); | |
514 | } | |
515 |
2
1. toString : negated conditional → SURVIVED 2. toString : negated conditional → NO_COVERAGE |
if (description != null) { |
516 | builder.append("(").append(description).append(")"); | |
517 | } | |
518 |
2
1. toString : negated conditional → NO_COVERAGE 2. toString : negated conditional → SURVIVED |
if (contentId != null) { |
519 | builder.append("{").append(contentId).append("}>"); | |
520 | } | |
521 | builder.append("|").append(disposition).append("|>"); | |
522 |
2
1. toString : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::toString → SURVIVED 2. toString : replaced return value with "" for fr/sii/ogham/email/attachment/Attachment::toString → NO_COVERAGE |
return builder.toString(); |
523 | } | |
524 | ||
525 | @Override | |
526 | public int hashCode() { | |
527 |
3
1. hashCode : replaced int return with 0 for fr/sii/ogham/email/attachment/Attachment::hashCode → SURVIVED 2. hashCode : replaced int return with 0 for fr/sii/ogham/email/attachment/Attachment::hashCode → NO_COVERAGE 3. hashCode : replaced int return with 0 for fr/sii/ogham/email/attachment/Attachment::hashCode → KILLED |
return new HashCodeBuilder().append(resource, description, disposition, contentId, contentType).hashCode(); |
528 | } | |
529 | ||
530 | @Override | |
531 | public boolean equals(Object obj) { | |
532 |
6
1. equals : replaced boolean return with false for fr/sii/ogham/email/attachment/Attachment::equals → NO_COVERAGE 2. equals : replaced boolean return with true for fr/sii/ogham/email/attachment/Attachment::equals → SURVIVED 3. equals : replaced boolean return with true for fr/sii/ogham/email/attachment/Attachment::equals → NO_COVERAGE 4. equals : replaced boolean return with false for fr/sii/ogham/email/attachment/Attachment::equals → KILLED 5. equals : replaced boolean return with false for fr/sii/ogham/email/attachment/Attachment::equals → KILLED 6. equals : replaced boolean return with true for fr/sii/ogham/email/attachment/Attachment::equals → KILLED |
return new EqualsBuilder(this, obj).appendFields("resource", "description", "disposition", "contentId", "contentType").isEqual(); |
533 | } | |
534 | } | |
Mutations | ||
469 |
1.1 2.2 3.3 |
|
477 |
1.1 2.2 3.3 4.4 |
|
485 |
1.1 2.2 3.3 4.4 |
|
493 |
1.1 2.2 3.3 4.4 |
|
501 |
1.1 2.2 3.3 4.4 |
|
512 |
1.1 2.2 |
|
515 |
1.1 2.2 |
|
518 |
1.1 2.2 |
|
522 |
1.1 2.2 |
|
527 |
1.1 2.2 3.3 |
|
532 |
1.1 2.2 3.3 4.4 5.5 6.6 |