|
8 | 8 | import java.util.concurrent.CountDownLatch; |
9 | 9 | import java.util.concurrent.TimeUnit; |
10 | 10 |
|
11 | | -import org.apache.commons.logging.Log; |
12 | | -import org.apache.commons.logging.LogFactory; |
13 | 11 | import org.springframework.cloud.function.serverless.web.FunctionClassUtils; |
14 | 12 | import org.springframework.cloud.function.serverless.web.ProxyHttpServletRequest; |
15 | 13 | import org.springframework.cloud.function.serverless.web.ProxyMvc; |
|
52 | 50 | */ |
53 | 51 | public class SpringDelegatingLambdaContainerHandler implements RequestStreamHandler { |
54 | 52 |
|
55 | | - private final Class<?>[] startupClasses; |
56 | | - |
57 | | - private final ProxyMvc mvc; |
58 | | - |
59 | | - private final ObjectMapper mapper; |
60 | | - |
61 | | - private final AwsProxyHttpServletResponseWriter responseWriter; |
62 | | - |
63 | | - public SpringDelegatingLambdaContainerHandler() { |
64 | | - this(new Class[] {FunctionClassUtils.getStartClass()}); |
65 | | - } |
66 | | - |
67 | | - public SpringDelegatingLambdaContainerHandler(Class<?>... startupClasses) { |
68 | | - this.startupClasses = startupClasses; |
69 | | - this.mvc = ProxyMvc.INSTANCE(this.startupClasses); |
70 | | - this.mapper = new ObjectMapper(); |
71 | | - this.responseWriter = new AwsProxyHttpServletResponseWriter(); |
72 | | - } |
73 | | - |
74 | | - @SuppressWarnings({"rawtypes" }) |
75 | | - @Override |
76 | | - public void handleRequest(InputStream input, OutputStream output, Context lambdaContext) throws IOException { |
77 | | - Map request = mapper.readValue(input, Map.class); |
78 | | - SecurityContextWriter securityWriter = "2.0".equals(request.get("version")) |
79 | | - ? new AwsHttpApiV2SecurityContextWriter() : new AwsProxySecurityContextWriter(); |
80 | | - HttpServletRequest httpServletRequest = "2.0".equals(request.get("version")) |
81 | | - ? this.generateRequest2(request, lambdaContext, securityWriter) : this.generateRequest(request, lambdaContext, securityWriter); |
82 | | - |
83 | | - CountDownLatch latch = new CountDownLatch(1); |
84 | | - AwsHttpServletResponse httpServletResponse = new AwsHttpServletResponse(httpServletRequest, latch); |
85 | | - try { |
86 | | - mvc.service(httpServletRequest, httpServletResponse); |
87 | | - latch.await(10, TimeUnit.SECONDS); |
88 | | - mapper.writeValue(output, responseWriter.writeResponse(httpServletResponse, lambdaContext)); |
89 | | - } |
90 | | - catch (Exception e) { |
91 | | - throw new IllegalStateException(e); |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - @SuppressWarnings({ "unchecked", "rawtypes" }) |
96 | | - private HttpServletRequest generateRequest(Map request, Context lambdaContext, SecurityContextWriter securityWriter) { |
97 | | - AwsProxyRequest v1Request = this.mapper.convertValue(request, AwsProxyRequest.class); |
98 | | - |
99 | | - ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(this.mvc.getApplicationContext().getServletContext(), |
100 | | - v1Request.getHttpMethod(), v1Request.getPath()); |
101 | | - |
102 | | - if (StringUtils.hasText(v1Request.getBody())) { |
103 | | - httpRequest.setContentType("application/json"); |
104 | | - httpRequest.setContent(v1Request.getBody().getBytes(StandardCharsets.UTF_8)); |
105 | | - } |
106 | | - httpRequest.setAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY, v1Request.getRequestContext()); |
107 | | - httpRequest.setAttribute(RequestReader.API_GATEWAY_STAGE_VARS_PROPERTY, v1Request.getStageVariables()); |
108 | | - httpRequest.setAttribute(RequestReader.API_GATEWAY_EVENT_PROPERTY, v1Request); |
109 | | - httpRequest.setAttribute(RequestReader.ALB_CONTEXT_PROPERTY, v1Request.getRequestContext().getElb()); |
110 | | - httpRequest.setAttribute(RequestReader.LAMBDA_CONTEXT_PROPERTY, lambdaContext); |
111 | | - httpRequest.setAttribute(RequestReader.JAX_SECURITY_CONTEXT_PROPERTY, securityWriter.writeSecurityContext(v1Request, lambdaContext)); |
112 | | - return httpRequest; |
113 | | - } |
114 | | - |
115 | | - @SuppressWarnings({ "rawtypes", "unchecked" }) |
116 | | - public HttpServletRequest generateRequest2(Map request, Context lambdaContext, SecurityContextWriter securityWriter) { |
117 | | - HttpApiV2ProxyRequest v2Request = this.mapper.convertValue(request, HttpApiV2ProxyRequest.class); |
| 53 | + private final Class<?>[] startupClasses; |
| 54 | + |
| 55 | + private final ProxyMvc mvc; |
| 56 | + |
| 57 | + private final ObjectMapper mapper; |
| 58 | + |
| 59 | + private final AwsProxyHttpServletResponseWriter responseWriter; |
| 60 | + |
| 61 | + public SpringDelegatingLambdaContainerHandler() { |
| 62 | + this(new Class[] {FunctionClassUtils.getStartClass()}); |
| 63 | + } |
| 64 | + |
| 65 | + public SpringDelegatingLambdaContainerHandler(Class<?>... startupClasses) { |
| 66 | + this.startupClasses = startupClasses; |
| 67 | + this.mvc = ProxyMvc.INSTANCE(this.startupClasses); |
| 68 | + this.mapper = new ObjectMapper(); |
| 69 | + this.responseWriter = new AwsProxyHttpServletResponseWriter(); |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings({"rawtypes" }) |
| 73 | + @Override |
| 74 | + public void handleRequest(InputStream input, OutputStream output, Context lambdaContext) throws IOException { |
| 75 | + Map request = mapper.readValue(input, Map.class); |
| 76 | + SecurityContextWriter securityWriter = "2.0".equals(request.get("version")) |
| 77 | + ? new AwsHttpApiV2SecurityContextWriter() : new AwsProxySecurityContextWriter(); |
| 78 | + HttpServletRequest httpServletRequest = "2.0".equals(request.get("version")) |
| 79 | + ? this.generateRequest2(request, lambdaContext, securityWriter) : this.generateRequest(request, lambdaContext, securityWriter); |
| 80 | + |
| 81 | + CountDownLatch latch = new CountDownLatch(1); |
| 82 | + AwsHttpServletResponse httpServletResponse = new AwsHttpServletResponse(httpServletRequest, latch); |
| 83 | + try { |
| 84 | + mvc.service(httpServletRequest, httpServletResponse); |
| 85 | + latch.await(10, TimeUnit.SECONDS); |
| 86 | + mapper.writeValue(output, responseWriter.writeResponse(httpServletResponse, lambdaContext)); |
| 87 | + } |
| 88 | + catch (Exception e) { |
| 89 | + throw new IllegalStateException(e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 94 | + private HttpServletRequest generateRequest(Map request, Context lambdaContext, SecurityContextWriter securityWriter) { |
| 95 | + AwsProxyRequest v1Request = this.mapper.convertValue(request, AwsProxyRequest.class); |
| 96 | + |
| 97 | + ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(this.mvc.getApplicationContext().getServletContext(), |
| 98 | + v1Request.getHttpMethod(), v1Request.getPath()); |
| 99 | + |
| 100 | + if (StringUtils.hasText(v1Request.getBody())) { |
| 101 | + httpRequest.setContentType("application/json"); |
| 102 | + httpRequest.setContent(v1Request.getBody().getBytes(StandardCharsets.UTF_8)); |
| 103 | + } |
| 104 | + httpRequest.setAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY, v1Request.getRequestContext()); |
| 105 | + httpRequest.setAttribute(RequestReader.API_GATEWAY_STAGE_VARS_PROPERTY, v1Request.getStageVariables()); |
| 106 | + httpRequest.setAttribute(RequestReader.API_GATEWAY_EVENT_PROPERTY, v1Request); |
| 107 | + httpRequest.setAttribute(RequestReader.ALB_CONTEXT_PROPERTY, v1Request.getRequestContext().getElb()); |
| 108 | + httpRequest.setAttribute(RequestReader.LAMBDA_CONTEXT_PROPERTY, lambdaContext); |
| 109 | + httpRequest.setAttribute(RequestReader.JAX_SECURITY_CONTEXT_PROPERTY, securityWriter.writeSecurityContext(v1Request, lambdaContext)); |
| 110 | + return httpRequest; |
| 111 | + } |
| 112 | + |
| 113 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 114 | + public HttpServletRequest generateRequest2(Map request, Context lambdaContext, SecurityContextWriter securityWriter) { |
| 115 | + HttpApiV2ProxyRequest v2Request = this.mapper.convertValue(request, HttpApiV2ProxyRequest.class); |
118 | 116 | ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(this.mvc.getApplicationContext().getServletContext(), |
119 | | - v2Request.getRequestContext().getHttp().getMethod(), v2Request.getRequestContext().getHttp().getPath()); |
| 117 | + v2Request.getRequestContext().getHttp().getMethod(), v2Request.getRequestContext().getHttp().getPath()); |
120 | 118 |
|
121 | 119 | if (StringUtils.hasText(v2Request.getBody())) { |
122 | | - httpRequest.setContentType("application/json"); |
123 | | - httpRequest.setContent(v2Request.getBody().getBytes(StandardCharsets.UTF_8)); |
| 120 | + httpRequest.setContentType("application/json"); |
| 121 | + httpRequest.setContent(v2Request.getBody().getBytes(StandardCharsets.UTF_8)); |
124 | 122 | } |
125 | 123 | httpRequest.setAttribute(RequestReader.HTTP_API_CONTEXT_PROPERTY, v2Request.getRequestContext()); |
126 | 124 | httpRequest.setAttribute(RequestReader.HTTP_API_STAGE_VARS_PROPERTY, v2Request.getStageVariables()); |
|
0 commit comments