|
| 1 | +package com.amazonaws.serverless.proxy.struts2; |
| 2 | + |
| 3 | +import com.amazonaws.serverless.exceptions.ContainerInitializationException; |
| 4 | +import com.amazonaws.serverless.proxy.AwsProxyExceptionHandler; |
| 5 | +import com.amazonaws.serverless.proxy.AwsProxySecurityContextWriter; |
| 6 | +import com.amazonaws.serverless.proxy.ExceptionHandler; |
| 7 | +import com.amazonaws.serverless.proxy.RequestReader; |
| 8 | +import com.amazonaws.serverless.proxy.ResponseWriter; |
| 9 | +import com.amazonaws.serverless.proxy.SecurityContextWriter; |
| 10 | +import com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse; |
| 11 | +import com.amazonaws.serverless.proxy.internal.servlet.AwsLambdaServletContainerHandler; |
| 12 | +import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequest; |
| 13 | +import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletRequestReader; |
| 14 | +import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletResponseWriter; |
| 15 | +import com.amazonaws.serverless.proxy.internal.testutils.Timer; |
| 16 | +import com.amazonaws.serverless.proxy.model.AwsProxyRequest; |
| 17 | +import com.amazonaws.serverless.proxy.model.AwsProxyResponse; |
| 18 | +import com.amazonaws.services.lambda.runtime.Context; |
| 19 | +import org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import javax.servlet.DispatcherType; |
| 24 | +import javax.servlet.FilterRegistration; |
| 25 | +import java.util.EnumSet; |
| 26 | +import java.util.concurrent.CountDownLatch; |
| 27 | + |
| 28 | +/** |
| 29 | + * A Lambda handler to initialize the Struts2 filter and proxy the requests. |
| 30 | + * |
| 31 | + * @param <RequestType> request type |
| 32 | + * @param <ResponseType> response type |
| 33 | + */ |
| 34 | +public class Struts2LambdaContainerHandler<RequestType, ResponseType> extends AwsLambdaServletContainerHandler<RequestType, ResponseType, AwsProxyHttpServletRequest, AwsHttpServletResponse> { |
| 35 | + |
| 36 | + private static final Logger log = LoggerFactory.getLogger(Struts2LambdaContainerHandler.class); |
| 37 | + |
| 38 | + public static final String HEADER_STRUTS_STATUS_CODE = "X-Struts-StatusCode"; |
| 39 | + |
| 40 | + private static final String TIMER_STRUTS_2_CONTAINER_CONSTRUCTOR = "STRUTS2_CONTAINER_CONSTRUCTOR"; |
| 41 | + private static final String TIMER_STRUTS_2_HANDLE_REQUEST = "STRUTS2_HANDLE_REQUEST"; |
| 42 | + private static final String TIMER_STRUTS_2_COLD_START_INIT = "STRUTS2_COLD_START_INIT"; |
| 43 | + private static final String STRUTS_FILTER_NAME = "Struts2Filter"; |
| 44 | + |
| 45 | + private boolean initialized; |
| 46 | + |
| 47 | + public static Struts2LambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler() { |
| 48 | + return new Struts2LambdaContainerHandler( |
| 49 | + AwsProxyRequest.class, |
| 50 | + AwsProxyResponse.class, |
| 51 | + new AwsProxyHttpServletRequestReader(), |
| 52 | + new AwsProxyHttpServletResponseWriter(), |
| 53 | + new AwsProxySecurityContextWriter(), |
| 54 | + new AwsProxyExceptionHandler()); |
| 55 | + } |
| 56 | + |
| 57 | + public Struts2LambdaContainerHandler(Class<RequestType> requestTypeClass, |
| 58 | + Class<ResponseType> responseTypeClass, |
| 59 | + RequestReader<RequestType, AwsProxyHttpServletRequest> requestReader, |
| 60 | + ResponseWriter<AwsHttpServletResponse, ResponseType> responseWriter, |
| 61 | + SecurityContextWriter<RequestType> securityContextWriter, |
| 62 | + ExceptionHandler<ResponseType> exceptionHandler) { |
| 63 | + |
| 64 | + super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler); |
| 65 | + Timer.start(TIMER_STRUTS_2_CONTAINER_CONSTRUCTOR); |
| 66 | + this.initialized = false; |
| 67 | + Timer.stop(TIMER_STRUTS_2_CONTAINER_CONSTRUCTOR); |
| 68 | + } |
| 69 | + |
| 70 | + protected AwsHttpServletResponse getContainerResponse(AwsProxyHttpServletRequest request, CountDownLatch latch) { |
| 71 | + return new AwsHttpServletResponse(request, latch); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected void handleRequest(AwsProxyHttpServletRequest httpServletRequest, |
| 76 | + AwsHttpServletResponse httpServletResponse, |
| 77 | + Context lambdaContext) throws Exception { |
| 78 | + Timer.start(TIMER_STRUTS_2_HANDLE_REQUEST); |
| 79 | + if (!this.initialized) { |
| 80 | + initialize(); |
| 81 | + } |
| 82 | + |
| 83 | + httpServletRequest.setServletContext(this.getServletContext()); |
| 84 | + this.doFilter(httpServletRequest, httpServletResponse, null); |
| 85 | + String responseStatusCode = httpServletResponse.getHeader(HEADER_STRUTS_STATUS_CODE); |
| 86 | + if (responseStatusCode != null) { |
| 87 | + httpServletResponse.setStatus(Integer.parseInt(responseStatusCode)); |
| 88 | + } |
| 89 | + Timer.stop(TIMER_STRUTS_2_HANDLE_REQUEST); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void initialize() throws ContainerInitializationException { |
| 94 | + log.info("Initialize Struts2 Lambda Application ..."); |
| 95 | + Timer.start(TIMER_STRUTS_2_COLD_START_INIT); |
| 96 | + try { |
| 97 | + if (this.startupHandler != null) { |
| 98 | + this.startupHandler.onStartup(this.getServletContext()); |
| 99 | + } |
| 100 | + StrutsPrepareAndExecuteFilter filter = new StrutsPrepareAndExecuteFilter(); |
| 101 | + FilterRegistration.Dynamic filterRegistration = this.getServletContext() |
| 102 | + .addFilter(STRUTS_FILTER_NAME, filter); |
| 103 | + filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); |
| 104 | + } catch (Exception e) { |
| 105 | + throw new ContainerInitializationException("Could not initialize Struts2", e); |
| 106 | + } |
| 107 | + |
| 108 | + this.initialized = true; |
| 109 | + Timer.stop(TIMER_STRUTS_2_COLD_START_INIT); |
| 110 | + log.info("... initialize of Struts2 Lambda Application completed!"); |
| 111 | + } |
| 112 | +} |
0 commit comments