StreamResourceHandler.java

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 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.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 1 1. 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 1 1. 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 1 1. setData : removed call to java/io/InputStream::reset → NO_COVERAGE
			s.reset();
53
			// set the content
54 1 1. setData : removed call to javax/mail/BodyPart::setDataHandler → NO_COVERAGE
			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 1 1. getMimetype : negated conditional → NO_COVERAGE
		if (attachment.getContentType() != null) {
66 1 1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javaxmail/StreamResourceHandler::getMimetype → NO_COVERAGE
			return attachment.getContentType();
67
		}
68 1 1. getMimetype : replaced return value with "" for fr/sii/ogham/email/sender/impl/javaxmail/StreamResourceHandler::getMimetype → NO_COVERAGE
		return mimetypeProvider.detect(stream).toString();
69
	}
70
71
}

Mutations

44

1.1
Location : setData
Killed by :
negated conditional → NO_COVERAGE

48

1.1
Location : setData
Killed by :
removed call to java/io/InputStream::mark → NO_COVERAGE

52

1.1
Location : setData
Killed by :
removed call to java/io/InputStream::reset → NO_COVERAGE

54

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

65

1.1
Location : getMimetype
Killed by :
negated conditional → NO_COVERAGE

66

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

68

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

Active mutators

Tests examined


Report generated by PIT 1.13.1