Skip to content

Commit

Permalink
Simplified MailSenderImpl constructor sequence and removed obsolete e…
Browse files Browse the repository at this point in the history
…xception case
  • Loading branch information
bbottema committed May 24, 2019
1 parent d279d3b commit cbe355e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class MailSenderException extends MailException {
static final String INVALID_PROXY_SLL_COMBINATION = "Proxy is not supported for SSL connections (this is a limitation by the underlying JavaMail framework)";
static final String GENERIC_ERROR = "Third party error";
static final String INVALID_ENCODING = "Encoding not accepted";
static final String CANNOT_SET_TRUST_WITHOUT_TRANSPORTSTRATEGY = "Cannot determine the trust properties to set without a provided transport strategy";
static final String CANNOT_SET_BOUNCETO_WITHOUT_TRANSPORTSTRATEGY = "Cannot determine the envelope .from property to set without a provided transport strategy";
static final String ERROR_CONNECTING_SMTP_SERVER = "Was unable to connect to SMTP server";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static org.simplejavamail.converter.EmailConverter.mimeMessageToEML;
import static org.simplejavamail.internal.util.ListUtil.getFirst;
import static org.simplejavamail.internal.util.Preconditions.assumeNonNull;
import static org.simplejavamail.internal.util.Preconditions.checkNonEmptyArgument;

/**
* Class that performs the actual javax.mail SMTP integration.
Expand All @@ -56,7 +55,7 @@ public class MailSenderImpl implements MailSender {
* Depending on the transport strategy, these properties are different, that's why we need to keep a global hold on this instance.
* <p>
* <strong>NOTE:</strong><br>
* This is an optional parameter and as such some functions will throw an error when used (such as {@link #trustAllHosts(boolean)}) or
* This is an optional parameter and as such some functions will throw an error when used (such as {@link #trustAllHosts(Session, boolean, TransportStrategy)}) or
* will skip setting optional properties (such as default timeouts) and also skip mandatory properties which are assumed to be preconfigured on
* the Session instance (these will be logged on DEBUG level, such as proxy host and port properties).
*/
Expand Down Expand Up @@ -95,21 +94,22 @@ public MailSenderImpl(@Nonnull final Session session,
@Nonnull final OperationalConfig operationalConfig,
@Nonnull final ProxyConfig proxyConfig,
@Nullable final TransportStrategy transportStrategy) {
initSession(session, operationalConfig, transportStrategy);

this.session = session;
this.operationalConfig = operationalConfig;
this.transportStrategy = transportStrategy;
this.proxyServer = configureSessionWithProxy(proxyConfig, session, transportStrategy);
init(operationalConfig);
}

private void init(@Nonnull OperationalConfig operationalConfig) {
private void initSession(@Nonnull final Session session, @Nonnull OperationalConfig operationalConfig, @Nullable final TransportStrategy transportStrategy) {
session.setDebug(operationalConfig.isDebugLogging());
session.getProperties().putAll(operationalConfig.getProperties());
if (transportStrategy != null) {
if (operationalConfig.isTrustAllSSLHost()) {
trustAllHosts(true);
trustAllHosts(session, true, transportStrategy);
} else {
trustHosts(operationalConfig.getSslHostsToTrust());
trustHosts(session, operationalConfig.getSslHostsToTrust(), transportStrategy);
}
}
}
Expand Down Expand Up @@ -324,29 +324,12 @@ private static void logSession(final Session session, boolean async, final Strin
LOGGER.debug("starting{} {} with {}", async ? " async" : "", activity, sessionDetails);
}

/**
* @see MailerGenericBuilderImpl#trustingAllHosts(boolean)
*/
private void trustAllHosts(final boolean trustAllHosts) {
if (transportStrategy != null) {
session.getProperties().remove(transportStrategy.propertyNameSSLTrust());
if (trustAllHosts) {
session.getProperties().setProperty(transportStrategy.propertyNameSSLTrust(), "*");
}
} else {
throw new MailSenderException(MailSenderException.CANNOT_SET_TRUST_WITHOUT_TRANSPORTSTRATEGY);
}
}

/**
* @see MailerGenericBuilderImpl#trustingSSLHosts(String...)
*/
private void trustHosts(@Nonnull final List<String> hosts) {
trustAllHosts(false);
private void trustHosts(@Nonnull final Session session, @Nonnull final List<String> hosts, @Nonnull final TransportStrategy transportStrategy) {
trustAllHosts(session, false, transportStrategy);
if (!hosts.isEmpty()) {
if (transportStrategy == null) {
throw new MailSenderException(MailSenderException.CANNOT_SET_TRUST_WITHOUT_TRANSPORTSTRATEGY);
}
final StringBuilder builder = new StringBuilder(getFirst(hosts));
for (int i = 1; i < hosts.size(); i++) {
builder.append(" ").append(hosts.get(i));
Expand All @@ -355,6 +338,16 @@ private void trustHosts(@Nonnull final List<String> hosts) {
}
}

/**
* @see MailerGenericBuilderImpl#trustingAllHosts(boolean)
*/
private void trustAllHosts(@Nonnull final Session session, final boolean trustAllHosts, @Nonnull final TransportStrategy transportStrategy) {
session.getProperties().remove(transportStrategy.propertyNameSSLTrust());
if (trustAllHosts) {
session.getProperties().setProperty(transportStrategy.propertyNameSSLTrust(), "*");
}
}

/**
* @see MailSender#testConnection(boolean)
*/
Expand Down

0 comments on commit cbe355e

Please sign in to comment.