Skip to content

Commit

Permalink
Merge pull request #113 from awslabs/jersey
Browse files Browse the repository at this point in the history
Fix routing issue with Jersey when stripBasePath is configured (addresses #112)
  • Loading branch information
sapessi authored Jan 31, 2018
2 parents 5fbba8e + 68658d8 commit 805ccbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,10 @@ public void destroy() {
@SuppressFBWarnings({ "SERVLET_HEADER", "SERVLET_QUERY_STRING" })
private ContainerRequest servletRequestToContainerRequest(ServletRequest request) {
Timer.start("JERSEY_SERVLET_REQUEST_TO_CONTAINER");
URI basePathUri;
URI requestPathUri;
String basePath = "/";
HttpServletRequest servletRequest = (HttpServletRequest)request;

try {
if (servletRequest.getContextPath().equals("")) {
basePathUri = URI.create(basePath);
} else {
basePathUri = new URI(servletRequest.getContextPath());
}
} catch (URISyntaxException e) {
log.error("Could not read base path URI", e);
basePathUri = URI.create(basePath);
}

UriBuilder uriBuilder = UriBuilder.fromPath(servletRequest.getPathInfo());
uriBuilder.replaceQuery(AwsProxyHttpServletRequest.decodeValueIfEncoded(servletRequest.getQueryString()));

Expand All @@ -125,7 +113,7 @@ private ContainerRequest servletRequestToContainerRequest(ServletRequest request
apiGatewayProperties.setProperty(JERSEY_SERVLET_REQUEST_PROPERTY, servletRequest);

ContainerRequest requestContext = new ContainerRequest(
basePathUri,
URI.create(basePath), // for routing within Jersey we always assume the base path is "/"
requestPathUri,
servletRequest.getMethod().toUpperCase(Locale.ENGLISH),
(SecurityContext)servletRequest.getAttribute(JAX_SECURITY_CONTEXT_PROPERTY),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,30 @@ public void exception_mapException_mapToNotImplemented() {
assertEquals(Response.Status.NOT_IMPLEMENTED.getStatusCode(), response.getStatusCode());
}

@Test
public void stripBasePath_route_shouldRouteCorrectly() {
AwsProxyRequest request = new AwsProxyRequestBuilder("/custompath/echo/status-code", "GET")
.json()
.queryString("status", "201")
.build();
handler.stripBasePath("/custompath");
AwsProxyResponse output = handler.proxy(request, lambdaContext);
assertEquals(201, output.getStatusCode());
handler.stripBasePath("");
}

@Test
public void stripBasePath_route_shouldReturn404() {
AwsProxyRequest request = new AwsProxyRequestBuilder("/custompath/echo/status-code", "GET")
.json()
.queryString("status", "201")
.build();
handler.stripBasePath("/custom");
AwsProxyResponse output = handler.proxy(request, lambdaContext);
assertEquals(404, output.getStatusCode());
handler.stripBasePath("");
}

private void validateMapResponseModel(AwsProxyResponse output) {
validateMapResponseModel(output, CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE);
}
Expand Down

0 comments on commit 805ccbd

Please sign in to comment.