DATAREST-448 - RepositoryRestHandlerMapping is now checking for a repository mapping again.

Re-introduced the accidentally removed checks that the resolved handler method actually points to a repository resource.
This commit is contained in:
Oliver Gierke
2015-01-16 14:36:53 +01:00
parent c3ffe7793b
commit 65f45d6433
3 changed files with 44 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.webmvc.support.JpaHelper;
import org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@@ -88,7 +89,14 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
return null;
}
return new BaseUri(configuration.getBaseUri()).getRepositoryLookupPath(lookupPath) == null ? null : handlerMethod;
String repositoryLookupPath = new BaseUri(configuration.getBaseUri()).getRepositoryLookupPath(lookupPath);
// Repository root resource
if (!StringUtils.hasText(repositoryLookupPath)) {
return handlerMethod;
}
return mappings.exportsTopLevelResourceFor(getRepositoryBasePath(repositoryLookupPath)) ? handlerMethod : null;
}
/*
@@ -122,4 +130,16 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {
}
}
}
/**
* Returns the first segment of the given repository lookup path.
*
* @param repositoryLookupPath must not be {@literal null}.
* @return
*/
private static String getRepositoryBasePath(String repositoryLookupPath) {
int secondSlashIndex = repositoryLookupPath.indexOf('/', repositoryLookupPath.startsWith("/") ? 1 : 0);
return secondSlashIndex == -1 ? repositoryLookupPath : repositoryLookupPath.substring(0, secondSlashIndex);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,11 +37,11 @@ import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilde
import com.jayway.jsonpath.JsonPath;
/**
* This class contains a common test suite used to verify multiple data stores with the same domain space.
* When verifying support of a new data store, it's good to start with extending this suite of tests. However,
* if the data store doesn't map well onto this, then a good alternative would be write a new test suite
* using {@link org.springframework.data.rest.webmvc.AbstractWebIntegrationTests AbstractWebIntegrationTests} as
* the test harness.
* This class contains a common test suite used to verify multiple data stores with the same domain space. When
* verifying support of a new data store, it's good to start with extending this suite of tests. However, if the data
* store doesn't map well onto this, then a good alternative would be write a new test suite using
* {@link org.springframework.data.rest.webmvc.AbstractWebIntegrationTests AbstractWebIntegrationTests} as the test
* harness.
*
* @author Oliver Gierke
* @author Greg Turnquist
@@ -198,4 +198,13 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests {
andExpect(content().contentType(ALPS_MEDIA_TYPE));
}
/**
* @see DATAREST-448
*/
@Test
public void returnsNotFoundForUriNotBackedByARepository() throws Exception {
mvc.perform(get("/index.html")).//
andExpect(status().isNotFound());
}
}

View File

@@ -100,7 +100,7 @@ public class RepositoryRestHandlerMappingUnitTests {
@Test
public void looksUpRepositoryEntityControllerMethodCorrectly() throws Exception {
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", "/people");
handlerMapping.afterPropertiesSet();
@@ -116,7 +116,7 @@ public class RepositoryRestHandlerMappingUnitTests {
@Test
public void returnsRepositoryHandlerMethodWithBaseUriConfigured() throws Exception {
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", "/base/people");
configuration.setBasePath("/base");
@@ -134,7 +134,7 @@ public class RepositoryRestHandlerMappingUnitTests {
@Test
public void returnsRootHandlerMethodWithBaseUriConfigured() throws Exception {
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", "/base");
configuration.setBasePath("/base");
@@ -153,7 +153,7 @@ public class RepositoryRestHandlerMappingUnitTests {
@SuppressWarnings("deprecation")
public void returnsRepositoryHandlerMethodForAbsoluteBaseUri() throws Exception {
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", "/base/people/");
configuration.setBaseUri("http://localhost/base");
@@ -172,7 +172,7 @@ public class RepositoryRestHandlerMappingUnitTests {
@SuppressWarnings("deprecation")
public void returnsRepositoryHandlerMethodForAbsoluteBaseUriWithServletMapping() throws Exception {
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", "/base/people");
mockRequest.setServletPath("/base/people");
@@ -192,7 +192,7 @@ public class RepositoryRestHandlerMappingUnitTests {
@SuppressWarnings("deprecation")
public void refrainsFromMappingIfTheRequestDoesNotPointIntoAbsolutelyDefinedUriSpace() throws Exception {
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", "/servlet-path");
mockRequest.setServletPath("/servlet-path");
@@ -213,7 +213,7 @@ public class RepositoryRestHandlerMappingUnitTests {
String baseUri = "foo";
String uri = baseUri.concat("/people");
when(mappings.exportsTopLevelResourceFor("people")).thenReturn(true);
when(mappings.exportsTopLevelResourceFor("/people")).thenReturn(true);
mockRequest = new MockHttpServletRequest("GET", uri);
mockRequest.setServletPath(uri);