Skip to content

Commit

Permalink
fix(citrus-simulator): citrus-simulator compatibility
Browse files Browse the repository at this point in the history
Compatibility fixes for
the [citrusframework/citrus-simulator](https://github.com/citrusframework/citrus-simulator)
project. These changes - mostly in `citrus-jms` and `citrus-mail` - will allow it to
build on jdk17 and with the latest Spring- and Boot framework as well.

Progress originally reported in PR: citrusframework/citrus-simulator#163 (comment).
  • Loading branch information
bbortt authored and christophd committed Aug 16, 2023
1 parent ce6c750 commit f16907b
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 401 deletions.
4 changes: 0 additions & 4 deletions connectors/citrus-docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
Expand Down
17 changes: 2 additions & 15 deletions endpoints/citrus-mail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,13 @@
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>

<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
<groupId>org.eclipse.angus</groupId>
<artifactId>jakarta.mail</artifactId>
</dependency>

<!-- Test scoped dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.citrusframework.mail.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;

import jakarta.mail.Authenticator;
import jakarta.mail.MessagingException;
import jakarta.mail.PasswordAuthentication;
import org.citrusframework.common.InitializingPhase;
import org.citrusframework.context.TestContext;
import org.citrusframework.endpoint.AbstractEndpoint;
Expand All @@ -28,20 +27,24 @@
import org.citrusframework.message.RawMessage;
import org.citrusframework.messaging.Consumer;
import org.citrusframework.messaging.Producer;
import jakarta.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
* @author Christoph Deppisch
* @since 1.4
*/
public class MailClient extends AbstractEndpoint implements Producer, InitializingPhase {
/** Logger */

private static Logger log = LoggerFactory.getLogger(MailClient.class);

private MailSender mailSender = new MailSender();

/**
* Default constructor initializing endpoint configuration.
*/
Expand All @@ -51,7 +54,6 @@ public MailClient() {

/**
* Default constructor using endpoint configuration.
* @param endpointConfiguration
*/
public MailClient(MailEndpointConfiguration endpointConfiguration) {
super(endpointConfiguration);
Expand All @@ -69,7 +71,12 @@ public void send(Message message, TestContext context) {
}

MimeMailMessage mimeMessage = getEndpointConfiguration().getMessageConverter().convertOutbound(message, getEndpointConfiguration(), context);
getEndpointConfiguration().getJavaMailSender().send(mimeMessage.getMimeMessage());

try {
mailSender.send(mimeMessage.getMimeMessage());
} catch (MessagingException e) {
throw new CitrusRuntimeException("Failed to send mail message!", e);
}

ByteArrayOutputStream bos = new ByteArrayOutputStream();
Message mailMessage;
Expand Down Expand Up @@ -103,8 +110,6 @@ public Producer createProducer() {
/**
* Creates a message consumer for this endpoint. Consumer receives
* messages on this endpoint.
*
* @return
*/
@Override
public Consumer createConsumer() {
Expand All @@ -113,18 +118,31 @@ public Consumer createConsumer() {

@Override
public void initialize() {
if (StringUtils.hasText(getEndpointConfiguration().getJavaMailSender().getUsername()) ||
StringUtils.hasText(getEndpointConfiguration().getJavaMailSender().getPassword())) {
if (!StringUtils.hasText(getEndpointConfiguration().getProtocol())) {
throw new CitrusRuntimeException("A mailing protocol must be configured!");
}

Properties javaMailProperties = getEndpointConfiguration().getJavaMailSender().getJavaMailProperties();
if (StringUtils.hasText(getEndpointConfiguration().getUsername()) ||
StringUtils.hasText(getEndpointConfiguration().getPassword())) {

javaMailProperties.setProperty("mail.smtp.auth", "true");
getEndpointConfiguration().getJavaMailSender().setJavaMailProperties(javaMailProperties);
getEndpointConfiguration().getJavaMailProperties().setProperty("mail." + getEndpointConfiguration().getProtocol() + ".auth", "true");
getEndpointConfiguration().setAuthenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(getEndpointConfiguration().getUsername(), getEndpointConfiguration().getPassword());
}
});
}

if (!StringUtils.hasText(getEndpointConfiguration().getJavaMailSender().getProtocol())) {
getEndpointConfiguration().getJavaMailSender().setProtocol("smtp");
if (StringUtils.hasText(getEndpointConfiguration().getHost())) {
getEndpointConfiguration().getJavaMailProperties().setProperty("mail." + getEndpointConfiguration().getProtocol() + ".host", getEndpointConfiguration().getHost());
}
if (getEndpointConfiguration().getPort() > 0) {
getEndpointConfiguration().getJavaMailProperties().setProperty("mail." + getEndpointConfiguration().getProtocol() + ".port", String.valueOf(getEndpointConfiguration().getPort()));
}
}

void setMailSender(MailSender mailSenderMock) {
this.mailSender = mailSenderMock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package org.citrusframework.mail.client;

import java.util.Properties;

import jakarta.mail.Authenticator;
import org.citrusframework.endpoint.AbstractEndpointConfiguration;
import org.citrusframework.mail.message.MailMessageConverter;
import org.citrusframework.mail.model.MailMarshaller;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

/**
* @author Christoph Deppisch
* @since 1.4
Expand All @@ -41,39 +42,22 @@ public class MailEndpointConfiguration extends AbstractEndpointConfiguration {
/** Password */
private String password;

/** An optional username + password authenticator **/
private Authenticator authenticator;

/** Protocol */
private String protocol = JavaMailSenderImpl.DEFAULT_PROTOCOL;

/** Java mail properties */
private Properties javaMailProperties;

/** Mail sender implementation */
private JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
private Properties javaMailProperties = new Properties();

/** Mail message marshaller converts from XML to mail message object */
private MailMarshaller marshaller = new MailMarshaller();

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

/**
* Gets the mail protocol.
* @return the mail protocol.
*/
public String getProtocol() {
return protocol;
}

/**
* Set the mail protocol. Default is "smtp".
* @param protocol
*/
public void setProtocol(String protocol) {
this.protocol = protocol;
javaMailSender.setProtocol(protocol);
}

/**
/**
* Gets the mail host.
* @return the mail host.
*/
Expand All @@ -83,11 +67,11 @@ public String getHost() {

/**
* Set the mail server host, typically an SMTP host.
* @param host
* @param host the mail server host.
*/
public void setHost(String host) {
this.host = host;
javaMailSender.setHost(host);
getJavaMailProperties().setProperty("mail."+ getProtocol()+".host", host);
}

/**
Expand All @@ -101,11 +85,11 @@ public int getPort() {
/**
* Set the mail server port.
* Default is the Java mail port for SMTP (25).
* @param port
* @param port the mail server port.
*/
public void setPort(int port) {
this.port = port;
javaMailSender.setPort(port);
getJavaMailProperties().put("mail."+getProtocol()+".port", "25");
}

/**
Expand All @@ -117,19 +101,18 @@ public String getUsername() {
}

/**
* Set the username for accessing the mail host. Underlying mail seesion
* Set the username for accessing the mail host. The underlying mail session
* has to be configured with the property <code>"mail.smtp.auth"</code> set to
* <code>true</code>.
* @param username
* @param username the username for accessing the mail host.
*/
public void setUsername(String username) {
this.username = username;
javaMailSender.setUsername(username);
}

/**
* Gets the mail password.
* @return the mail ppassword.
* @return the mail password.
*/
public String getPassword() {
return password;
Expand All @@ -139,11 +122,42 @@ public String getPassword() {
* Set the password for accessing the mail host. Underlying mail seesion
* has to be configured with the property <code>"mail.smtp.auth"</code> set to
* <code>true</code>.
* @param password
* @param password the password for accessing the mail host.
*/
public void setPassword(String password) {
this.password = password;
javaMailSender.setPassword(password);
}

/**
* Gets the authenticator.
* @return The authenticator.
*/
public Authenticator getAuthenticator() {
return authenticator;
}

/**
* Sets the authenticator.
* @param authenticator the authenticator.
*/
public void setAuthenticator(Authenticator authenticator) {
this.authenticator = authenticator;
}

/**
* Gets the mail protocol.
* @return the mail protocol.
*/
public String getProtocol() {
return protocol;
}

/**
* Set the mailing protocol. Default is "smtp".
* @param protocol the mailing protocol.
*/
public void setProtocol(String protocol) {
this.protocol = protocol;
}

/**
Expand All @@ -157,56 +171,39 @@ public Properties getJavaMailProperties() {
/**
* Set JavaMail properties for the mail session such as <code>"mail.smtp.auth"</code>
* when using username and password. New session is created when properties are set.
* @param javaMailProperties
* @param javaMailProperties all mailing properties.
*/
public void setJavaMailProperties(Properties javaMailProperties) {
this.javaMailProperties = javaMailProperties;
javaMailSender.setJavaMailProperties(javaMailProperties);
}

/**
* Gets the mail message marshaller implementation.
* @return
* @return the mail message marshaller.
*/
public MailMarshaller getMarshaller() {
return marshaller;
}

/**
* Sets the mail message marshaller implementation.
* @param marshaller
* @param marshaller the mail message marshaller.
*/
public void setMarshaller(MailMarshaller marshaller) {
this.marshaller = marshaller;
}

/**
* Gets the Java mail sender implementation.
* @return
*/
public JavaMailSenderImpl getJavaMailSender() {
return javaMailSender;
}

/**
* Sets the Java mail sender implementation.
* @param javaMailSender
*/
public void setJavaMailSender(JavaMailSenderImpl javaMailSender) {
this.javaMailSender = javaMailSender;
}

/**
* Gets the mail message converter.
* @return
* @return the mail message converter.
*/
public MailMessageConverter getMessageConverter() {
return messageConverter;
}

/**
* Sets the mail message converter.
* @param messageConverter
* @param messageConverter the mail message converter.
*/
public void setMessageConverter(MailMessageConverter messageConverter) {
this.messageConverter = messageConverter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.citrusframework.mail.client;

import jakarta.mail.MessagingException;
import jakarta.mail.Transport;
import jakarta.mail.internet.MimeMessage;

/**
* This class exists mainly for testing purposes (mocking).
*/
public class MailSender {

public void send(MimeMessage mimeMessage) throws MessagingException {
Transport.send(mimeMessage);
}
}
Loading

0 comments on commit f16907b

Please sign in to comment.