1 | package fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.handler; | |
2 | ||
3 | import org.slf4j.Logger; | |
4 | import org.slf4j.LoggerFactory; | |
5 | ||
6 | import fr.sii.ogham.core.exception.mimetype.MimeTypeDetectionException; | |
7 | import fr.sii.ogham.core.message.content.Content; | |
8 | import fr.sii.ogham.core.message.content.MayHaveStringContent; | |
9 | import fr.sii.ogham.core.message.content.StringContent; | |
10 | import fr.sii.ogham.core.mimetype.MimeTypeProvider; | |
11 | import fr.sii.ogham.email.exception.handler.ContentHandlerException; | |
12 | import fr.sii.ogham.email.message.Email; | |
13 | import fr.sii.ogham.email.sendgrid.v4.sender.impl.sendgrid.compat.MailCompat; | |
14 | ||
15 | /** | |
16 | * Content handler that puts plain text or HTML content into email to be sent | |
17 | * through SendGrid. MIME type detection is delegated to an instance of | |
18 | * {@link MimeTypeProvider}. | |
19 | */ | |
20 | public final class StringContentHandler implements SendGridContentHandler { | |
21 | ||
22 | private static final Logger LOG = LoggerFactory.getLogger(StringContentHandler.class); | |
23 | ||
24 | private final MimeTypeProvider mimeProvider; | |
25 | ||
26 | /** | |
27 | * Constructor. | |
28 | * | |
29 | * @param mimeProvider | |
30 | * an object in charge of determining the MIME type of the | |
31 | * messages to send | |
32 | */ | |
33 | public StringContentHandler(final MimeTypeProvider mimeProvider) { | |
34 |
4
1. <init> : negated conditional → NO_COVERAGE 2. <init> : negated conditional → TIMED_OUT 3. <init> : negated conditional → KILLED 4. <init> : negated conditional → KILLED |
if (mimeProvider == null) { |
35 | throw new IllegalArgumentException("[mimeProvider] cannot be null"); | |
36 | } | |
37 | ||
38 | this.mimeProvider = mimeProvider; | |
39 | } | |
40 | ||
41 | /** | |
42 | * Reads the content and adds it into the email. This method is expected to | |
43 | * update the content of the {@code email} parameter. | |
44 | * | |
45 | * While the method signature accepts any {@link Content} instance as | |
46 | * parameter, the method will fail if anything other than a | |
47 | * {@link StringContent} is provided. | |
48 | * | |
49 | * @param original | |
50 | * the original Ogham email | |
51 | * @param email | |
52 | * the email to put the content in | |
53 | * @param content | |
54 | * the unprocessed content | |
55 | * @throws ContentHandlerException | |
56 | * the handler is unable to add the content to the email | |
57 | * @throws IllegalArgumentException | |
58 | * the content provided is not of the right type | |
59 | */ | |
60 | @Override | |
61 | public void setContent(final Email original, final MailCompat email, final Content content) throws ContentHandlerException { | |
62 |
2
1. setContent : negated conditional → NO_COVERAGE 2. setContent : negated conditional → KILLED |
if (email == null) { |
63 | throw new IllegalArgumentException("[email] cannot be null"); | |
64 | } | |
65 |
2
1. setContent : negated conditional → NO_COVERAGE 2. setContent : negated conditional → KILLED |
if (content == null) { |
66 | throw new IllegalArgumentException("[content] cannot be null"); | |
67 | } | |
68 | ||
69 |
2
1. setContent : negated conditional → NO_COVERAGE 2. setContent : negated conditional → KILLED |
if (content instanceof MayHaveStringContent) { |
70 | final String contentStr = ((MayHaveStringContent) content).asString(); | |
71 | ||
72 | try { | |
73 | final String mime = mimeProvider.detect(contentStr).toString(); | |
74 | LOG.debug("Email content has detected type {}", mime); | |
75 | LOG.trace("content: {}", content); | |
76 |
2
1. setContent : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/StringContentHandler::setMimeContent → NO_COVERAGE 2. setContent : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/handler/StringContentHandler::setMimeContent → KILLED |
setMimeContent(email, contentStr, mime); |
77 | } catch (MimeTypeDetectionException e) { | |
78 | throw new ContentHandlerException("Unable to set the email content", content, e); | |
79 | } | |
80 | } else { | |
81 | throw new IllegalArgumentException("This instance can only work with MayHaveStringContent instances, but was passed " + content.getClass().getSimpleName()); | |
82 | } | |
83 | ||
84 | } | |
85 | ||
86 | private static void setMimeContent(final MailCompat email, final String contentStr, final String mime) { | |
87 |
2
1. setMimeContent : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/compat/MailCompat::addContent → NO_COVERAGE 2. setMimeContent : removed call to fr/sii/ogham/email/sendgrid/v4/sender/impl/sendgrid/compat/MailCompat::addContent → KILLED |
email.addContent(mime, contentStr); |
88 | } | |
89 | ||
90 | } | |
Mutations | ||
34 |
1.1 2.2 3.3 4.4 |
|
62 |
1.1 2.2 |
|
65 |
1.1 2.2 |
|
69 |
1.1 2.2 |
|
76 |
1.1 2.2 |
|
87 |
1.1 2.2 |