Skip to content

Commit

Permalink
Provide a default page for GreenMail WAR (issue #106)
Browse files Browse the repository at this point in the history
Enhanced webapp for JAX-RS GreenMail API resource with initial support
for getting current configuration.

Added OpenAPI spec for API, and visualization / test UI by GreenMail API OpenAPI spec.
  • Loading branch information
marcelmay committed Oct 22, 2019
1 parent 7a7a483 commit 54feaf4
Show file tree
Hide file tree
Showing 15 changed files with 502 additions and 53 deletions.
Binary file added greenmail-site/images/greenmail_webapp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions greenmail-site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,15 @@ <h5>Passing configuration options to GreenMail Standalone Docker image</h5>

<h3 id="deploy_webapp" class="anchor">Deploy as a webapp (WAR)</h3>

<p>GreenMail Webapp provides a lightweight Java web application wrapping GreenMail mail server.</p>
<p>GreenMail Webapp provides a lightweight Java web application wrapping GreenMail mail server and exposing GreenMail API.</p>

<img src="images/greenmail_webapp.png" class="img-rounded"
alt="GreenMail webapp showing OpenAPI UI"/>

<p>Like the <a href="#deploy_jboss_sar">GreenMail JBoss Service</a>, the usage scenario is a development
or test environment where a
real mail server is too much overhead. The webapp is application server neutral - you should be able
to use it on any JEE application server running und Java 5.</p>
to use it on any Java EE 7 application server (Servlet 3.1) running und Java 7 (or greater).</p>

<p>With the GreenMail mail service each developer has its own local mail server sandbox - so there's no
danger for accidentally leaking test mails into the Internet.</p>
Expand Down
41 changes: 39 additions & 2 deletions greenmail-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<packaging>war</packaging>
<name>== Greenmail Webapp ==</name>
<description>Embedded GreenMail Webapp</description>

<parent>
<groupId>com.icegreen</groupId>
<artifactId>greenmail-parent</artifactId>
Expand All @@ -21,19 +21,52 @@
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>

<!-- Test scope -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<scope>test</scope>
</dependency>

<!-- Logging -->
<dependency>
Expand All @@ -58,6 +91,10 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
package com.icegreen.greenmail.webapp;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* DESC
* GreenMail Web configuration.
*
* @author mm
*/
public class Configuration {
/** Default hostname ({@value} */
/**
* Default hostname ({@value}
*/
public static final String DEFAULT_HOSTNAME = "localhost";
/**
* Default port offset added to SMTP/IMAP/POP3/... default ports ({@value}).
*
* <p>
* 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.
*/
static class ServiceConfiguration {
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;
}
}
static class User {

public static class User {
String login;
String password;

public String getLogin() {
return login;
}

public String getEmail() {
return email;
}

String email;
}

Expand Down Expand Up @@ -85,8 +109,8 @@ public List<ServiceConfiguration> getServiceConfigurations() {
}

public ServiceConfiguration getServiceConfigurationByProtocol(final Protocol pProtocol) {
for(ServiceConfiguration c: services) {
if(pProtocol.equals(c.protocol)) {
for (ServiceConfiguration c : services) {
if (pProtocol.equals(c.protocol)) {
return c;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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;

/**
* JAX-RS 2.0
*/
@Path("/configuration")
public class ConfigurationResource {
private @Context
ServletContext context;

@GET
@Produces("application/json")
public Configuration configuration() {
return ContextHelper.getConfiguration(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.icegreen.greenmail.webapp;

import javax.servlet.ServletContext;

import com.icegreen.greenmail.Managers;

/**
* Helps accessing servlet context attributes.
*/
public class ContextHelper {
static final String ATTRIBUTE_NAME_MANAGERS = "greenmail_managers";
static final String ATTRIBUTE_NAME_CONFIGURATION = "greenmail_configuration";

private ContextHelper() {
// Nothing
}

public static void initAttributes(ServletContext ctx, Managers managers, Configuration configuration) {
ctx.setAttribute(ATTRIBUTE_NAME_MANAGERS, managers);
ctx.setAttribute(ATTRIBUTE_NAME_CONFIGURATION, configuration);
}

public static Managers getManagers(ServletContext ctx) {
return (Managers) ctx.getAttribute(ATTRIBUTE_NAME_MANAGERS);
}

public static Configuration getConfiguration(ServletContext ctx) {
return (Configuration) ctx.getAttribute(ATTRIBUTE_NAME_CONFIGURATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.Map;

/**
* Automatically starts and stops GreenMail server upon deployment/undeployment.
* Automatically starts and stops GreenMail server upon deployment/un-deployment.
*
* @author mm
*/
Expand Down Expand Up @@ -47,10 +47,13 @@ public void contextInitialized(final ServletContextEvent sce) {
}
}
}

for (Service s : services) {
log.info("Starting GreenMail service: {}", s);
s.startService();
}

ContextHelper.initAttributes(ctx, managers, configuration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.icegreen.greenmail.webapp;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.ObjectMapper;

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

/**
*
*/
@Provider
public class JacksonObjectMapperProvider implements ContextResolver<ObjectMapper> {

final ObjectMapper defaultObjectMapper;

public JacksonObjectMapperProvider() {
defaultObjectMapper = createDefaultMapper();
}

@Override
public ObjectMapper getContext(Class<?> type) {
return defaultObjectMapper;
}


private static ObjectMapper createDefaultMapper() {
final ObjectMapper result = new ObjectMapper();
result.configure(FAIL_ON_EMPTY_BEANS, false);

return result;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Defines the supported mail protocols and default ports.
*/
enum Protocol {
public enum Protocol {
/** SMTP */
SMTP(25),
/** Secure SMTP */
Expand Down
91 changes: 55 additions & 36 deletions greenmail-webapp/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<context-param>
<param-name>greenmail.portOffset</param-name>
<param-value>10000</param-value>
</context-param>
<context-param>
<param-name>greenmail.smtp.host</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<context-param>
<param-name>greenmail.smtp.port</param-name>
<param-value>10025</param-value>
</context-param>
<context-param>
<param-name>greenmail.pop3.host</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<context-param>
<param-name>greenmail.pop3.port</param-name>
<param-value>10110</param-value>
</context-param>
<context-param>
<param-name>greenmail.users</param-name>
<param-value>
user1:password@localhost
user2:password2@localhost
</param-value>
</context-param>
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<context-param>
<param-name>greenmail.portOffset</param-name>
<param-value>10000</param-value>
</context-param>
<context-param>
<param-name>greenmail.smtp.host</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<context-param>
<param-name>greenmail.smtp.port</param-name>
<param-value>10025</param-value>
</context-param>
<context-param>
<param-name>greenmail.pop3.host</param-name>
<param-value>127.0.0.1</param-value>
</context-param>
<context-param>
<param-name>greenmail.pop3.port</param-name>
<param-value>10110</param-value>
</context-param>
<context-param>
<param-name>greenmail.users</param-name>
<param-value>
user1:password@localhost
user2:password2@localhost
</param-value>
</context-param>

<listener>
<description>GreenMail Server Startup Listener</description>
<listener-class>com.icegreen.greenmail.webapp.GreenMailListener</listener-class>
</listener>

<servlet>
<servlet-name>Jersey</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.icegreen.greenmail.webapp</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>



<listener>
<description>GreenMail Server Startup Listener</description>
<listener-class>com.icegreen.greenmail.webapp.GreenMailListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Loading

0 comments on commit 54feaf4

Please sign in to comment.