forked from aws/serverless-java-container
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringBootLambdaContainerHandler.java
More file actions
236 lines (212 loc) · 12.1 KB
/
SpringBootLambdaContainerHandler.java
File metadata and controls
236 lines (212 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* 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 java.util.concurrent.CountDownLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.ExceptionHandler;
import com.amazonaws.serverless.proxy.InitializationWrapper;
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.AwsHttpServletRequest;
import com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse;
import com.amazonaws.serverless.proxy.internal.servlet.AwsLambdaServletContainerHandler;
import com.amazonaws.serverless.proxy.internal.servlet.AwsServletContext;
import com.amazonaws.serverless.proxy.internal.servlet.AwsServletRegistration;
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.model.HttpApiV2ProxyRequest;
import com.amazonaws.serverless.proxy.spring.embedded.ServerlessReactiveServletEmbeddedServerFactory;
import com.amazonaws.serverless.proxy.spring.embedded.ServerlessServletEmbeddedServerFactory;
import com.amazonaws.services.lambda.runtime.Context;
import jakarta.servlet.Servlet;
import jakarta.servlet.http.HttpServletRequest;
/**
* SpringBoot 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, HttpServletRequest, AwsHttpServletResponse> {
private static final String DISPATCHER_SERVLET_REGISTRATION_NAME = "dispatcherServlet";
private final Class<?> springBootInitializer;
private static final Logger log = LoggerFactory.getLogger(SpringBootLambdaContainerHandler.class);
private String[] springProfiles = null;
private WebApplicationType springWebApplicationType;
private ConfigurableApplicationContext applicationContext;
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 ServerlessReactiveServletEmbeddedServerFactory}.
*
* @return The initialized instance
*/
public static SpringBootLambdaContainerHandler getInstance() {
return instance;
}
/**
* 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 {
return new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
.defaultProxy()
.initializationWrapper(new InitializationWrapper())
.springBootApplication(springBootInitializer)
.profiles(profiles)
.buildAndInitialize();
}
/**
* Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `HttpApiV2ProxyRequest` 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<HttpApiV2ProxyRequest, AwsProxyResponse> getHttpApiV2ProxyHandler(Class<?> springBootInitializer, String... profiles)
throws ContainerInitializationException {
return new SpringBootProxyHandlerBuilder<HttpApiV2ProxyRequest>()
.defaultHttpApiV2Proxy()
.initializationWrapper(new InitializationWrapper())
.springBootApplication(springBootInitializer)
.profiles(profiles)
.buildAndInitialize();
}
/**
* Creates a new container handler with the given reader and writer objects
*
* @param requestTypeClass The class for the incoming Lambda event
* @param responseTypeClass The class for the Lambda function output
* @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
* @param init The initialization Wrapper that will be used to start Spring Boot
* @param applicationType The Spring Web Application Type
*/
public SpringBootLambdaContainerHandler(Class<RequestType> requestTypeClass,
Class<ResponseType> responseTypeClass,
RequestReader<RequestType, HttpServletRequest> requestReader,
ResponseWriter<AwsHttpServletResponse, ResponseType> responseWriter,
SecurityContextWriter<RequestType> securityContextWriter,
ExceptionHandler<ResponseType> exceptionHandler,
Class<?> springBootInitializer,
InitializationWrapper init,
WebApplicationType applicationType) {
super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler);
Timer.start("SPRINGBOOT2_CONTAINER_HANDLER_CONSTRUCTOR");
initialized = false;
this.springBootInitializer = springBootInitializer;
springWebApplicationType = applicationType;
setInitializationWrapper(init);
SpringBootLambdaContainerHandler.setInstance(this);
Timer.stop("SPRINGBOOT2_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(HttpServletRequest request, CountDownLatch latch) {
return new AwsHttpServletResponse(request, latch);
}
@Override
protected void handleRequest(HttpServletRequest containerRequest, AwsHttpServletResponse containerResponse, Context lambdaContext) throws Exception {
// this method of the AwsLambdaServletContainerHandler sets the servlet context
Timer.start("SPRINGBOOT2_HANDLE_REQUEST");
// wire up the application context on the first invocation
if (!initialized) {
initialize();
}
// process filters & invoke servlet
Servlet reqServlet = ((AwsServletContext)getServletContext()).getServletForPath(containerRequest.getPathInfo());
if (AwsHttpServletRequest.class.isAssignableFrom(containerRequest.getClass())) {
((AwsHttpServletRequest)containerRequest).setServletContext(getServletContext());
((AwsHttpServletRequest)containerRequest).setResponse(containerResponse);
}
doFilter(containerRequest, containerResponse, reqServlet);
Timer.stop("SPRINGBOOT2_HANDLE_REQUEST");
}
SpringApplicationBuilder getSpringApplicationBuilder(Class<?>... sources) {
return new SpringApplicationBuilder(sources);
}
@Override
public void initialize()
throws ContainerInitializationException {
Timer.start("SPRINGBOOT2_COLD_START");
SpringApplicationBuilder builder = getSpringApplicationBuilder(getEmbeddedContainerClasses())
.web(springWebApplicationType); // .REACTIVE, .SERVLET
if(springBootInitializer != null) {
builder.main(springBootInitializer);
}
if (springProfiles != null) {
builder.profiles(springProfiles);
}
applicationContext = builder.run();
if (springWebApplicationType == WebApplicationType.SERVLET) {
((ConfigurableWebApplicationContext)applicationContext).setServletContext(getServletContext());
AwsServletRegistration reg = (AwsServletRegistration)getServletContext().getServletRegistration(DISPATCHER_SERVLET_REGISTRATION_NAME);
if (reg != null) {
reg.setLoadOnStartup(1);
}
}
super.initialize();
initialized = true;
Timer.stop("SPRINGBOOT2_COLD_START");
}
private Class<?>[] getEmbeddedContainerClasses() {
Class<?>[] classes = new Class[2];
if (springWebApplicationType == WebApplicationType.REACTIVE) {
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] = ServerlessReactiveServletEmbeddedServerFactory.class;
} catch (ClassNotFoundException e) {
springWebApplicationType = WebApplicationType.SERVLET;
classes[0] = ServerlessServletEmbeddedServerFactory.class;
}
} else {
classes[0] = ServerlessServletEmbeddedServerFactory.class;
}
classes[1] = springBootInitializer;
return classes;
}
}