StringContentHandler.java

1
package fr.sii.ogham.email.sender.impl.javamail;
2
3
import java.nio.charset.Charset;
4
5
import jakarta.mail.MessagingException;
6
import jakarta.mail.Multipart;
7
import jakarta.mail.internet.MimeBodyPart;
8
import jakarta.mail.internet.MimePart;
9
10
import fr.sii.ogham.core.charset.CharsetDetector;
11
import fr.sii.ogham.core.exception.mimetype.MimeTypeDetectionException;
12
import fr.sii.ogham.core.message.content.Content;
13
import fr.sii.ogham.core.message.content.MayHaveStringContent;
14
import fr.sii.ogham.core.mimetype.MimeTypeProvider;
15
import fr.sii.ogham.email.exception.handler.ContentHandlerException;
16
import fr.sii.ogham.email.message.Email;
17
18
/**
19
 * Content handler that adds string contents (HTML, text, ...). It needs to
20
 * detect Mime Type for indicating the type of the added content.
21
 * 
22
 * @author Aurélien Baudet
23
 *
24
 */
25
public class StringContentHandler implements JavaMailContentHandler {
26
	/**
27
	 * The Mime Type detector
28
	 */
29
	private MimeTypeProvider mimetypeProvider;
30
31
	/**
32
	 * The charset provider
33
	 */
34
	private CharsetDetector charsetProvider;
35
36
	public StringContentHandler(MimeTypeProvider mimetypeProvider, CharsetDetector charsetProvider) {
37
		super();
38
		this.mimetypeProvider = mimetypeProvider;
39
		this.charsetProvider = charsetProvider;
40
	}
41
42
	@Override
43
	public void setContent(MimePart message, Multipart multipart, Email email, Content content) throws ContentHandlerException {
44
		try {
45
			String strContent = ((MayHaveStringContent) content).asString();
46
			Charset charset = charsetProvider.detect(strContent);
47 1 1. setContent : negated conditional → RUN_ERROR
			String charsetParam = charset == null ? "" : (";charset=" + charset.name());
48
			String contentType = mimetypeProvider.detect(strContent).toString() + charsetParam;
49
			// add the part
50
			MimeBodyPart part = new MimeBodyPart();
51 1 1. setContent : removed call to jakarta/mail/internet/MimeBodyPart::setContent → RUN_ERROR
			part.setContent(strContent, contentType);
52 1 1. setContent : removed call to jakarta/mail/internet/MimeBodyPart::setHeader → RUN_ERROR
			part.setHeader("Content-Type", contentType);
53 1 1. setContent : removed call to jakarta/mail/Multipart::addBodyPart → RUN_ERROR
			multipart.addBodyPart(part);
54
		} catch (MessagingException e) {
55
			throw new ContentHandlerException("failed to set content on mime message", content, e);
56
		} catch (MimeTypeDetectionException e) {
57
			throw new ContentHandlerException("failed to determine mimetype for the content", content, e);
58
		}
59
	}
60
61
}

Mutations

47

1.1
Location : setContent
Killed by :
negated conditional → RUN_ERROR

51

1.1
Location : setContent
Killed by :
removed call to jakarta/mail/internet/MimeBodyPart::setContent → RUN_ERROR

52

1.1
Location : setContent
Killed by :
removed call to jakarta/mail/internet/MimeBodyPart::setHeader → RUN_ERROR

53

1.1
Location : setContent
Killed by :
removed call to jakarta/mail/Multipart::addBodyPart → RUN_ERROR

Active mutators

Tests examined


Report generated by PIT 1.13.1