| 1 | package fr.sii.ogham.email.builder.javamail; | |
| 2 | ||
| 3 | import static fr.sii.ogham.core.builder.configuration.MayOverride.overrideIfNotSet; | |
| 4 | import static fr.sii.ogham.email.JavaMailConstants.DEFAULT_JAVAMAIL_CONFIGURER_PRIORITY; | |
| 5 | import static java.nio.charset.StandardCharsets.UTF_8; | |
| 6 | ||
| 7 | import org.slf4j.Logger; | |
| 8 | import org.slf4j.LoggerFactory; | |
| 9 | ||
| 10 | import fr.sii.ogham.core.builder.MessagingBuilder; | |
| 11 | import fr.sii.ogham.core.builder.configurer.ConfigurerFor; | |
| 12 | import fr.sii.ogham.core.builder.configurer.MessagingConfigurer; | |
| 13 | import fr.sii.ogham.core.builder.context.BuildContext; | |
| 14 | import fr.sii.ogham.core.util.ClasspathUtils; | |
| 15 | import fr.sii.ogham.email.sender.impl.javamail.UsernamePasswordAuthenticator; | |
| 16 | ||
| 17 | /** | |
| 18 | * Default JavaMail configurer that is automatically applied every time a | |
| 19 | * {@link MessagingBuilder} instance is created through | |
| 20 | * {@link MessagingBuilder#standard()}. | |
| 21 | * | |
| 22 | * <p> | |
| 23 | * The configurer has a priority of 50000 in order to be applied after | |
| 24 | * templating configurers. | |
| 25 | * </p> | |
| 26 | * | |
| 27 | * This configurer is applied only if {@code javax.mail.Transport} and | |
| 28 | * {@code javax.mail.internet.MimeMessage} are present in the classpath. If not | |
| 29 | * present, JavaMail implementation is not registered at all. | |
| 30 | * | |
| 31 | * <p> | |
| 32 | * This configurer inherits environment configuration (see | |
| 33 | * {@link BuildContext}). | |
| 34 | * </p> | |
| 35 | * | |
| 36 | * <p> | |
| 37 | * This configurer applies the following configuration: | |
| 38 | * <ul> | |
| 39 | * <li>Configures host and port: | |
| 40 | * <ul> | |
| 41 | * <li>It uses one of "ogham.email.javamail.host", "mail.smtp.host" or | |
| 42 | * "mail.hofromst" property if defined for mail server host address (IP or | |
| 43 | * hostname)</li> | |
| 44 | * <li>It uses one of "ogham.email.javamail.port", "mail.smtp.port" or | |
| 45 | * "mail.port" property if defined for mail server port. Default port is 25</li> | |
| 46 | * </ul> | |
| 47 | * </li> | |
| 48 | * <li>Configures authentication: | |
| 49 | * <ul> | |
| 50 | * <li>If property "ogham.email.javamail.authenticator.username" and | |
| 51 | * "ogham.email.javamail.authenticator.password" are defined, then an | |
| 52 | * {@link UsernamePasswordAuthenticator} is used to handle username/password | |
| 53 | * authentication</li> | |
| 54 | * </ul> | |
| 55 | * </li> | |
| 56 | * <li>Configures encoding: | |
| 57 | * <ul> | |
| 58 | * <li>It uses "ogham.email.javamail.body.charset" property value as charset for | |
| 59 | * email body if defined. Default charset is UTF-8</li> | |
| 60 | * </ul> | |
| 61 | * </li> | |
| 62 | * <li>Configures mimetype detection: | |
| 63 | * <ul> | |
| 64 | * <li>Uses Apache Tika to detect mimetype</li> | |
| 65 | * <li>Explicitly use "text/html" mimetype instead of more specific ones like | |
| 66 | * "application/xhtml" (XHTML)</li> | |
| 67 | * </ul> | |
| 68 | * </li> | |
| 69 | * </ul> | |
| 70 | * | |
| 71 | * @author Aurélien Baudet | |
| 72 | * | |
| 73 | */ | |
| 74 | public final class DefaultJavaMailConfigurer { | |
| 75 | private static final Logger LOG = LoggerFactory.getLogger(DefaultJavaMailConfigurer.class); | |
| 76 | private static final int DEFAULT_SMTP_PORT = 25; | |
| 77 | ||
| 78 | @ConfigurerFor(targetedBuilder = "standard", priority = DEFAULT_JAVAMAIL_CONFIGURER_PRIORITY) | |
| 79 | public static class JavaMailConfigurer implements MessagingConfigurer { | |
| 80 | @Override | |
| 81 | public void configure(MessagingBuilder msgBuilder) { | |
| 82 |
4
1. configure : negated conditional → KILLED 2. configure : negated conditional → KILLED 3. configure : negated conditional → KILLED 4. configure : negated conditional → KILLED |
if (!canUseJavaMail()) { |
| 83 | LOG.debug("[{}] skip configuration", this); | |
| 84 | return; | |
| 85 | } | |
| 86 | LOG.debug("[{}] apply configuration", this); | |
| 87 | JavaMailBuilder builder = msgBuilder.email().sender(JavaMailBuilder.class); | |
| 88 | // @formatter:off | |
| 89 | builder | |
| 90 | .host().properties("${ogham.email.javamail.host}", "${mail.smtp.host}", "${mail.host}").and() | |
| 91 | .port().properties("${ogham.email.javamail.port}", "${mail.smtp.port}", "${mail.port}").defaultValue(overrideIfNotSet(DEFAULT_SMTP_PORT)).and() | |
| 92 | .authenticator() | |
| 93 | .username().properties("${ogham.email.javamail.authenticator.username}").and() | |
| 94 | .password().properties("${ogham.email.javamail.authenticator.password}").and() | |
| 95 | .and() | |
| 96 | .charset().properties("${ogham.email.javamail.body.charset}").defaultValue(overrideIfNotSet(UTF_8)).and() | |
| 97 | .mimetype() | |
| 98 | .tika() | |
| 99 | .failIfOctetStream().defaultValue(overrideIfNotSet(false)).and() | |
| 100 | .and() | |
| 101 | .replace() | |
| 102 | // the distinction between xhtml and html can be useful in some cases | |
| 103 | // most email clients don't understand xhtml mimetype | |
| 104 | // for emails, this distinction must not be done | |
| 105 | .pattern("application/xhtml[^;]*(;.*)?", "text/html$1"); | |
| 106 | // @formatter:on | |
| 107 | } | |
| 108 | ||
| 109 | private static boolean canUseJavaMail() { | |
| 110 |
9
1. canUseJavaMail : replaced boolean return with true for fr/sii/ogham/email/builder/javamail/DefaultJavaMailConfigurer$JavaMailConfigurer::canUseJavaMail → SURVIVED 2. canUseJavaMail : negated conditional → KILLED 3. canUseJavaMail : negated conditional → KILLED 4. canUseJavaMail : negated conditional → KILLED 5. canUseJavaMail : negated conditional → KILLED 6. canUseJavaMail : negated conditional → KILLED 7. canUseJavaMail : negated conditional → KILLED 8. canUseJavaMail : negated conditional → KILLED 9. canUseJavaMail : negated conditional → KILLED |
return ClasspathUtils.exists("javax.mail.Transport") && ClasspathUtils.exists("javax.mail.internet.MimeMessage"); |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | private DefaultJavaMailConfigurer() { | |
| 115 | super(); | |
| 116 | } | |
| 117 | } | |
Mutations | ||
| 82 |
1.1 2.2 3.3 4.4 |
|
| 110 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 |