FileResourceHandler.java

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 jakarta.activation.DataHandler;
8
import jakarta.mail.BodyPart;
9
import jakarta.mail.MessagingException;
10
import jakarta.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 1 1. setData : removed call to jakarta/mail/BodyPart::setDataHandler → NO_COVERAGE
			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 1 1. getMimetype : negated conditional → NO_COVERAGE
		if (attachment.getContentType() != null) {
57 1 1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → NO_COVERAGE
			return attachment.getContentType();
58
		}
59 1 1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → NO_COVERAGE
		return mimetypeProvider.getMimeType(fileResource.getFile()).toString();
60
	}
61
62
}

Mutations

43

1.1
Location : setData
Killed by :
removed call to jakarta/mail/BodyPart::setDataHandler → NO_COVERAGE

56

1.1
Location : getMimetype
Killed by :
negated conditional → NO_COVERAGE

57

1.1
Location : getMimetype
Killed by :
replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → NO_COVERAGE

59

1.1
Location : getMimetype
Killed by :
replaced return value with "" for fr/sii/ogham/email/sender/impl/javamail/FileResourceHandler::getMimetype → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.13.1