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