| 1 | package fr.sii.ogham.email.builder.javamail; | |
| 2 | ||
| 3 | import fr.sii.ogham.core.builder.MessagingBuilder; | |
| 4 | import fr.sii.ogham.core.builder.configurer.ConfigurerFor; | |
| 5 | import fr.sii.ogham.core.builder.configurer.MessagingConfigurer; | |
| 6 | import fr.sii.ogham.core.builder.context.BuildContext; | |
| 7 | import fr.sii.ogham.core.exception.configurer.ClasspathConsistencyException; | |
| 8 | import fr.sii.ogham.core.exception.configurer.ConfigureException; | |
| 9 | import fr.sii.ogham.core.exception.configurer.MissingImplementationException; | |
| 10 | import fr.sii.ogham.core.util.ClasspathUtils; | |
| 11 | import fr.sii.ogham.email.builder.javamail.JavaMailConsistencyChecker.JavaMailConsistencyException; | |
| 12 | import fr.sii.ogham.email.sender.impl.javamail.UsernamePasswordAuthenticator; | |
| 13 | import jakarta.mail.internet.MimeMessage; | |
| 14 | ||
| 15 | import static fr.sii.ogham.core.builder.configuration.MayOverride.overrideIfNotSet; | |
| 16 | import static fr.sii.ogham.email.JavaMailConstants.DEFAULT_JAVAMAIL_CONFIGURER_PRIORITY; | |
| 17 | import static fr.sii.ogham.email.builder.javamail.JavaMailConsistencyChecker.checkDataHandlersAvailable; | |
| 18 | import static fr.sii.ogham.email.builder.javamail.JavaMailConsistencyChecker.checkMailProvidersAvailable; | |
| 19 | import static java.nio.charset.StandardCharsets.UTF_8; | |
| 20 | ||
| 21 | /** | |
| 22 | * Default JavaMail configurer that is automatically applied every time a | |
| 23 | * {@link MessagingBuilder} instance is created through | |
| 24 | * {@link MessagingBuilder#standard()}. | |
| 25 | * | |
| 26 | * <p> | |
| 27 | * The configurer has a priority of 50000 in order to be applied after | |
| 28 | * templating configurers. | |
| 29 | * </p> | |
| 30 | * | |
| 31 | * This configurer is applied only if {@code jakarta.mail.Transport} and | |
| 32 | * {@code jakarta.mail.internet.MimeMessage} are present in the classpath. If not | |
| 33 | * present, JavaMail implementation is not registered at all. | |
| 34 | * | |
| 35 | * <p> | |
| 36 | * This configurer inherits environment configuration (see | |
| 37 | * {@link BuildContext}). | |
| 38 | * </p> | |
| 39 | * | |
| 40 | * <p> | |
| 41 | * This configurer applies the following configuration: | |
| 42 | * <ul> | |
| 43 | * <li>Configures host and port: | |
| 44 | * <ul> | |
| 45 | * <li>It uses one of "ogham.email.javamail.host", "mail.smtp.host" or | |
| 46 | * "mail.hofromst" property if defined for mail server host address (IP or | |
| 47 | * hostname)</li> | |
| 48 | * <li>It uses one of "ogham.email.javamail.port", "mail.smtp.port" or | |
| 49 | * "mail.port" property if defined for mail server port. Default port is 25</li> | |
| 50 | * </ul> | |
| 51 | * </li> | |
| 52 | * <li>Configures authentication: | |
| 53 | * <ul> | |
| 54 | * <li>If property "ogham.email.javamail.authenticator.username" and | |
| 55 | * "ogham.email.javamail.authenticator.password" are defined, then an | |
| 56 | * {@link UsernamePasswordAuthenticator} is used to handle username/password | |
| 57 | * authentication</li> | |
| 58 | * </ul> | |
| 59 | * </li> | |
| 60 | * <li>Configures encoding: | |
| 61 | * <ul> | |
| 62 | * <li>It uses "ogham.email.javamail.body.charset" property value as charset for | |
| 63 | * email body if defined. Default charset is UTF-8</li> | |
| 64 | * </ul> | |
| 65 | * </li> | |
| 66 | * <li>Configures mimetype detection: | |
| 67 | * <ul> | |
| 68 | * <li>Uses Apache Tika to detect mimetype</li> | |
| 69 | * <li>Explicitly use "text/html" mimetype instead of more specific ones like | |
| 70 | * "application/xhtml" (XHTML)</li> | |
| 71 | * </ul> | |
| 72 | * </li> | |
| 73 | * </ul> | |
| 74 | * | |
| 75 | * @author Aurélien Baudet | |
| 76 | * | |
| 77 | */ | |
| 78 | public final class DefaultJavaMailConfigurer { | |
| 79 | private static final int DEFAULT_SMTP_PORT = 25; | |
| 80 | ||
| 81 | @ConfigurerFor(targetedBuilder = "standard", priority = DEFAULT_JAVAMAIL_CONFIGURER_PRIORITY) | |
| 82 | public static class JavaMailConfigurer implements MessagingConfigurer { | |
| 83 | @Override | |
| 84 | public void configure(MessagingBuilder msgBuilder) throws ConfigureException { | |
| 85 |
1
1. configure : removed call to fr/sii/ogham/email/builder/javamail/DefaultJavaMailConfigurer$JavaMailConfigurer::checkCanUseJavaMail → RUN_ERROR |
checkCanUseJavaMail(); |
| 86 | ||
| 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 void checkCanUseJavaMail() throws ConfigureException { | |
| 110 |
1
1. checkCanUseJavaMail : negated conditional → RUN_ERROR |
if (!isJakartaMailPresent()) { |
| 111 | throw new MissingImplementationException("Can't send Email using Java mail (Jakarta) because jakarta.mail API is not present in the classpath", "jakarta.mail.Transport", "jakarta.mail.internet.MimeMessage"); | |
| 112 | } | |
| 113 |
1
1. checkCanUseJavaMail : negated conditional → RUN_ERROR |
if (!isJakartaActivationPresent()) { |
| 114 | throw new MissingImplementationException("Can't send Email using Java mail (Jakarta) because jakarta.activation API is present in the classpath but jakarta.activation is not", "jakarta.activation.DataHandler"); | |
| 115 | } | |
| 116 | try { | |
| 117 | checkMailProvidersAvailable(); | |
| 118 | } catch (JavaMailConsistencyException e) { | |
| 119 | throw new ClasspathConsistencyException(e.getMessage(), e); | |
| 120 | } | |
| 121 | try { | |
| 122 | checkDataHandlersAvailable(); | |
| 123 | } catch (JavaMailConsistencyException e) { | |
| 124 | throw new ClasspathConsistencyException(e.getMessage(), e); | |
| 125 | } | |
| 126 | } | |
| 127 | ||
| 128 | private static boolean isJakartaMailPresent() { | |
| 129 |
2
1. isJakartaMailPresent : negated conditional → RUN_ERROR 2. isJakartaMailPresent : replaced boolean return with true for fr/sii/ogham/email/builder/javamail/DefaultJavaMailConfigurer$JavaMailConfigurer::isJakartaMailPresent → RUN_ERROR |
return ClasspathUtils.exists("jakarta.mail.Transport") |
| 130 |
1
1. isJakartaMailPresent : negated conditional → RUN_ERROR |
&& ClasspathUtils.exists("jakarta.mail.internet.MimeMessage") |
| 131 |
1
1. isJakartaMailPresent : negated conditional → RUN_ERROR |
&& ClasspathUtils.exists("jakarta.mail.Session"); |
| 132 | } | |
| 133 | ||
| 134 | private static boolean isJakartaActivationPresent() { | |
| 135 |
2
1. isJakartaActivationPresent : replaced boolean return with true for fr/sii/ogham/email/builder/javamail/DefaultJavaMailConfigurer$JavaMailConfigurer::isJakartaActivationPresent → RUN_ERROR 2. isJakartaActivationPresent : replaced boolean return with false for fr/sii/ogham/email/builder/javamail/DefaultJavaMailConfigurer$JavaMailConfigurer::isJakartaActivationPresent → RUN_ERROR |
return ClasspathUtils.exists("jakarta.activation.DataHandler"); |
| 136 | } | |
| 137 | ||
| 138 | } | |
| 139 | ||
| 140 | private DefaultJavaMailConfigurer() { | |
| 141 | super(); | |
| 142 | } | |
| 143 | } | |
Mutations | ||
| 85 |
1.1 |
|
| 110 |
1.1 |
|
| 113 |
1.1 |
|
| 129 |
1.1 2.2 |
|
| 130 |
1.1 |
|
| 131 |
1.1 |
|
| 135 |
1.1 2.2 |