Skip to content

Commit

Permalink
fix(#1199): respect custom mailserver on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Sep 2, 2024
1 parent 622d733 commit 0ee0c56
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@

package org.citrusframework.mail.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Stack;
import java.util.stream.Collectors;
import javax.xml.transform.Source;

import com.icegreen.greenmail.mail.MailAddress;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.user.UserException;
Expand All @@ -38,18 +30,19 @@
import org.citrusframework.mail.message.CitrusMailMessageHeaders;
import org.citrusframework.mail.message.MailMessage;
import org.citrusframework.mail.message.MailMessageConverter;
import org.citrusframework.mail.model.AcceptResponse;
import org.citrusframework.mail.model.AttachmentPart;
import org.citrusframework.mail.model.BodyPart;
import org.citrusframework.mail.model.MailMarshaller;
import org.citrusframework.mail.model.MailRequest;
import org.citrusframework.mail.model.MailResponse;
import org.citrusframework.mail.model.*;
import org.citrusframework.message.Message;
import org.citrusframework.server.AbstractServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.MimeMailMessage;

import javax.xml.transform.Source;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;

/**
* Mail server implementation starts new SMTP server instance and listens for incoming mail messages. Incoming mail messages
* are converted to XML representation and forwarded to some message endpoint adapter (e.g. forwarding mail content to
Expand All @@ -66,41 +59,63 @@
*/
public class MailServer extends AbstractServer {

/** Logger */
/**
* Logger
*/
private static final Logger logger = LoggerFactory.getLogger(MailServer.class);

/** Server port */
/**
* Server port
*/
private int port = 25;

/** XML message mapper */
/**
* XML message mapper
*/
private MailMarshaller marshaller = new MailMarshaller();

/** Mail message converter */
/**
* Mail message converter
*/
private MailMessageConverter messageConverter = new MailMessageConverter();

/** Java mail session */
/**
* Java mail session
*/
private Session mailSession;

/** Java mail properties */
/**
* Java mail properties
*/
private Properties javaMailProperties = new Properties();

/** Should accept automatically or handled via test case */
/**
* Should accept automatically or handled via test case
*/
private boolean autoAccept = true;

/** Requires users to properly authenticate with the server */
/**
* Requires users to properly authenticate with the server
*/
private boolean authRequired = true;

/** Should split multipart messages for each mime part */
/**
* Should split multipart messages for each mime part
*/
private boolean splitMultipart = false;

private final List<String> knownUsers = new ArrayList<>();

/** Smtp server instance */
/**
* Smtp server instance
*/
private GreenMail smtpServer;

@Override
protected void startup() {
smtpServer = new GreenMail(new ServerSetup(port, null, "smtp"));
if (isNull(smtpServer)) {
smtpServer = new GreenMail(new ServerSetup(port, null, "smtp"));
}

if (!authRequired) {
smtpServer.getManagers().getSmtpManager().getUserManager().setAuthRequired(false);
Expand Down Expand Up @@ -131,26 +146,26 @@ protected void startup() {

private void addKnownUsers(UserManager userManager) {
knownUsers.stream()
.map(userSpec -> userSpec.split(":"))
.map(credentials -> {
if (credentials.length > 3) {
return new String[] { credentials[0], credentials[1], credentials[2] };
} else if (credentials.length == 2) {
return new String[] { credentials[0], credentials[1], credentials[0] };
} else if (credentials.length == 1) {
return new String[] { credentials[0], credentials[0], credentials[0] };
} else {
return credentials;
}
})
.filter(credentials -> credentials.length == 3)
.forEach(credentials -> {
try {
userManager.createUser(credentials[0], credentials[1], credentials[2]);
} catch (UserException e) {
logger.warn(String.format("Failed to create known user: %s:%s:%s", credentials[0], credentials[1], credentials[2]));
}
});
.map(userSpec -> userSpec.split(":"))
.map(credentials -> {
if (credentials.length > 3) {
return new String[]{credentials[0], credentials[1], credentials[2]};
} else if (credentials.length == 2) {
return new String[]{credentials[0], credentials[1], credentials[0]};
} else if (credentials.length == 1) {
return new String[]{credentials[0], credentials[0], credentials[0]};
} else {
return credentials;
}
})
.filter(credentials -> credentials.length == 3)
.forEach(credentials -> {
try {
userManager.createUser(credentials[0], credentials[1], credentials[2]);
} catch (UserException e) {
logger.warn(String.format("Failed to create known user: %s:%s:%s", credentials[0], credentials[1], credentials[2]));
}
});
}

@Override
Expand All @@ -165,7 +180,7 @@ public boolean accept(String from, List<MailAddress> recipients) {

Message response = getEndpointAdapter().handleMessage(
MailMessage.accept(from, recipients.stream().map(MailAddress::getEmail).collect(Collectors.joining(",")))
.marshaller(marshaller));
.marshaller(marshaller));

if (response == null || response.getPayload() == null) {
throw new CitrusRuntimeException("Did not receive accept response. Missing accept response because autoAccept is disabled.");
Expand Down Expand Up @@ -271,6 +286,7 @@ public synchronized Session getSession() {

/**
* Users must authenticate properly with the server.
*
* @return
*/
public boolean isAuthRequired() {
Expand All @@ -279,6 +295,7 @@ public boolean isAuthRequired() {

/**
* Enable/disable the user authentication on this server.
*
* @param authRequired
*/
public void setAuthRequired(boolean authRequired) {
Expand Down Expand Up @@ -385,6 +402,7 @@ public void setMessageConverter(MailMessageConverter messageConverter) {

/**
* Gets the known users.
*
* @return
*/
public List<String> getKnownUsers() {
Expand All @@ -393,6 +411,7 @@ public List<String> getKnownUsers() {

/**
* Sets the known users.
*
* @param knownUsers
*/
public void setKnownUsers(List<String> knownUsers) {
Expand All @@ -401,6 +420,7 @@ public void setKnownUsers(List<String> knownUsers) {

/**
* Adds a new user known to this mail server.
*
* @param email
* @param login
* @param password
Expand Down
Loading

0 comments on commit 0ee0c56

Please sign in to comment.