Skip to content

Commit

Permalink
Release 1.5.2 (#385)
Browse files Browse the repository at this point in the history
* fix: Use single value headers for HTTP API response (#377)

Parametrized the use of the single value headers for the response writer to support the v2 proxy schema for HTTP API

* fix: Updated HTTP API handler constructor (#377)

Changed default httpApiV2 constructor for the handler in all the framework implementations

* fix: Switched to non-mime encoder for respponse (#339)
  • Loading branch information
sapessi authored Oct 6, 2020
1 parent edf11fe commit 5c4ca2b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -32,6 +35,16 @@
*/
public class AwsProxyHttpServletResponseWriter extends ResponseWriter<AwsHttpServletResponse, AwsProxyResponse> {

private boolean writeSingleValueHeaders;

public AwsProxyHttpServletResponseWriter() {
this(false);
}

public AwsProxyHttpServletResponseWriter(boolean singleValueHeaders) {
writeSingleValueHeaders = singleValueHeaders;
}

//-------------------------------------------------------------
// Methods - Implementation
//-------------------------------------------------------------
Expand All @@ -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());

Expand All @@ -65,6 +81,17 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse,
return awsProxyResponse;
}

private Map<String, String> toSingleValueHeaders(Headers h) {
Map<String, String> 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(';');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Builder defaultProxy() {
public Builder defaultHttpApiV2Proxy() {
initializationWrapper(new InitializationWrapper())
.requestReader((RequestReader<RequestType, ContainerRequestType>) new AwsHttpApiV2HttpServletRequestReader())
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter())
.responseWriter((ResponseWriter<AwsHttpServletResponse, ResponseType>) new AwsProxyHttpServletResponseWriter(true))
.securityContextWriter((SecurityContextWriter<RequestType>) new AwsHttpApiV2SecurityContextWriter())
.exceptionHandler((ExceptionHandler<ResponseType>) new AwsProxyExceptionHandler())
.requestTypeClass((Class<RequestType>) HttpApiV2ProxyRequest.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static JerseyLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyRespon
HttpApiV2ProxyRequest.class,
AwsProxyResponse.class,
new AwsHttpApiV2HttpServletRequestReader(),
new AwsProxyHttpServletResponseWriter(),
new AwsProxyHttpServletResponseWriter(true),
new AwsHttpApiV2SecurityContextWriter(),
new AwsProxyExceptionHandler(),
jaxRsApplication);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static SparkLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyRespons
SparkLambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyResponse> newHandler = new SparkLambdaContainerHandler<>(HttpApiV2ProxyRequest.class,
AwsProxyResponse.class,
new AwsHttpApiV2HttpServletRequestReader(),
new AwsProxyHttpServletResponseWriter(),
new AwsProxyHttpServletResponseWriter(true),
new AwsHttpApiV2SecurityContextWriter(),
new AwsProxyExceptionHandler(),
new LambdaEmbeddedServerFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static Struts2LambdaContainerHandler<HttpApiV2ProxyRequest, AwsProxyRespo
HttpApiV2ProxyRequest.class,
AwsProxyResponse.class,
new AwsHttpApiV2HttpServletRequestReader(),
new AwsProxyHttpServletResponseWriter(),
new AwsProxyHttpServletResponseWriter(true),
new AwsHttpApiV2SecurityContextWriter(),
new AwsProxyExceptionHandler());
}
Expand Down

0 comments on commit 5c4ca2b

Please sign in to comment.