DATAREST-276 - Fixed ArrayIndexOutOfBoundsException in RepositoryRestHandlerMapping.

We now make sure the RepositoryRestHandlerMapping correctly refrains from handling the request in case the URI doesn't match the configured base URI.
This commit is contained in:
Oliver Gierke
2014-04-30 13:36:54 +02:00
parent b844b95a27
commit e5dbc30d8a
2 changed files with 53 additions and 12 deletions

View File

@@ -113,14 +113,13 @@ public class RepositoryRestHandlerMapping extends RequestMappingHandlerMapping {
acceptType = config.getDefaultMediaType().toString();
}
HttpServletRequest request = new DefaultAcceptTypeHttpServletRequest(origRequest, acceptType);
String uri = extractRepositoryLookupPath(lookupPath, config.getBaseUri());
if (!hasText(lookupPath)) {
return super.lookupHandlerMethod(lookupPath, request);
if (uri == null) {
return null;
}
String uri = extractRepositoryLookupPath(lookupPath, config.getBaseUri());
request = new DefaultAcceptTypeHttpServletRequest(request, acceptType, uri);
HttpServletRequest request = new DefaultAcceptTypeHttpServletRequest(origRequest, acceptType, uri);
// Root request
if (!StringUtils.hasText(uri) || uri.equals("/")) {
@@ -129,7 +128,7 @@ public class RepositoryRestHandlerMapping extends RequestMappingHandlerMapping {
String[] parts = uri.split("/");
if (mappings.exportsTopLevelResourceFor(parts[1])) {
if (mappings.exportsTopLevelResourceFor(parts[uri.startsWith("/") ? 1 : 0])) {
return super.lookupHandlerMethod(uri, request);
}
@@ -142,7 +141,8 @@ public class RepositoryRestHandlerMapping extends RequestMappingHandlerMapping {
*
* @param lookupPath must not be {@literal null}.
* @param baseUri must not be {@literal null}.
* @return
* @return the stripped lookup path with then the repository URI space or {@literal null} in case the lookup path is
* not pointing into the repository URI space.
*/
private static String extractRepositoryLookupPath(String lookupPath, URI baseUri) {
@@ -160,7 +160,7 @@ public class RepositoryRestHandlerMapping extends RequestMappingHandlerMapping {
}
uri = uri.startsWith("/") ? uri : "/".concat(uri);
return lookupPath.substring(uri.length(), lookupPath.length());
return lookupPath.startsWith(uri) ? lookupPath.substring(uri.length(), lookupPath.length()) : null;
}
List<String> baseUriSegments = UriComponentsBuilder.fromUri(baseUri).build().getPathSegments();
@@ -176,7 +176,7 @@ public class RepositoryRestHandlerMapping extends RequestMappingHandlerMapping {
}
}
return lookupPath;
return null;
}
/*

View File

@@ -163,16 +163,57 @@ public class RepositoryRestHandlerMappingUnitTests {
public void returnsRepositoryHandlerMethodForAbsoluteBaseUriWithServletMapping() throws Exception {
String baseUri = "http://localhost/base";
String uri = baseUri.concat("/people/");
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", baseUri.concat("/people/"));
mockRequest.setServletPath(baseUri.concat("/people/"));
mockRequest = new MockHttpServletRequest("GET", uri);
mockRequest.setServletPath(uri);
configuration.setBaseUri(URI.create(baseUri));
HandlerMethod method = handlerMapping.lookupHandlerMethod("/people/", mockRequest);
HandlerMethod method = handlerMapping.lookupHandlerMethod("/base/people/", mockRequest);
assertThat(method, is(notNullValue()));
assertThat(method.getMethod(), is(listEntitiesMethod));
}
/**
* @see DATAREST-276
*/
@Test
public void refrainsFromMappingIfTheRequestDoesNotPointIntoAbsolutelyDefinedUriSpace() throws Exception {
String baseUri = "http://localhost/base";
String uri = baseUri.concat("/people/");
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", uri);
mockRequest.setServletPath(uri);
configuration.setBaseUri(URI.create(baseUri));
HandlerMethod method = handlerMapping.lookupHandlerMethod("/people", mockRequest);
assertThat(method, is(nullValue()));
}
/**
* @see DATAREST-276
*/
@Test
public void refrainsFromMappingWhenUrisDontMatch() throws Exception {
String baseUri = "foo";
String uri = baseUri.concat("/people");
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", uri);
mockRequest.setServletPath(uri);
configuration.setBaseUri(URI.create(baseUri));
HandlerMethod method = handlerMapping.lookupHandlerMethod("/people", mockRequest);
assertThat(method, is(nullValue()));
}
}