-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new springboot2 module that supports both WebFlux and Servlet e…
…mbedded servers, actually addressing #239.
- Loading branch information
Showing
13 changed files
with
687 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,171 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>aws-serverless-java-container</artifactId> | ||
<groupId>com.amazonaws.serverless</groupId> | ||
<version>1.4-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.amazonaws.serverless</groupId> | ||
<artifactId>aws-serverless-java-container-springboot2</artifactId> | ||
<name>AWS Serverless Java container support - SpringBoot 2 implementation</name> | ||
<description>Allows Java applications written for SpringBoot 2 to run in AWS Lambda</description> | ||
<url>https://aws.amazon.com/lambda</url> | ||
<version>1.4-SNAPSHOT</version> | ||
|
||
<properties> | ||
<spring.version>5.1.9.RELEASE</spring.version> | ||
<springboot.version>2.1.8.RELEASE</springboot.version> | ||
|
||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</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.4-SNAPSHOT</version> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-core --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webflux</artifactId> | ||
<version>${spring.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot</artifactId> | ||
<version>${springboot.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
<version>${springboot.version}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<version>0.8.1</version> | ||
<configuration> | ||
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile> | ||
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>default-prepare-agent</id> | ||
<goals> | ||
<goal>prepare-agent</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>jacoco-site</id> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>report</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>jacoco-check</id> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>check</goal> | ||
</goals> | ||
<configuration> | ||
<haltOnFailure>true</haltOnFailure> | ||
<rules><rule> | ||
<element>BUNDLE</element> | ||
<limits> | ||
<limit> | ||
<counter>INSTRUCTION</counter> | ||
<value>COVEREDRATIO</value> | ||
<minimum>${jacoco.minCoverage}</minimum> | ||
</limit> | ||
</limits> | ||
</rule></rules> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<!-- fork JVM before each spring 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>${dependencyCheck.version}</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> |
204 changes: 204 additions & 0 deletions
204
...src/main/java/com/amazonaws/serverless/proxy/spring/SpringBootLambdaContainerHandler.java
This file contains 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,204 @@ | ||
/* | ||
* Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance | ||
* with the License. A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
package com.amazonaws.serverless.proxy.spring; | ||
|
||
import com.amazonaws.serverless.exceptions.ContainerInitializationException; | ||
import com.amazonaws.serverless.proxy.*; | ||
import com.amazonaws.serverless.proxy.internal.servlet.*; | ||
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.serverless.proxy.spring.embedded.ServerlessReactiveEmbeddedServerFactory; | ||
import com.amazonaws.serverless.proxy.spring.embedded.ServerlessServletEmbeddedServerFactory; | ||
import com.amazonaws.services.lambda.runtime.Context; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.core.env.ConfigurableEnvironment; | ||
import org.springframework.core.env.StandardEnvironment; | ||
|
||
import javax.servlet.Servlet; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
/** | ||
* SpringBoot 1.x implementation of the `LambdaContainerHandler` abstract class. This class uses the `LambdaSpringApplicationInitializer` | ||
* object behind the scenes to proxy requests. The default implementation leverages the `AwsProxyHttpServletRequest` and | ||
* `AwsHttpServletResponse` implemented in the `aws-serverless-java-container-core` package. | ||
* | ||
* Important: Make sure to add <code>LambdaFlushResponseListener</code> in your SpringBootServletInitializer subclass configure(). | ||
* | ||
* @param <RequestType> The incoming event type | ||
* @param <ResponseType> The expected return type | ||
*/ | ||
public class SpringBootLambdaContainerHandler<RequestType, ResponseType> extends AwsLambdaServletContainerHandler<RequestType, ResponseType, AwsProxyHttpServletRequest, AwsHttpServletResponse> { | ||
private final Class<?> springBootInitializer; | ||
private static final Logger log = LoggerFactory.getLogger(SpringBootLambdaContainerHandler.class); | ||
private String[] springProfiles = null; | ||
|
||
private static SpringBootLambdaContainerHandler instance; | ||
|
||
// State vars | ||
private boolean initialized; | ||
|
||
/** | ||
* We need to rely on the static instance of this for SpringBoot because we need it to access the ServletContext. | ||
* Normally, SpringBoot would initialize its own embedded container through the <code>SpringApplication.run()</code> | ||
* method. However, in our case we need to rely on the pre-initialized handler and need to fetch information from it | ||
* for our mock {@link com.amazonaws.serverless.proxy.spring.embedded.ServerlessReactiveEmbeddedServerFactory}. | ||
* | ||
* @return The initialized instance | ||
*/ | ||
public static SpringBootLambdaContainerHandler getInstance() { | ||
return instance; | ||
} | ||
|
||
/** | ||
* Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects | ||
* @param springBootInitializer {@code SpringBootServletInitializer} class | ||
* @return An initialized instance of the `SpringLambdaContainerHandler` | ||
* @throws ContainerInitializationException If an error occurs while initializing the Spring framework | ||
*/ | ||
public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(Class<?> springBootInitializer) | ||
throws ContainerInitializationException { | ||
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> newHandler = new SpringBootLambdaContainerHandler<>( | ||
AwsProxyRequest.class, | ||
AwsProxyResponse.class, | ||
new AwsProxyHttpServletRequestReader(), | ||
new AwsProxyHttpServletResponseWriter(), | ||
new AwsProxySecurityContextWriter(), | ||
new AwsProxyExceptionHandler(), | ||
springBootInitializer); | ||
newHandler.initialize(); | ||
return newHandler; | ||
} | ||
|
||
/** | ||
* Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects and the given Spring profiles | ||
* @param springBootInitializer {@code SpringBootServletInitializer} class | ||
* @param profiles A list of Spring profiles to activate | ||
* @return An initialized instance of the `SpringLambdaContainerHandler` | ||
* @throws ContainerInitializationException If an error occurs while initializing the Spring framework | ||
*/ | ||
public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(Class<?> springBootInitializer, String... profiles) | ||
throws ContainerInitializationException { | ||
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> newHandler = new SpringBootLambdaContainerHandler<>( | ||
AwsProxyRequest.class, | ||
AwsProxyResponse.class, | ||
new AwsProxyHttpServletRequestReader(), | ||
new AwsProxyHttpServletResponseWriter(), | ||
new AwsProxySecurityContextWriter(), | ||
new AwsProxyExceptionHandler(), | ||
springBootInitializer); | ||
newHandler.activateSpringProfiles(profiles); | ||
newHandler.initialize(); | ||
return newHandler; | ||
} | ||
|
||
/** | ||
* Creates a new container handler with the given reader and writer objects | ||
* | ||
* @param requestTypeClass The class for the incoming Lambda event | ||
* @param requestReader An implementation of `RequestReader` | ||
* @param responseWriter An implementation of `ResponseWriter` | ||
* @param securityContextWriter An implementation of `SecurityContextWriter` | ||
* @param exceptionHandler An implementation of `ExceptionHandler` | ||
* @param springBootInitializer {@code SpringBootServletInitializer} class | ||
* @throws ContainerInitializationException If an error occurs while initializing the Spring framework | ||
*/ | ||
public SpringBootLambdaContainerHandler(Class<RequestType> requestTypeClass, | ||
Class<ResponseType> responseTypeClass, | ||
RequestReader<RequestType, AwsProxyHttpServletRequest> requestReader, | ||
ResponseWriter<AwsHttpServletResponse, ResponseType> responseWriter, | ||
SecurityContextWriter<RequestType> securityContextWriter, | ||
ExceptionHandler<ResponseType> exceptionHandler, | ||
Class<?> springBootInitializer) | ||
throws ContainerInitializationException { | ||
super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler); | ||
Timer.start("SPRINGBOOT_CONTAINER_HANDLER_CONSTRUCTOR"); | ||
initialized = false; | ||
this.springBootInitializer = springBootInitializer; | ||
SpringBootLambdaContainerHandler.setInstance(this); | ||
|
||
Timer.stop("SPRINGBOOT_CONTAINER_HANDLER_CONSTRUCTOR"); | ||
} | ||
|
||
// this is not pretty. However, because SpringBoot wants to control all of the initialization | ||
// we need to access this handler as a singleton from the EmbeddedContainer to set the servlet | ||
// context and from the ServletConfigurationSupport implementation | ||
private static void setInstance(SpringBootLambdaContainerHandler h) { | ||
SpringBootLambdaContainerHandler.instance = h; | ||
} | ||
|
||
public void activateSpringProfiles(String... profiles) { | ||
springProfiles = profiles; | ||
// force a re-initialization | ||
initialized = false; | ||
} | ||
|
||
@Override | ||
protected AwsHttpServletResponse getContainerResponse(AwsProxyHttpServletRequest request, CountDownLatch latch) { | ||
return new AwsHttpServletResponse(request, latch); | ||
} | ||
|
||
@Override | ||
protected void handleRequest(AwsProxyHttpServletRequest containerRequest, AwsHttpServletResponse containerResponse, Context lambdaContext) throws Exception { | ||
// this method of the AwsLambdaServletContainerHandler sets the servlet context | ||
Timer.start("SPRINGBOOT_HANDLE_REQUEST"); | ||
|
||
// wire up the application context on the first invocation | ||
if (!initialized) { | ||
initialize(); | ||
} | ||
|
||
containerRequest.setServletContext(getServletContext()); | ||
|
||
// process filters & invoke servlet | ||
Servlet reqServlet = ((AwsServletContext)getServletContext()).getServletForPath(containerRequest.getPathInfo()); | ||
doFilter(containerRequest, containerResponse, reqServlet); | ||
Timer.stop("SPRINGBOOT_HANDLE_REQUEST"); | ||
} | ||
|
||
|
||
@Override | ||
public void initialize() | ||
throws ContainerInitializationException { | ||
Timer.start("SPRINGBOOT_COLD_START"); | ||
|
||
SpringApplication app = new SpringApplication(getEmbeddedContainerClasses()); | ||
if (springProfiles != null && springProfiles.length > 0) { | ||
ConfigurableEnvironment springEnv = new StandardEnvironment(); | ||
springEnv.setActiveProfiles(springProfiles); | ||
app.setEnvironment(springEnv); | ||
} | ||
|
||
app.run(); | ||
|
||
initialized = true; | ||
Timer.stop("SPRINGBOOT_COLD_START"); | ||
} | ||
|
||
private Class<?>[] getEmbeddedContainerClasses() { | ||
Class<?>[] classes = new Class[2]; | ||
try { | ||
// if HandlerAdapter is available we assume they are using WebFlux. Otherwise plain servlet. | ||
this.getClass().getClassLoader().loadClass("org.springframework.web.reactive.HandlerAdapter"); | ||
log.debug("Found WebFlux HandlerAdapter on classpath, using reactive server factory"); | ||
classes[0] = ServerlessReactiveEmbeddedServerFactory.class; | ||
} catch (ClassNotFoundException e) { | ||
classes[0] = ServerlessServletEmbeddedServerFactory.class; | ||
} | ||
|
||
classes[1] = springBootInitializer; | ||
return classes; | ||
} | ||
} |
Oops, something went wrong.