From 65f45d6433d7a3fa31143945a8136993c4669dd4 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 16 Jan 2015 14:36:53 +0100 Subject: [PATCH] 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. --- .../webmvc/RepositoryRestHandlerMapping.java | 24 +++++++++++++++++-- .../data/rest/webmvc/CommonWebTests.java | 21 +++++++++++----- ...RepositoryRestHandlerMappingUnitTests.java | 14 +++++------ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java index d671fefd5..6c3e42366 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java @@ -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); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java index ef6a41446..0fbc6049f 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java @@ -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()); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java index c96c0e38c..fc26646d7 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMappingUnitTests.java @@ -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);