| 1 | package fr.sii.ogham.email.sender.impl.javamail; | |
| 2 | ||
| 3 | import java.io.FileInputStream; | |
| 4 | import java.io.FileNotFoundException; | |
| 5 | import java.io.IOException; | |
| 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.FileResource; | |
| 15 | import fr.sii.ogham.core.resource.NamedResource; | |
| 16 | import fr.sii.ogham.email.attachment.Attachment; | |
| 17 | import fr.sii.ogham.email.exception.handler.AttachmentResourceHandlerException; | |
| 18 | ||
| 19 | /** | |
| 20 | * Specific implementation for files so that mimetype detection may also use the | |
| 21 | * file name to guess the mimetype. | |
| 22 | * | |
| 23 | * @author Aurélien Baudet | |
| 24 | * | |
| 25 | */ | |
| 26 | public class FileResourceHandler implements JavaMailAttachmentResourceHandler { | |
| 27 | private static final String ERROR_MESSAGE_PREFIX = "Failed to attach '"; | |
| 28 | ||
| 29 | /** | |
| 30 | * The Mime Type detector | |
| 31 | */ | |
| 32 | private MimeTypeProvider mimetypeProvider; | |
| 33 | ||
| 34 | public FileResourceHandler(MimeTypeProvider mimetypeProvider) { | |
| 35 | super(); | |
| 36 | this.mimetypeProvider = mimetypeProvider; | |
| 37 | } | |
| 38 | ||
| 39 | @Override | |
| 40 | public void setData(BodyPart part, NamedResource resource, Attachment attachment) throws AttachmentResourceHandlerException { | |
| 41 | FileResource fileResource = (FileResource) resource; | |
| 42 | try (FileInputStream fis = new FileInputStream(fileResource.getFile())) { | |
| 43 |
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(fis, getMimetype(attachment, fileResource)))); |
| 44 | } catch (MimeTypeDetectionException e) { | |
| 45 | throw new AttachmentResourceHandlerException(ERROR_MESSAGE_PREFIX + resource.getName() + "'. Mime type can't be detected", attachment, e); | |
| 46 | } catch (FileNotFoundException e) { | |
| 47 | throw new AttachmentResourceHandlerException(ERROR_MESSAGE_PREFIX + resource.getName() + "'. File doesn't exists", attachment, e); | |
| 48 | } catch (MessagingException e) { | |
| 49 | throw new AttachmentResourceHandlerException(ERROR_MESSAGE_PREFIX + resource.getName() + "'", attachment, e); | |
| 50 | } catch (IOException e) { | |
| 51 | throw new AttachmentResourceHandlerException(ERROR_MESSAGE_PREFIX + resource.getName() + "'. File can't be read", attachment, e); | |
| 52 | } | |
| 53 | } | |
| 54 | ||
| 55 | private String getMimetype(Attachment attachment, FileResource fileResource) throws MimeTypeDetectionException { | |
| 56 |
3
1. getMimetype : negated conditional → NO_COVERAGE 2. getMimetype : negated conditional → KILLED 3. getMimetype : negated conditional → KILLED |
if (attachment.getContentType() != null) { |
| 57 |
2
1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → NO_COVERAGE 2. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → KILLED |
return attachment.getContentType(); |
| 58 | } | |
| 59 |
3
1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → NO_COVERAGE 2. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → KILLED 3. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → KILLED |
return mimetypeProvider.getMimeType(fileResource.getFile()).toString(); |
| 60 | } | |
| 61 | ||
| 62 | } | |
Mutations | ||
| 43 |
1.1 2.2 3.3 |
|
| 56 |
1.1 2.2 3.3 |
|
| 57 |
1.1 2.2 |
|
| 59 |
1.1 2.2 3.3 |