Skip to content

Provide first module to run Struts2 based applications as AWS Lambda … #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions aws-serverless-java-container-struts2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>aws-serverless-java-container-struts2</artifactId>
<name>AWS Serverless Java container support - Struts2 implementation</name>
<description>Allows Java applications written for the Struts2 framework to run in AWS Lambda</description>
<url>https://aws.amazon.com/lambda</url>
<version>1.2-SNAPSHOT</version>

<parent>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container</artifactId>
<version>1.2-SNAPSHOT</version>
</parent>

<properties>
<struts2.version>2.5.16</struts2.version>
<jackson.version>2.9.5</jackson.version>
</properties>

<dependencies>
<!-- Core interfaces for the aws-serverless-java-container project -->
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-core</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
<optional>true</optional>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this optional because you expect developers to pick their own version of Struts2? If we want to enable this, we should make sure that this can support most versions of struts 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right struts2-core should be required when this lib is used. I changed this.

<exclusions>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>${struts2.version}</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.10.0</version>
</dependency>

<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- fork JVM before each struts2 test to make sure we have a clean context -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<forkMode>always</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<!--
Enables analysis which takes more memory but finds more bugs.
If you run out of memory, changes the value of the effort element
to 'Low'.
-->
<effort>Max</effort>
<!-- Reports all bugs (other values are medium and max) -->
<threshold>Low</threshold>
<!-- Produces XML report -->
<xmlOutput>true</xmlOutput>
<!-- Configures the directory in which the XML report is created -->
<spotbugsXmlOutputDirectory>${project.build.directory}/spotbugs</spotbugsXmlOutputDirectory>

<plugins>
<plugin>
<groupId>com.h3xstream.findsecbugs</groupId>
<artifactId>findsecbugs-plugin</artifactId>
<version>1.7.1</version>
</plugin>
</plugins>
</configuration>
<executions>
<!--
Ensures that SpotBug inspects source code when project is compiled.
-->
<execution>
<id>analyze-compile</id>
<phase>compile</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<version>3.1.1</version>
<configuration>
<skipProvidedScope>true</skipProvidedScope>
<suppressionFiles>
<suppressionFile>${project.basedir}/../owasp-suppression.xml</suppressionFile>
</suppressionFiles>
<failBuildOnCVSS>7</failBuildOnCVSS>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.amazonaws.serverless.proxy.struts2;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.AwsProxyExceptionHandler;
import com.amazonaws.serverless.proxy.AwsProxySecurityContextWriter;
import com.amazonaws.serverless.proxy.ExceptionHandler;
import com.amazonaws.serverless.proxy.RequestReader;
import com.amazonaws.serverless.proxy.ResponseWriter;
import com.amazonaws.serverless.proxy.SecurityContextWriter;
import com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse;
import com.amazonaws.serverless.proxy.internal.servlet.AwsLambdaServletContainerHandler;
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest;
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader;
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletResponseWriter;
import com.amazonaws.serverless.proxy.internal.testutils.Timer;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.services.lambda.runtime.Context;
import org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import java.util.EnumSet;
import java.util.concurrent.CountDownLatch;

/**
* A Lambda handler to initialize the Struts2 filter and proxy the requests.
*
* @param <RequestType> request type
* @param <ResponseType> response type
*/
public class Struts2LambdaContainerHandler<RequestType, ResponseType> extends AwsLambdaServletContainerHandler<RequestType, ResponseType, AwsProxyHttpServletRequest, AwsHttpServletResponse> {

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

public static final String HEADER_STRUTS_STATUS_CODE = "X-Struts-StatusCode";

private static final String TIMER_STRUTS_2_CONTAINER_CONSTRUCTOR = "STRUTS2_CONTAINER_CONSTRUCTOR";
private static final String TIMER_STRUTS_2_HANDLE_REQUEST = "STRUTS2_HANDLE_REQUEST";
private static final String TIMER_STRUTS_2_COLD_START_INIT = "STRUTS2_COLD_START_INIT";
private static final String STRUTS_FILTER_NAME = "Struts2Filter";

private boolean initialized;

public static Struts2LambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler() {
return new Struts2LambdaContainerHandler(
AwsProxyRequest.class,
AwsProxyResponse.class,
new AwsProxyHttpServletRequestReader(),
new AwsProxyHttpServletResponseWriter(),
new AwsProxySecurityContextWriter(),
new AwsProxyExceptionHandler());
}

public Struts2LambdaContainerHandler(Class<RequestType> requestTypeClass,
Class<ResponseType> responseTypeClass,
RequestReader<RequestType, AwsProxyHttpServletRequest> requestReader,
ResponseWriter<AwsHttpServletResponse, ResponseType> responseWriter,
SecurityContextWriter<RequestType> securityContextWriter,
ExceptionHandler<ResponseType> exceptionHandler) {

super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler);
Timer.start(TIMER_STRUTS_2_CONTAINER_CONSTRUCTOR);
this.initialized = false;
Timer.stop(TIMER_STRUTS_2_CONTAINER_CONSTRUCTOR);
}

protected AwsHttpServletResponse getContainerResponse(AwsProxyHttpServletRequest request, CountDownLatch latch) {
return new AwsHttpServletResponse(request, latch);
}

@Override
protected void handleRequest(AwsProxyHttpServletRequest httpServletRequest,
AwsHttpServletResponse httpServletResponse,
Context lambdaContext) throws Exception {
Timer.start(TIMER_STRUTS_2_HANDLE_REQUEST);
if (!this.initialized) {
initialize();
}

httpServletRequest.setServletContext(this.getServletContext());
this.doFilter(httpServletRequest, httpServletResponse, null);
String responseStatusCode = httpServletResponse.getHeader(HEADER_STRUTS_STATUS_CODE);
if (responseStatusCode != null) {
httpServletResponse.setStatus(Integer.parseInt(responseStatusCode));
}
Timer.stop(TIMER_STRUTS_2_HANDLE_REQUEST);
}

@Override
public void initialize() throws ContainerInitializationException {
log.info("Initialize Struts2 Lambda Application ...");
Timer.start(TIMER_STRUTS_2_COLD_START_INIT);
try {
if (this.startupHandler != null) {
this.startupHandler.onStartup(this.getServletContext());
}
StrutsPrepareAndExecuteFilter filter = new StrutsPrepareAndExecuteFilter();
FilterRegistration.Dynamic filterRegistration = this.getServletContext()
.addFilter(STRUTS_FILTER_NAME, filter);
filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
} catch (Exception e) {
throw new ContainerInitializationException("Could not initialize Struts2", e);
}

this.initialized = true;
Timer.stop(TIMER_STRUTS_2_COLD_START_INIT);
log.info("... initialize of Struts2 Lambda Application completed!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.amazonaws.serverless.proxy.struts2;

import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* The lambda handler to handle the requests.
* <p>
* <code>
* com.amazonaws.serverless.proxy.struts2.Struts2LambdaHandler::handleRequest
* </code>
*/
public class Struts2LambdaHandler implements RequestStreamHandler {

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

private final Struts2LambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = Struts2LambdaContainerHandler
.getAwsProxyHandler();

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) {

try {
AwsProxyRequest request = LambdaContainerHandler.getObjectMapper()
.readValue(inputStream, AwsProxyRequest.class);

AwsProxyResponse response = handler.proxy(request, context);
LambdaContainerHandler.getObjectMapper().writeValue(outputStream, response);

// just in case it wasn't closed by the mapper
outputStream.close();
} catch (IOException e) {
log.error("An unexpected exception happened while handling request", e);
}
}
}
Loading