-
Notifications
You must be signed in to change notification settings - Fork 566
Add support for native web workloads #739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
566e926
Add support for native web workloads
olegz fa819bc
chore: exclude native implementation from coverage report due to comp…
deki c30b197
Address PR comments and add sample which also addresses most of the P…
olegz 0b9384f
Fix gradle file name
olegz 8f0fc79
feat: native Spring Web workloads (#335) - consistent naming
deki 1bd5f72
feat: native Spring Web workloads (#335) - polishing
deki 227b2a7
feat: native Spring Web workloads (#335) - use newer dependency versi…
deki 01606c1
feat: native Spring Web workloads (#335) - removed System.out.println…
deki 4ff8c41
feat: native Spring Web workloads (#335) - update sample to Amazon Li…
deki 9be742a
feat: native Spring Web workloads (#335) - update README for sample
deki b4add79
Merge remote-tracking branch 'origin/main' into native
deki cb17885
feat: native Spring Web workloads (#335) - use latest runtime version
deki 3ef9aab
feat: native Spring Web workloads (#335) - native and non-native Hand…
deki c21a826
feat: native Spring Web workloads (#335) - add -march=compatibility t…
deki 4533c4b
feat: native Spring Web workloads (#335) - use spring-cloud-function-…
deki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
...ntainer-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AWSHttpUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package com.amazonaws.serverless.proxy.spring; | ||
|
||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
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.ServerlessHttpServletRequest; | ||
import org.springframework.cloud.function.serverless.web.ServerlessMVC; | ||
import org.springframework.util.FileCopyUtils; | ||
import org.springframework.util.MultiValueMapAdapter; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.amazonaws.serverless.proxy.AwsHttpApiV2SecurityContextWriter; | ||
import com.amazonaws.serverless.proxy.AwsProxySecurityContextWriter; | ||
import com.amazonaws.serverless.proxy.RequestReader; | ||
import com.amazonaws.serverless.proxy.SecurityContextWriter; | ||
import com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse; | ||
import com.amazonaws.serverless.proxy.internal.servlet.AwsProxyHttpServletResponseWriter; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyRequest; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyResponse; | ||
import com.amazonaws.serverless.proxy.model.HttpApiV2ProxyRequest; | ||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import jakarta.servlet.ServletContext; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
public class AWSHttpUtils { | ||
|
||
private static Log logger = LogFactory.getLog(AWSWebRuntimeEventLoop.class); | ||
olegz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
public static AwsProxyResponse serviceAWS(String gatewayEvent, ServerlessMVC mvc, ObjectMapper mapper, AwsProxyHttpServletResponseWriter responseWriter) { | ||
olegz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HttpServletRequest request = AWSHttpUtils.generateHttpServletRequest(gatewayEvent, null, mvc.getServletContext(), mapper); | ||
CountDownLatch latch = new CountDownLatch(1); | ||
AwsHttpServletResponse response = new AwsHttpServletResponse(request, latch); | ||
try { | ||
mvc.service(request, response); | ||
latch.await(10, TimeUnit.SECONDS); | ||
olegz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AwsProxyResponse awsResponse = responseWriter.writeResponse(response, null); | ||
return awsResponse; | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
public static String extractVersion() { | ||
try { | ||
String path = AWSHttpUtils.class.getProtectionDomain().getCodeSource().getLocation().toString(); | ||
int endIndex = path.lastIndexOf('.'); | ||
if (endIndex < 0) { | ||
return "UNKNOWN-VERSION"; | ||
} | ||
int startIndex = path.lastIndexOf("/") + 1; | ||
return path.substring(startIndex, endIndex).replace("spring-cloud-function-serverless-web-", ""); | ||
} | ||
catch (Exception e) { | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Failed to detect version", e); | ||
} | ||
return "UNKNOWN-VERSION"; | ||
} | ||
|
||
} | ||
|
||
public static HttpServletRequest generateHttpServletRequest(InputStream jsonRequest, Context lambdaContext, | ||
ServletContext servletContext, ObjectMapper mapper) { | ||
try { | ||
String text = new String(FileCopyUtils.copyToByteArray(jsonRequest), StandardCharsets.UTF_8); | ||
if (logger.isDebugEnabled()) { | ||
logger.debug("Creating HttpServletRequest from: " + text); | ||
} | ||
return generateHttpServletRequest(text, lambdaContext, servletContext, mapper); | ||
} catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public static HttpServletRequest generateHttpServletRequest(String jsonRequest, Context lambdaContext, | ||
ServletContext servletContext, ObjectMapper mapper) { | ||
Map<String, Object> _request = readValue(jsonRequest, Map.class, mapper); | ||
SecurityContextWriter securityWriter = "2.0".equals(_request.get("version")) | ||
? new AwsHttpApiV2SecurityContextWriter() | ||
: new AwsProxySecurityContextWriter(); | ||
HttpServletRequest httpServletRequest = "2.0".equals(_request.get("version")) | ||
? AWSHttpUtils.generateRequest2(jsonRequest, lambdaContext, securityWriter, mapper, servletContext) | ||
: AWSHttpUtils.generateRequest1(jsonRequest, lambdaContext, securityWriter, mapper, servletContext); | ||
return httpServletRequest; | ||
} | ||
|
||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
private static HttpServletRequest generateRequest1(String request, Context lambdaContext, | ||
SecurityContextWriter securityWriter, ObjectMapper mapper, ServletContext servletContext) { | ||
AwsProxyRequest v1Request = readValue(request, AwsProxyRequest.class, mapper); | ||
|
||
ServerlessHttpServletRequest httpRequest = new ServerlessHttpServletRequest(servletContext, v1Request.getHttpMethod(), v1Request.getPath()); | ||
if (v1Request.getMultiValueHeaders() != null) { | ||
MultiValueMapAdapter headers = new MultiValueMapAdapter(v1Request.getMultiValueHeaders()); | ||
httpRequest.setHeaders(headers); | ||
} | ||
if (StringUtils.hasText(v1Request.getBody())) { | ||
httpRequest.setContentType("application/json"); | ||
httpRequest.setContent(v1Request.getBody().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
if (v1Request.getRequestContext() != null) { | ||
httpRequest.setAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY, v1Request.getRequestContext()); | ||
httpRequest.setAttribute(RequestReader.ALB_CONTEXT_PROPERTY, v1Request.getRequestContext().getElb()); | ||
} | ||
httpRequest.setAttribute(RequestReader.API_GATEWAY_STAGE_VARS_PROPERTY, v1Request.getStageVariables()); | ||
httpRequest.setAttribute(RequestReader.API_GATEWAY_EVENT_PROPERTY, v1Request); | ||
httpRequest.setAttribute(RequestReader.LAMBDA_CONTEXT_PROPERTY, lambdaContext); | ||
httpRequest.setAttribute(RequestReader.JAX_SECURITY_CONTEXT_PROPERTY, | ||
securityWriter.writeSecurityContext(v1Request, lambdaContext)); | ||
return httpRequest; | ||
} | ||
|
||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
private static HttpServletRequest generateRequest2(String request, Context lambdaContext, | ||
SecurityContextWriter securityWriter, ObjectMapper mapper, ServletContext servletContext) { | ||
HttpApiV2ProxyRequest v2Request = readValue(request, HttpApiV2ProxyRequest.class, mapper); | ||
ServerlessHttpServletRequest httpRequest = new ServerlessHttpServletRequest(servletContext, | ||
v2Request.getRequestContext().getHttp().getMethod(), v2Request.getRequestContext().getHttp().getPath()); | ||
|
||
v2Request.getHeaders().forEach((k,v) -> httpRequest.setHeader(k, v)); | ||
|
||
if (StringUtils.hasText(v2Request.getBody())) { | ||
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()); | ||
httpRequest.setAttribute(RequestReader.HTTP_API_EVENT_PROPERTY, v2Request); | ||
httpRequest.setAttribute(RequestReader.LAMBDA_CONTEXT_PROPERTY, lambdaContext); | ||
httpRequest.setAttribute(RequestReader.JAX_SECURITY_CONTEXT_PROPERTY, | ||
securityWriter.writeSecurityContext(v2Request, lambdaContext)); | ||
return httpRequest; | ||
} | ||
|
||
private static <T> T readValue(String json, Class<T> clazz, ObjectMapper mapper) { | ||
try { | ||
return mapper.readValue(json, clazz); | ||
} | ||
catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
// public static class ProxyServletConfig implements ServletConfig { | ||
olegz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// private final ServletContext servletContext; | ||
// | ||
// public ProxyServletConfig(ServletContext servletContext) { | ||
// this.servletContext = servletContext; | ||
// } | ||
// | ||
// @Override | ||
// public String getServletName() { | ||
// return DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME; | ||
// } | ||
// | ||
// @Override | ||
// public ServletContext getServletContext() { | ||
// return this.servletContext; | ||
// } | ||
// | ||
// @Override | ||
// public Enumeration<String> getInitParameterNames() { | ||
// return Collections.enumeration(new ArrayList<String>()); | ||
// } | ||
// | ||
// @Override | ||
// public String getInitParameter(String name) { | ||
// return null; | ||
// } | ||
// } | ||
} |
76 changes: 76 additions & 0 deletions
76
...er-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AWSTypesProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2024-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License 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 org.springframework.aot.generate.GenerationContext; | ||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.RuntimeHints; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
|
||
import com.amazonaws.serverless.proxy.internal.servlet.AwsHttpServletResponse; | ||
import com.amazonaws.serverless.proxy.model.ApiGatewayRequestIdentity; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyRequest; | ||
import com.amazonaws.serverless.proxy.model.AwsProxyRequestContext; | ||
import com.amazonaws.serverless.proxy.model.Headers; | ||
import com.amazonaws.serverless.proxy.model.MultiValuedTreeMap; | ||
import com.amazonaws.serverless.proxy.model.SingleValueHeaders; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
|
||
/** | ||
* AOT Initialization processor required to register reflective hints for GraalVM. | ||
* This is necessary to ensure proper JSON serialization/deserialization. | ||
* It is registered with META-INF/spring/aot.factories | ||
* | ||
* @author Oleg Zhurakousky | ||
*/ | ||
public class AWSTypesProcessor implements BeanFactoryInitializationAotProcessor { | ||
olegz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { | ||
return new ReflectiveProcessorBeanFactoryInitializationAotContribution(); | ||
} | ||
|
||
private static final class ReflectiveProcessorBeanFactoryInitializationAotContribution implements BeanFactoryInitializationAotContribution { | ||
@Override | ||
public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) { | ||
RuntimeHints runtimeHints = generationContext.getRuntimeHints(); | ||
// known static types | ||
|
||
runtimeHints.reflection().registerType(AwsProxyRequest.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES); | ||
runtimeHints.reflection().registerType(SingleValueHeaders.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES); | ||
runtimeHints.reflection().registerType(JsonToken.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES); | ||
runtimeHints.reflection().registerType(MultiValuedTreeMap.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES); | ||
runtimeHints.reflection().registerType(Headers.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES); | ||
runtimeHints.reflection().registerType(AwsProxyRequestContext.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES); | ||
runtimeHints.reflection().registerType(ApiGatewayRequestIdentity.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES); | ||
runtimeHints.reflection().registerType(AwsHttpServletResponse.class, | ||
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, | ||
MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_CLASSES, MemberCategory.INTROSPECT_DECLARED_METHODS); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.