-
Notifications
You must be signed in to change notification settings - Fork 566
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
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a3d63aa
Provide first module to run Struts2 based applications as AWS Lambda …
jogep 69306ce
Remove optional from struts2-core dependency and remove duplicate dep…
jogep b14ee04
Merge remote-tracking branch 'remotes/upstream/master' into struts2
jogep 6af0022
Add link to Apache Struts project and to quick start guide for lambda…
jogep fbf4810
Correct link for quick start guide for lambdas based on Struts2
jogep cd9917f
Correct link for quick start guide for lambdas based on Struts2
jogep e9e474f
Switch to latest Struts2 version 2.5.17 with important security fixes
jogep 85c9a4a
Provide a pet store example for lambdas based on apache struts2 frame…
jogep c3de905
Correct some typos
jogep d48ca49
Adjust struts2 pet store log4j config
jogep 1d89996
Provide archetype for struts2 based lambdas
jogep 7e0a5ab
Switch to latest Struts2 version 2.5.18
jogep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<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> | ||
jogep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<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> |
112 changes: 112 additions & 0 deletions
112
...2/src/main/java/com/amazonaws/serverless/proxy/struts2/Struts2LambdaContainerHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...er-struts2/src/main/java/com/amazonaws/serverless/proxy/struts2/Struts2LambdaHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.amazonaws.serverless.proxy.struts2; | ||
jogep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.