diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletResponseWriter.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletResponseWriter.java index ba98eedb2..de84b5c2d 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletResponseWriter.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletResponseWriter.java @@ -19,11 +19,14 @@ import com.amazonaws.serverless.proxy.internal.testutils.Timer; import com.amazonaws.serverless.proxy.model.AwsProxyRequest; import com.amazonaws.serverless.proxy.model.AwsProxyResponse; +import com.amazonaws.serverless.proxy.model.Headers; import com.amazonaws.services.lambda.runtime.Context; import javax.ws.rs.core.Response; import java.util.Base64; +import java.util.HashMap; +import java.util.Map; /** @@ -32,6 +35,16 @@ */ public class AwsProxyHttpServletResponseWriter extends ResponseWriter { + private boolean writeSingleValueHeaders; + + public AwsProxyHttpServletResponseWriter() { + this(false); + } + + public AwsProxyHttpServletResponseWriter(boolean singleValueHeaders) { + writeSingleValueHeaders = singleValueHeaders; + } + //------------------------------------------------------------- // Methods - Implementation //------------------------------------------------------------- @@ -47,13 +60,16 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse, if (!isBinary(containerResponse.getContentType()) && isValidUtf8(containerResponse.getAwsResponseBodyBytes())) { responseString = containerResponse.getAwsResponseBodyString(); } else { - responseString = Base64.getMimeEncoder().encodeToString(containerResponse.getAwsResponseBodyBytes()); + responseString = Base64.getEncoder().encodeToString(containerResponse.getAwsResponseBodyBytes()); awsProxyResponse.setBase64Encoded(true); } awsProxyResponse.setBody(responseString); } awsProxyResponse.setMultiValueHeaders(containerResponse.getAwsResponseHeaders()); + if (writeSingleValueHeaders) { + awsProxyResponse.setHeaders(toSingleValueHeaders(containerResponse.getAwsResponseHeaders())); + } awsProxyResponse.setStatusCode(containerResponse.getStatus()); @@ -65,6 +81,17 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse, return awsProxyResponse; } + private Map toSingleValueHeaders(Headers h) { + Map out = new HashMap<>(); + if (h == null || h.isEmpty()) { + return out; + } + for (String k : h.keySet()) { + out.put(k, h.getFirst(k)); + } + return out; + } + private boolean isBinary(String contentType) { if(contentType != null) { int semidx = contentType.indexOf(';'); diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java index 88e59fb61..caead2aaf 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/ServletLambdaContainerHandlerBuilder.java @@ -110,7 +110,7 @@ public Builder defaultProxy() { public Builder defaultHttpApiV2Proxy() { initializationWrapper(new InitializationWrapper()) .requestReader((RequestReader) new AwsHttpApiV2HttpServletRequestReader()) - .responseWriter((ResponseWriter) new AwsProxyHttpServletResponseWriter()) + .responseWriter((ResponseWriter) new AwsProxyHttpServletResponseWriter(true)) .securityContextWriter((SecurityContextWriter) new AwsHttpApiV2SecurityContextWriter()) .exceptionHandler((ExceptionHandler) new AwsProxyExceptionHandler()) .requestTypeClass((Class) HttpApiV2ProxyRequest.class) diff --git a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java index bb9d95091..fe50cd0fd 100644 --- a/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java +++ b/aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyLambdaContainerHandler.java @@ -116,7 +116,7 @@ public static JerseyLambdaContainerHandler newHandler = new SparkLambdaContainerHandler<>(HttpApiV2ProxyRequest.class, AwsProxyResponse.class, new AwsHttpApiV2HttpServletRequestReader(), - new AwsProxyHttpServletResponseWriter(), + new AwsProxyHttpServletResponseWriter(true), new AwsHttpApiV2SecurityContextWriter(), new AwsProxyExceptionHandler(), new LambdaEmbeddedServerFactory()); diff --git a/aws-serverless-java-container-springboot2/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java b/aws-serverless-java-container-springboot2/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java index de2228434..7c80dc86b 100644 --- a/aws-serverless-java-container-springboot2/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java +++ b/aws-serverless-java-container-springboot2/src/test/java/com/amazonaws/serverless/proxy/spring/ServletAppTest.java @@ -23,8 +23,7 @@ import java.util.Collection; import java.util.stream.Collectors; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; @RunWith(Parameterized.class) public class ServletAppTest { @@ -192,4 +191,18 @@ public void springExceptionMapping_throw404Ex_expectMappedTo404() { assertNotNull(resp); assertEquals(404, resp.getStatusCode()); } + + @Test + public void echoMessage_populatesSingleValueHeadersForHttpApiV2() { + AwsProxyRequestBuilder req = new AwsProxyRequestBuilder("/message", "POST") + .header(HttpHeaders.CONTENT_TYPE, "application/json;v=1") + .header(HttpHeaders.ACCEPT, "application/json;v=1") + .body(new MessageData("test message")); + AwsProxyResponse resp = handler.handleRequest(req, lambdaContext); + if ("HTTP_API".equals(type)) { + assertNotNull(resp.getHeaders()); + } else { + assertNull(resp.getHeaders()); + } + } } diff --git a/aws-serverless-java-container-struts2/src/main/java/com/amazonaws/serverless/proxy/struts2/Struts2LambdaContainerHandler.java b/aws-serverless-java-container-struts2/src/main/java/com/amazonaws/serverless/proxy/struts2/Struts2LambdaContainerHandler.java index 3a536c9b2..99c89c449 100644 --- a/aws-serverless-java-container-struts2/src/main/java/com/amazonaws/serverless/proxy/struts2/Struts2LambdaContainerHandler.java +++ b/aws-serverless-java-container-struts2/src/main/java/com/amazonaws/serverless/proxy/struts2/Struts2LambdaContainerHandler.java @@ -65,7 +65,7 @@ public static Struts2LambdaContainerHandler