Skip to content

Commit

Permalink
Substitute tabs for spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
olegz committed Jul 4, 2023
1 parent 41174bc commit baeaa1a
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 349 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AwsProxyRequest {

private String body;
private String version;
private String resource;
private String resource;
private AwsProxyRequestContext requestContext;
private MultiValuedTreeMap<String, String> multiValueQueryStringParameters;
private Map<String, String> queryStringParameters;
Expand Down Expand Up @@ -97,12 +97,12 @@ public String getResource() {
}

public String getVersion() {
return version;
}
return version;
}

public void setVersion(String version) {
this.version = version;
}
public void setVersion(String version) {
this.version = version;
}

public void setResource(String resource) {
this.resource = resource;
Expand Down
34 changes: 17 additions & 17 deletions aws-serverless-java-container-springboot3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,21 @@
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.function.serverless.web.FunctionClassUtils;
import org.springframework.cloud.function.serverless.web.ProxyHttpServletRequest;
import org.springframework.cloud.function.serverless.web.ProxyMvc;
Expand Down Expand Up @@ -52,75 +50,75 @@
*/
public class SpringDelegatingLambdaContainerHandler implements RequestStreamHandler {

private final Class<?>[] startupClasses;

private final ProxyMvc mvc;

private final ObjectMapper mapper;

private final AwsProxyHttpServletResponseWriter responseWriter;

public SpringDelegatingLambdaContainerHandler() {
this(new Class[] {FunctionClassUtils.getStartClass()});
}

public SpringDelegatingLambdaContainerHandler(Class<?>... startupClasses) {
this.startupClasses = startupClasses;
this.mvc = ProxyMvc.INSTANCE(this.startupClasses);
this.mapper = new ObjectMapper();
this.responseWriter = new AwsProxyHttpServletResponseWriter();
}

@SuppressWarnings({"rawtypes" })
@Override
public void handleRequest(InputStream input, OutputStream output, Context lambdaContext) throws IOException {
Map request = mapper.readValue(input, Map.class);
SecurityContextWriter securityWriter = "2.0".equals(request.get("version"))
? new AwsHttpApiV2SecurityContextWriter() : new AwsProxySecurityContextWriter();
HttpServletRequest httpServletRequest = "2.0".equals(request.get("version"))
? this.generateRequest2(request, lambdaContext, securityWriter) : this.generateRequest(request, lambdaContext, securityWriter);

CountDownLatch latch = new CountDownLatch(1);
AwsHttpServletResponse httpServletResponse = new AwsHttpServletResponse(httpServletRequest, latch);
try {
mvc.service(httpServletRequest, httpServletResponse);
latch.await(10, TimeUnit.SECONDS);
mapper.writeValue(output, responseWriter.writeResponse(httpServletResponse, lambdaContext));
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private HttpServletRequest generateRequest(Map request, Context lambdaContext, SecurityContextWriter securityWriter) {
AwsProxyRequest v1Request = this.mapper.convertValue(request, AwsProxyRequest.class);

ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(this.mvc.getApplicationContext().getServletContext(),
v1Request.getHttpMethod(), v1Request.getPath());

if (StringUtils.hasText(v1Request.getBody())) {
httpRequest.setContentType("application/json");
httpRequest.setContent(v1Request.getBody().getBytes(StandardCharsets.UTF_8));
}
httpRequest.setAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY, v1Request.getRequestContext());
httpRequest.setAttribute(RequestReader.API_GATEWAY_STAGE_VARS_PROPERTY, v1Request.getStageVariables());
httpRequest.setAttribute(RequestReader.API_GATEWAY_EVENT_PROPERTY, v1Request);
httpRequest.setAttribute(RequestReader.ALB_CONTEXT_PROPERTY, v1Request.getRequestContext().getElb());
httpRequest.setAttribute(RequestReader.LAMBDA_CONTEXT_PROPERTY, lambdaContext);
httpRequest.setAttribute(RequestReader.JAX_SECURITY_CONTEXT_PROPERTY, securityWriter.writeSecurityContext(v1Request, lambdaContext));
return httpRequest;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public HttpServletRequest generateRequest2(Map request, Context lambdaContext, SecurityContextWriter securityWriter) {
HttpApiV2ProxyRequest v2Request = this.mapper.convertValue(request, HttpApiV2ProxyRequest.class);
private final Class<?>[] startupClasses;

private final ProxyMvc mvc;

private final ObjectMapper mapper;

private final AwsProxyHttpServletResponseWriter responseWriter;

public SpringDelegatingLambdaContainerHandler() {
this(new Class[] {FunctionClassUtils.getStartClass()});
}

public SpringDelegatingLambdaContainerHandler(Class<?>... startupClasses) {
this.startupClasses = startupClasses;
this.mvc = ProxyMvc.INSTANCE(this.startupClasses);
this.mapper = new ObjectMapper();
this.responseWriter = new AwsProxyHttpServletResponseWriter();
}

@SuppressWarnings({"rawtypes" })
@Override
public void handleRequest(InputStream input, OutputStream output, Context lambdaContext) throws IOException {
Map request = mapper.readValue(input, Map.class);
SecurityContextWriter securityWriter = "2.0".equals(request.get("version"))
? new AwsHttpApiV2SecurityContextWriter() : new AwsProxySecurityContextWriter();
HttpServletRequest httpServletRequest = "2.0".equals(request.get("version"))
? this.generateRequest2(request, lambdaContext, securityWriter) : this.generateRequest(request, lambdaContext, securityWriter);

CountDownLatch latch = new CountDownLatch(1);
AwsHttpServletResponse httpServletResponse = new AwsHttpServletResponse(httpServletRequest, latch);
try {
mvc.service(httpServletRequest, httpServletResponse);
latch.await(10, TimeUnit.SECONDS);
mapper.writeValue(output, responseWriter.writeResponse(httpServletResponse, lambdaContext));
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private HttpServletRequest generateRequest(Map request, Context lambdaContext, SecurityContextWriter securityWriter) {
AwsProxyRequest v1Request = this.mapper.convertValue(request, AwsProxyRequest.class);

ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(this.mvc.getApplicationContext().getServletContext(),
v1Request.getHttpMethod(), v1Request.getPath());

if (StringUtils.hasText(v1Request.getBody())) {
httpRequest.setContentType("application/json");
httpRequest.setContent(v1Request.getBody().getBytes(StandardCharsets.UTF_8));
}
httpRequest.setAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY, v1Request.getRequestContext());
httpRequest.setAttribute(RequestReader.API_GATEWAY_STAGE_VARS_PROPERTY, v1Request.getStageVariables());
httpRequest.setAttribute(RequestReader.API_GATEWAY_EVENT_PROPERTY, v1Request);
httpRequest.setAttribute(RequestReader.ALB_CONTEXT_PROPERTY, v1Request.getRequestContext().getElb());
httpRequest.setAttribute(RequestReader.LAMBDA_CONTEXT_PROPERTY, lambdaContext);
httpRequest.setAttribute(RequestReader.JAX_SECURITY_CONTEXT_PROPERTY, securityWriter.writeSecurityContext(v1Request, lambdaContext));
return httpRequest;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public HttpServletRequest generateRequest2(Map request, Context lambdaContext, SecurityContextWriter securityWriter) {
HttpApiV2ProxyRequest v2Request = this.mapper.convertValue(request, HttpApiV2ProxyRequest.class);
ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(this.mvc.getApplicationContext().getServletContext(),
v2Request.getRequestContext().getHttp().getMethod(), v2Request.getRequestContext().getHttp().getPath());
v2Request.getRequestContext().getHttp().getMethod(), v2Request.getRequestContext().getHttp().getPath());

if (StringUtils.hasText(v2Request.getBody())) {
httpRequest.setContentType("application/json");
httpRequest.setContent(v2Request.getBody().getBytes(StandardCharsets.UTF_8));
httpRequest.setContentType("application/json");
httpRequest.setContent(v2Request.getBody().getBytes(StandardCharsets.UTF_8));
}
httpRequest.setAttribute(RequestReader.HTTP_API_CONTEXT_PROPERTY, v2Request.getRequestContext());
httpRequest.setAttribute(RequestReader.HTTP_API_STAGE_VARS_PROPERTY, v2Request.getStageVariables());
Expand Down
Loading

0 comments on commit baeaa1a

Please sign in to comment.