Skip to content

Commit 8ee206e

Browse files
committed
Switched jersey request reader to use a instead of instantiating a new URI directly. This addresses issue #59
1 parent 0dcfa36 commit 8ee206e

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

aws-serverless-java-container-core/pom.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@
7373
<version>4.5.3</version>
7474
<scope>test</scope>
7575
</dependency>
76-
77-
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
78-
<dependency>
79-
<groupId>org.slf4j</groupId>
80-
<artifactId>slf4j-simple</artifactId>
81-
<version>1.7.25</version>
82-
<scope>test</scope>
83-
</dependency>
8476
</dependencies>
8577

8678
</project>

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/AwsProxyExceptionHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class AwsProxyExceptionHandler
7373

7474
@Override
7575
public AwsProxyResponse handle(Throwable ex) {
76+
log.error("Called exception handler for:", ex);
7677
if (ex instanceof InvalidRequestEventException) {
7778
return new AwsProxyResponse(500, headers, getErrorJson(INTERNAL_SERVER_ERROR));
7879
} else {

aws-serverless-java-container-jersey/src/main/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyRequestReader.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
import org.glassfish.jersey.internal.MapPropertiesDelegate;
2222
import org.glassfish.jersey.internal.PropertiesDelegate;
2323
import org.glassfish.jersey.server.ContainerRequest;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2426

2527
import javax.ws.rs.core.SecurityContext;
28+
import javax.ws.rs.core.UriBuilder;
29+
2630
import java.io.ByteArrayInputStream;
2731
import java.net.URI;
2832
import java.net.URISyntaxException;
@@ -47,6 +51,8 @@ public class JerseyAwsProxyRequestReader extends RequestReader<AwsProxyRequest,
4751
private static AwsProxyRequest currentRequest;
4852
private static Context currentLambdaContext;
4953

54+
private Logger log = LoggerFactory.getLogger(JerseyAwsProxyRequestReader.class);
55+
5056

5157
//-------------------------------------------------------------
5258
// Methods - Implementation
@@ -78,15 +84,21 @@ public ContainerRequest readRequest(AwsProxyRequest request, SecurityContext sec
7884
try {
7985
basePathUri = new URI(basePath);
8086
} catch (URISyntaxException e) {
87+
log.error("Could not read base path URI", e);
8188
throw new InvalidRequestEventException("Error while generating base path URI: " + basePath, e);
8289
}
8390

84-
try {
85-
requestPathUri = new URI(request.getQueryString() == null ? request.getPath() : request.getPath() + request.getQueryString());
86-
} catch (URISyntaxException e) {
87-
throw new InvalidRequestEventException("Error while generating request path URI: " + request.getPath(), e);
91+
92+
UriBuilder uriBuilder = UriBuilder.fromPath(request.getPath());
93+
94+
if (request.getQueryStringParameters() != null) {
95+
for (String paramKey : request.getQueryStringParameters().keySet()) {
96+
uriBuilder = uriBuilder.queryParam(paramKey, request.getQueryStringParameters().get(paramKey));
97+
}
8898
}
8999

100+
requestPathUri = uriBuilder.build();
101+
90102
PropertiesDelegate apiGatewayProperties = new MapPropertiesDelegate();
91103
apiGatewayProperties.setProperty(API_GATEWAY_CONTEXT_PROPERTY, request.getRequestContext());
92104
apiGatewayProperties.setProperty(API_GATEWAY_STAGE_VARS_PROPERTY, request.getStageVariables());

aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public class JerseyAwsProxyTest {
4747
private static final String CUSTOM_HEADER_KEY = "x-custom-header";
4848
private static final String CUSTOM_HEADER_VALUE = "my-custom-value";
4949
private static final String AUTHORIZER_PRINCIPAL_ID = "test-principal-" + UUID.randomUUID().toString();
50+
private static final String QUERY_STRING_KEY = "identifier";
51+
private static final String QUERY_STRING_NON_ENCODED_VALUE = "Space Test";
52+
private static final String QUERY_STRING_ENCODED_VALUE = "Space%20Test";
5053

5154

5255
private static ObjectMapper objectMapper = new ObjectMapper();
@@ -118,6 +121,34 @@ public void queryString_uriInfo_echo() {
118121
validateMapResponseModel(output);
119122
}
120123

124+
@Test
125+
public void queryString_notEncoded_echo() {
126+
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/query-string", "GET")
127+
.json()
128+
.queryString(QUERY_STRING_KEY, QUERY_STRING_NON_ENCODED_VALUE)
129+
.build();
130+
131+
AwsProxyResponse output = handler.proxy(request, lambdaContext);
132+
assertEquals(200, output.getStatusCode());
133+
assertEquals("application/json", output.getHeaders().get("Content-Type"));
134+
135+
validateMapResponseModel(output, QUERY_STRING_KEY, QUERY_STRING_NON_ENCODED_VALUE);
136+
}
137+
138+
@Test
139+
public void queryString_encoded_echo() {
140+
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/query-string", "GET")
141+
.json()
142+
.queryString(QUERY_STRING_KEY, QUERY_STRING_ENCODED_VALUE)
143+
.build();
144+
145+
AwsProxyResponse output = handler.proxy(request, lambdaContext);
146+
assertEquals(200, output.getStatusCode());
147+
assertEquals("application/json", output.getHeaders().get("Content-Type"));
148+
149+
validateMapResponseModel(output, QUERY_STRING_KEY, QUERY_STRING_NON_ENCODED_VALUE);
150+
}
151+
121152
@Test
122153
public void authorizer_securityContext_customPrincipalSuccess() {
123154
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/authorizer-principal", "GET")
@@ -215,10 +246,14 @@ public void base64_binaryResponse_base64Encoding() {
215246
}
216247

217248
private void validateMapResponseModel(AwsProxyResponse output) {
249+
validateMapResponseModel(output, CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE);
250+
}
251+
252+
private void validateMapResponseModel(AwsProxyResponse output, String key, String value) {
218253
try {
219254
MapResponseModel response = objectMapper.readValue(output.getBody(), MapResponseModel.class);
220-
assertNotNull(response.getValues().get(CUSTOM_HEADER_KEY));
221-
assertEquals(CUSTOM_HEADER_VALUE, response.getValues().get(CUSTOM_HEADER_KEY));
255+
assertNotNull(response.getValues().get(key));
256+
assertEquals(value, response.getValues().get(key));
222257
} catch (IOException e) {
223258
fail("Exception while parsing response body: " + e.getMessage());
224259
e.printStackTrace();

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@
4646
<artifactId>slf4j-api</artifactId>
4747
<version>1.7.25</version>
4848
</dependency>
49+
50+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
51+
<dependency>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>slf4j-simple</artifactId>
54+
<version>1.7.25</version>
55+
<scope>test</scope>
56+
</dependency>
4957
</dependencies>
5058

5159
<build>

0 commit comments

Comments
 (0)