1 | package fr.sii.ogham.email.sender.impl.javamail; | |
2 | ||
3 | import java.io.ByteArrayInputStream; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | ||
7 | import javax.activation.DataHandler; | |
8 | import javax.mail.BodyPart; | |
9 | import javax.mail.MessagingException; | |
10 | import javax.mail.util.ByteArrayDataSource; | |
11 | ||
12 | import fr.sii.ogham.core.exception.mimetype.MimeTypeDetectionException; | |
13 | import fr.sii.ogham.core.mimetype.MimeTypeProvider; | |
14 | import fr.sii.ogham.core.resource.NamedResource; | |
15 | import fr.sii.ogham.core.resource.Resource; | |
16 | import fr.sii.ogham.core.util.IOUtils; | |
17 | import fr.sii.ogham.email.attachment.Attachment; | |
18 | import fr.sii.ogham.email.exception.handler.AttachmentResourceHandlerException; | |
19 | ||
20 | /** | |
21 | * Implementation that is able to handle any {@link Resource}. | |
22 | * | |
23 | * @author Aurélien Baudet | |
24 | * | |
25 | */ | |
26 | public class StreamResourceHandler implements JavaMailAttachmentResourceHandler { | |
27 | /** | |
28 | * The Mime Type detector | |
29 | */ | |
30 | private MimeTypeProvider mimetypeProvider; | |
31 | ||
32 | public StreamResourceHandler(MimeTypeProvider mimetypeProvider) { | |
33 | super(); | |
34 | this.mimetypeProvider = mimetypeProvider; | |
35 | } | |
36 | ||
37 | @Override | |
38 | @SuppressWarnings("squid:S1192") | |
39 | public void setData(BodyPart part, NamedResource resource, Attachment attachment) throws AttachmentResourceHandlerException { | |
40 | try (InputStream stream = resource.getInputStream()) { | |
41 | InputStream s = stream; | |
42 | // stream is read twice, if stream can't handle reset => create a | |
43 | // stream that is able to do it | |
44 |
2
1. setData : negated conditional → SURVIVED 2. setData : negated conditional → NO_COVERAGE |
if (!stream.markSupported()) { |
45 | s = new ByteArrayInputStream(IOUtils.toByteArray(stream)); | |
46 | } | |
47 | // mark to reset at the start of the stream | |
48 |
2
1. setData : removed call to java/io/InputStream::mark → SURVIVED 2. setData : removed call to java/io/InputStream::mark → NO_COVERAGE |
s.mark(Integer.MAX_VALUE); |
49 | // detect the mimetype | |
50 | String mimetype = getMimetype(attachment, s); | |
51 | // reset the stream | |
52 |
2
1. setData : removed call to java/io/InputStream::reset → NO_COVERAGE 2. setData : removed call to java/io/InputStream::reset → SURVIVED |
s.reset(); |
53 | // set the content | |
54 |
3
1. setData : removed call to javax/mail/BodyPart::setDataHandler → NO_COVERAGE 2. setData : removed call to javax/mail/BodyPart::setDataHandler → KILLED 3. setData : removed call to javax/mail/BodyPart::setDataHandler → KILLED |
part.setDataHandler(new DataHandler(new ByteArrayDataSource(s, mimetype))); |
55 | } catch (MimeTypeDetectionException e) { | |
56 | throw new AttachmentResourceHandlerException("Failed to attach " + resource.getName() + ". Mime type can't be detected", attachment, e); | |
57 | } catch (MessagingException e) { | |
58 | throw new AttachmentResourceHandlerException("Failed to attach " + resource.getName(), attachment, e); | |
59 | } catch (IOException e) { | |
60 | throw new AttachmentResourceHandlerException("Failed to attach " + resource.getName() + ". Stream can't be read", attachment, e); | |
61 | } | |
62 | } | |
63 | ||
64 | private String getMimetype(Attachment attachment, InputStream stream) throws MimeTypeDetectionException { | |
65 |
3
1. getMimetype : negated conditional → NO_COVERAGE 2. getMimetype : negated conditional → KILLED 3. getMimetype : negated conditional → KILLED |
if (attachment.getContentType() != null) { |
66 |
2
1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/StreamResourceHandler::getMimetype → NO_COVERAGE 2. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/StreamResourceHandler::getMimetype → KILLED |
return attachment.getContentType(); |
67 | } | |
68 |
3
1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/StreamResourceHandler::getMimetype → NO_COVERAGE 2. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/StreamResourceHandler::getMimetype → KILLED 3. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/StreamResourceHandler::getMimetype → KILLED |
return mimetypeProvider.detect(stream).toString(); |
69 | } | |
70 | ||
71 | } | |
Mutations | ||
44 |
1.1 2.2 |
|
48 |
1.1 2.2 |
|
52 |
1.1 2.2 |
|
54 |
1.1 2.2 3.3 |
|
65 |
1.1 2.2 3.3 |
|
66 |
1.1 2.2 |
|
68 |
1.1 2.2 3.3 |