Skip to content

Commit

Permalink
Jakarta EE 9 baseline (servlet 5, restfulWS-3.0, fixes #546)
Browse files Browse the repository at this point in the history
- Update Jersey 2.x to 3.0.9 (fixes #544)
- Update jetty to 11.x (fixes #545)
  • Loading branch information
marcelmay committed Feb 26, 2023
1 parent cf0f04c commit 56e4554
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 181 deletions.
20 changes: 20 additions & 0 deletions greenmail-docker/standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;
import com.icegreen.greenmail.util.ServerSetupTest;
import org.junit.Test;

import jakarta.mail.Folder;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Store;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.junit.Test;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class DockerServiceIT {

Expand All @@ -26,27 +31,54 @@ public void testAllServices() throws MessagingException, InterruptedException {

// Send messages via SMTP and secure SMTPS
GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost",
"test1", "Test GreenMail Docker service",
ServerSetupTest.SMTP.createCopy(bindAddress));
"test1", "Test GreenMail Docker service",
ServerSetupTest.SMTP.createCopy(bindAddress));
GreenMailUtil.sendTextEmail("foo@localhost", "bar@localhost",
"test2", "Test GreenMail Docker service",
ServerSetupTest.SMTPS.createCopy(bindAddress));
"test2", "Test GreenMail Docker service",
ServerSetupTest.SMTPS.createCopy(bindAddress));

// IMAP
for (ServerSetup setup : Arrays.asList(
ServerSetupTest.IMAP.createCopy(bindAddress),
ServerSetupTest.IMAPS.createCopy(bindAddress),
ServerSetupTest.POP3.createCopy(bindAddress),
ServerSetupTest.POP3S.createCopy(bindAddress))) {
ServerSetupTest.IMAP.createCopy(bindAddress),
ServerSetupTest.IMAPS.createCopy(bindAddress),
ServerSetupTest.POP3.createCopy(bindAddress),
ServerSetupTest.POP3S.createCopy(bindAddress))) {
final Store store = Session.getInstance(setup.configureJavaMailSessionProperties(null, false)).getStore();
store.connect("foo@localhost", "foo@localhost");
try {
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
assertEquals("Can not check mails using "+store.getURLName(), 2, folder.getMessageCount());
assertThat(folder.getMessageCount())
.isEqualTo(2)
.withFailMessage("Can not check mails using " + store.getURLName());
} finally {
store.close();
}
}

// API
Client client = ClientBuilder.newClient();
WebTarget api = client.target("http://" + bindAddress + ":8080");

// Check indexResponse page
final Response indexResponse = api.path("/")
.request(MediaType.TEXT_HTML).get(Response.class);
assertThat(indexResponse.getStatus()).isEqualTo(200);
assertThat(indexResponse.readEntity(String.class)).contains("GreenMail API");

// Check API
final Response configResponse = api.path("/api/configuration")
.request(MediaType.APPLICATION_JSON).get(Response.class);
assertThat(configResponse.getStatus()).isEqualTo(200);
assertThat(configResponse.readEntity(String.class)).isEqualTo("{" +
"\"serverSetups\":[" +
"{\"port\":3025,\"address\":\"0.0.0.0\",\"protocol\":\"smtp\",\"isSecure\":false,\"readTimeout\":-1,\"writeTimeout\":-1,\"connectionTimeout\":-1,\"serverStartupTimeout\":2000,\"isDynamicPort\":false}," +
"{\"port\":3465,\"address\":\"0.0.0.0\",\"protocol\":\"smtps\",\"isSecure\":true,\"readTimeout\":-1,\"writeTimeout\":-1,\"connectionTimeout\":-1,\"serverStartupTimeout\":2000,\"isDynamicPort\":false}," +
"{\"port\":3110,\"address\":\"0.0.0.0\",\"protocol\":\"pop3\",\"isSecure\":false,\"readTimeout\":-1,\"writeTimeout\":-1,\"connectionTimeout\":-1,\"serverStartupTimeout\":2000,\"isDynamicPort\":false}," +
"{\"port\":3995,\"address\":\"0.0.0.0\",\"protocol\":\"pop3s\",\"isSecure\":true,\"readTimeout\":-1,\"writeTimeout\":-1,\"connectionTimeout\":-1,\"serverStartupTimeout\":2000,\"isDynamicPort\":false}," +
"{\"port\":3143,\"address\":\"0.0.0.0\",\"protocol\":\"imap\",\"isSecure\":false,\"readTimeout\":-1,\"writeTimeout\":-1,\"connectionTimeout\":-1,\"serverStartupTimeout\":2000,\"isDynamicPort\":false}," +
"{\"port\":3993,\"address\":\"0.0.0.0\",\"protocol\":\"imaps\",\"isSecure\":true,\"readTimeout\":-1,\"writeTimeout\":-1,\"connectionTimeout\":-1,\"serverStartupTimeout\":2000,\"isDynamicPort\":false}" +
"]," +
"\"authenticationDisabled\":true}"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import javax.ws.rs.core.UriBuilder;
import jakarta.ws.rs.core.UriBuilder;
import java.net.URI;
import java.util.Properties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.icegreen.greenmail.user.GreenMailUser;
import com.icegreen.greenmail.util.ServerSetup;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.Provider;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import java.io.IOException;

import static com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS;

/**
*
*/
@Provider
public class JacksonObjectMapperProvider implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.PropertiesBasedServerSetupBuilder;
import com.icegreen.greenmail.util.ServerSetupTest;
import jakarta.mail.Folder;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.Store;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.Invocation;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.junit.After;
import org.junit.Test;

import jakarta.mail.*;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -31,8 +39,8 @@ public void tearDown() {
public void testDoRun() throws MessagingException {
runner = createAndConfigureRunner(new Properties());

GreenMailUtil.sendTextEmail("test2@localhost", "test1@localhost", "Standalone test", "It worked",
ServerSetupTest.SMTP);
GreenMailUtil.sendTextEmail("test2@localhost", "test1@localhost",
"Standalone test", "It worked", ServerSetupTest.SMTP);

final Session session = runner.getGreenMail().getImap().createSession();
assertThat(session.getDebug()).isTrue();
Expand Down Expand Up @@ -90,10 +98,12 @@ public void testApi() {
String userId = "foo.bar";
final Response userCreateResponse = api.path("/api/user")
.request(MediaType.APPLICATION_JSON)
.post(Entity.entity("{\"email\":\"foo.bar@localhost\", \"login\":\""+userId+"\", \"password\":\"xxx\"}",
.post(Entity.entity(
"{\"email\":\"foo.bar@localhost\", \"login\":\"" + userId + "\", \"password\":\"xxx\"}",
MediaType.APPLICATION_JSON));
assertThat(userCreateResponse.getStatus()).isEqualTo(200);
assertThat(userCreateResponse.readEntity(String.class)).isEqualTo("{\"login\":\""+userId+"\",\"email\":\"foo.bar@localhost\"}");
assertThat(userCreateResponse.readEntity(String.class))
.isEqualTo("{\"login\":\"" + userId + "\",\"email\":\"foo.bar@localhost\"}");

final Invocation.Builder deleteRequest = api.path("/api/user/" + userId).request();
final Response userDeleteResponse = deleteRequest.delete();
Expand All @@ -103,7 +113,8 @@ public void testApi() {
final Response readinessResponse = api.path("/api/service/readiness")
.request(MediaType.APPLICATION_JSON).get(Response.class);
assertThat(readinessResponse.getStatus()).isEqualTo(200);
assertThat(readinessResponse.readEntity(String.class)).isEqualTo("{\"message\":\"Service running\"}");
assertThat(readinessResponse.readEntity(String.class))
.isEqualTo("{\"message\":\"Service running\"}");
}

private GreenMailStandaloneRunner createAndConfigureRunner(Properties properties) {
Expand Down
4 changes: 2 additions & 2 deletions greenmail-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<artifactId>greenmail</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,10 @@ public class Configuration {
* Example: A port offset of 10000 results in SMTP port 10025.
*/
public static final int DEFAULT_PORT_OFFSET = 10000;

/**
* A mail service configuration entry.
* <p>
* An entry contains a mandatory protocol and optional hostname and port.
* If the hostname and port are not configured, GreenMail uses
* the default hostname and the default protocol port plus the port offset.
*/
public static class ServiceConfiguration {
Protocol protocol;
String hostname;
int port;

public Protocol getProtocol() {
return protocol;
}

public String getHostname() {
return hostname;
}

public int getPort() {
return port;
}
}

public static class User {
String login;
String password;

public String getLogin() {
return login;
}

public String getEmail() {
return email;
}

String email;
}

private String defaultHostname;
private int portOffset;
private final List<ServiceConfiguration> services;
private final List<User> users;

private String defaultHostname;
private int portOffset;
/**
* Initializes configuration with
*/
Expand Down Expand Up @@ -116,4 +74,43 @@ public ServiceConfiguration getServiceConfigurationByProtocol(final Protocol pPr
}
return null;
}

/**
* A mail service configuration entry.
* <p>
* An entry contains a mandatory protocol and optional hostname and port.
* If the hostname and port are not configured, GreenMail uses
* the default hostname and the default protocol port plus the port offset.
*/
public static class ServiceConfiguration {
Protocol protocol;
String hostname;
int port;

public Protocol getProtocol() {
return protocol;
}

public String getHostname() {
return hostname;
}

public int getPort() {
return port;
}
}

public static class User {
String login;
String password;
String email;

public String getLogin() {
return login;
}

public String getEmail() {
return email;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void configure(final Configuration pConf,
protocol = Protocol.valueOf(pParamName.substring(0, dotIdx).toUpperCase());
}
Configuration.ServiceConfiguration serviceConf =
pConf.getServiceConfigurationByProtocol(protocol);
pConf.getServiceConfigurationByProtocol(protocol);
if (null == serviceConf) {
serviceConf = new Configuration.ServiceConfiguration();
serviceConf.protocol = protocol;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.icegreen.greenmail.webapp;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import jakarta.servlet.ServletContext;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;

/**
* JAX-RS 2.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.icegreen.greenmail.webapp;

import javax.servlet.ServletContext;

import com.icegreen.greenmail.Managers;
import jakarta.servlet.ServletContext;

/**
* Helps accessing servlet context attributes.
Expand Down
Loading

0 comments on commit 56e4554

Please sign in to comment.