|  | 
|  | 1 | +package com.amazonaws.serverless.proxy.spring.extensibility; | 
|  | 2 | + | 
|  | 3 | +import com.amazonaws.serverless.exceptions.ContainerInitializationException; | 
|  | 4 | +import com.amazonaws.serverless.proxy.*; | 
|  | 5 | +import com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse; | 
|  | 6 | +import com.amazonaws.serverless.proxy.model.AwsProxyRequest; | 
|  | 7 | +import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | 
|  | 8 | +import com.amazonaws.serverless.proxy.model.HttpApiV2ProxyRequest; | 
|  | 9 | +import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler; | 
|  | 10 | +import org.springframework.web.context.ConfigurableWebApplicationContext; | 
|  | 11 | + | 
|  | 12 | +import javax.servlet.ServletRegistration; | 
|  | 13 | +import javax.servlet.http.HttpServletRequest; | 
|  | 14 | + | 
|  | 15 | +public class CustomSpringLambdaContainerHandler<RequestType, ResponseType> extends SpringLambdaContainerHandler<RequestType, ResponseType> { | 
|  | 16 | + | 
|  | 17 | +    /** | 
|  | 18 | +     * Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects | 
|  | 19 | +     * @param config A set of classes annotated with the Spring @Configuration annotation | 
|  | 20 | +     * @return An initialized instance of the `SpringLambdaContainerHandler` | 
|  | 21 | +     * @throws ContainerInitializationException When the Spring framework fails to start. | 
|  | 22 | +     */ | 
|  | 23 | +    public static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(Class<?>... config) throws ContainerInitializationException { | 
|  | 24 | +        return new CustomSpringProxyHandlerBuilder<AwsProxyRequest>() | 
|  | 25 | +                .defaultProxy() | 
|  | 26 | +                .initializationWrapper(new InitializationWrapper()) | 
|  | 27 | +                .configurationClasses(config) | 
|  | 28 | +                .buildAndInitialize(); | 
|  | 29 | +    } | 
|  | 30 | + | 
|  | 31 | +    /** | 
|  | 32 | +     * Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects and sets the given profiles as active | 
|  | 33 | +     * @param applicationContext A custom ConfigurableWebApplicationContext to be used | 
|  | 34 | +     * @param profiles The spring profiles to activate | 
|  | 35 | +     * @return An initialized instance of the `SpringLambdaContainerHandler` | 
|  | 36 | +     * @throws ContainerInitializationException When the Spring framework fails to start. | 
|  | 37 | +     */ | 
|  | 38 | +    public static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(ConfigurableWebApplicationContext applicationContext, String... profiles) | 
|  | 39 | +            throws ContainerInitializationException { | 
|  | 40 | +        return new CustomSpringProxyHandlerBuilder<AwsProxyRequest>() | 
|  | 41 | +                .defaultProxy() | 
|  | 42 | +                .initializationWrapper(new InitializationWrapper()) | 
|  | 43 | +                .springApplicationContext(applicationContext) | 
|  | 44 | +                .profiles(profiles) | 
|  | 45 | +                .buildAndInitialize(); | 
|  | 46 | +    } | 
|  | 47 | + | 
|  | 48 | +    /** | 
|  | 49 | +     * Creates a default SpringLambdaContainerHandler initialized with the `HttpApiV2ProxyRequest` and `AwsProxyResponse` objects | 
|  | 50 | +     * @param config A set of classes annotated with the Spring @Configuration annotation | 
|  | 51 | +     * @return An initialized instance of the `SpringLambdaContainerHandler` | 
|  | 52 | +     * @throws ContainerInitializationException When the Spring framework fails to start. | 
|  | 53 | +     */ | 
|  | 54 | +    public static SpringLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyResponse> getHttpApiV2ProxyHandler(Class<?>... config) throws ContainerInitializationException { | 
|  | 55 | +        return new CustomSpringProxyHandlerBuilder<HttpApiV2ProxyRequest>() | 
|  | 56 | +                .defaultHttpApiV2Proxy() | 
|  | 57 | +                .initializationWrapper(new InitializationWrapper()) | 
|  | 58 | +                .configurationClasses(config) | 
|  | 59 | +                .buildAndInitialize(); | 
|  | 60 | +    } | 
|  | 61 | + | 
|  | 62 | +    /** | 
|  | 63 | +     * Creates a new container handler with the given reader and writer objects | 
|  | 64 | +     * | 
|  | 65 | +     * @param requestTypeClass The class for the incoming Lambda event | 
|  | 66 | +     * @param requestReader An implementation of `RequestReader` | 
|  | 67 | +     * @param responseWriter An implementation of `ResponseWriter` | 
|  | 68 | +     * @param securityContextWriter An implementation of `SecurityContextWriter` | 
|  | 69 | +     * @param exceptionHandler An implementation of `ExceptionHandler` | 
|  | 70 | +     */ | 
|  | 71 | +    public CustomSpringLambdaContainerHandler(Class<RequestType> requestTypeClass, | 
|  | 72 | +                                              Class<ResponseType> responseTypeClass, | 
|  | 73 | +                                              RequestReader<RequestType, HttpServletRequest> requestReader, | 
|  | 74 | +                                              ResponseWriter<AwsHttpServletResponse, ResponseType> responseWriter, | 
|  | 75 | +                                              SecurityContextWriter<RequestType> securityContextWriter, | 
|  | 76 | +                                              ExceptionHandler<ResponseType> exceptionHandler, | 
|  | 77 | +                                              ConfigurableWebApplicationContext applicationContext, | 
|  | 78 | +                                              InitializationWrapper init) { | 
|  | 79 | +        super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, | 
|  | 80 | +                exceptionHandler, applicationContext, init); | 
|  | 81 | +    } | 
|  | 82 | + | 
|  | 83 | +    @Override | 
|  | 84 | +    protected void registerServlets() { | 
|  | 85 | +        CustomServlet customServlet = new CustomServlet(); | 
|  | 86 | +        customServlet.setAppCtx(appContext); | 
|  | 87 | +        ServletRegistration.Dynamic reg = getServletContext().addServlet("customServlet", customServlet); | 
|  | 88 | +        reg.addMapping("/"); | 
|  | 89 | +        reg.setLoadOnStartup(1); | 
|  | 90 | +    } | 
|  | 91 | + | 
|  | 92 | +} | 
0 commit comments