Author: kwin Date: Wed Jan 14 17:42:50 2015 New Revision: 1651744 URL: http://svn.apache.org/r1651744 Log: SLING-4262 always convert resource types to relative ones fix bug in overlay handling when search paths where ending with a slash
Modified: sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java Modified: sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java?rev=1651744&r1=1651743&r2=1651744&view=diff ============================================================================== --- sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java (original) +++ sling/trunk/contrib/extensions/validation/api/src/main/java/org/apache/sling/validation/api/ValidationService.java Wed Jan 14 17:42:50 2015 @@ -31,11 +31,12 @@ public interface ValidationService { /** * Tries to obtain a {@link ValidationModel} that is able to validate a {@code Resource} of type {@code validatedResourceType}. * - * @param validatedResourceType the type of {@code Resources} the model validates + * @param validatedResourceType the type of {@code Resources} the model validates, should be either relative + * (i.e. not start with a "/") or starting with one of the resource resolver's search paths * @param applicablePath the model's applicable path (the path of the validated resource) * @return a {@code ValidationModel} if one is found, {@code null} otherwise * @throws IllegalStateException in case an invalid validation model was found - * @throws IllegalArgumentException in case validatedResourceType was blank or {@code null} + * @throws IllegalArgumentException in case validatedResourceType was blank, {@code null} or absolute but outside of the search paths. */ ValidationModel getValidationModel(String validatedResourceType, String applicablePath) throws IllegalStateException, IllegalArgumentException; Modified: sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java?rev=1651744&r1=1651743&r2=1651744&view=diff ============================================================================== --- sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java (original) +++ sling/trunk/contrib/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/ValidationServiceImpl.java Wed Jan 14 17:42:50 2015 @@ -68,7 +68,7 @@ public class ValidationServiceImpl imple private static final Logger LOG = LoggerFactory.getLogger(ValidationServiceImpl.class); - static final String MODEL_XPATH_QUERY = "/jcr:root%s//" + Constants.MODELS_HOME + "/*[@sling:resourceType=\"%s\" and @%s=\"%s\"]"; + static final String MODEL_XPATH_QUERY = "/jcr:root%s/" + Constants.MODELS_HOME + "/*[@sling:resourceType=\"%s\" and @%s=\"%s\"]"; static final String[] TOPICS = {SlingConstants.TOPIC_RESOURCE_REMOVED, SlingConstants.TOPIC_RESOURCE_CHANGED, SlingConstants.TOPIC_RESOURCE_ADDED}; @@ -93,6 +93,10 @@ public class ValidationServiceImpl imple // ValidationService ################################################################################################################### @Override public ValidationModel getValidationModel(String validatedResourceType, String resourcePath) { + if (validatedResourceType == null || resourcePath == null) { + throw new IllegalArgumentException("ValidationService.getValidationModel - cannot accept null parameters"); + } + validatedResourceType = getRelativeResourceType(validatedResourceType); ValidationModel model = null; Trie<JCRValidationModel> modelsForResourceType = validationModelsCache.get(validatedResourceType); if (modelsForResourceType != null) { @@ -115,7 +119,7 @@ public class ValidationServiceImpl imple @Override public ValidationResult validate(Resource resource, ValidationModel model) { if (resource == null || model == null) { - throw new IllegalArgumentException("ValidationResult.validate - cannot accept null parameters"); + throw new IllegalArgumentException("ValidationService.validate - cannot accept null parameters"); } ValidationResultImpl result = new ValidationResultImpl(); @@ -128,6 +132,39 @@ public class ValidationServiceImpl imple } /** + * If the given resourceType is starting with a "/", it will strip out the leading search path from the given resource type. + * Otherwise it will just return the given resource type (as this is already relative). + * @param resourceType + * @return a relative resource type (without the leading search path) + * @throws IllegalArgumentException in case the resource type is starting with a "/" but not with any of the search paths. + */ + protected String getRelativeResourceType(String resourceType) throws IllegalArgumentException { + if (resourceType.startsWith("/")) { + LOG.debug("try to strip the search path from the resource type"); + ResourceResolver rr = null; + try { + rr = rrf.getAdministrativeResourceResolver(null); + for (String searchPath : rr.getSearchPath()) { + if (resourceType.startsWith(searchPath)) { + resourceType = resourceType.substring(searchPath.length()); + return resourceType; + } + } + throw new IllegalArgumentException("Can only deal with resource types inside the resource resolver's search path (" + StringUtils.join(rr.getSearchPath()) + + ") but given resource type " + resourceType + " is outside!"); + } catch (LoginException e) { + throw new IllegalStateException("Could not login as administrator to figure out search paths", e); + } finally { + if (rr != null) { + rr.close(); + } + } + } + return resourceType; + + } + + /** * Validates a child resource with the help of the given {@code ChildResource} entry from the validation model * @param resource * @param relativePath relativePath of the resource (must be empty or end with "/") @@ -316,9 +353,6 @@ public class ValidationServiceImpl imple rr = rrf.getAdministrativeResourceResolver(null); String[] searchPaths = rr.getSearchPath(); for (String searchPath : searchPaths) { - if (searchPath.endsWith("/")) { - searchPath = searchPath.substring(0, searchPath.length() - 1); - } final String queryString = String.format(MODEL_XPATH_QUERY, searchPath, Constants.VALIDATION_MODEL_RESOURCE_TYPE, Constants.VALIDATED_RESOURCE_TYPE, validatedResourceType); Iterator<Resource> models = rr.findResources(queryString, Query.XPATH); Modified: sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java?rev=1651744&r1=1651743&r2=1651744&view=diff ============================================================================== --- sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java (original) +++ sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/ValidationServiceImplTest.java Wed Jan 14 17:42:50 2015 @@ -731,6 +731,20 @@ public class ValidationServiceImplTest { } } } + + @Test + public void testGetRelativeResourcePath() { + // return relative paths unmodified + Assert.assertThat(validationService.getRelativeResourceType("relative/path"), Matchers.equalTo("relative/path")); + Assert.assertThat(validationService.getRelativeResourceType("/apps/relative/path"), Matchers.equalTo("relative/path")); + Assert.assertThat(validationService.getRelativeResourceType("/libs/relative/path"), Matchers.equalTo("relative/path")); + } + + @Test(expected=IllegalArgumentException.class) + public void testGetRelativeResourcePathWithAbsolutePathOutsideOfTheSearchPaths() { + // return relative paths unmodified + validationService.getRelativeResourceType("/apps2/relative/path"); + } private Resource createValidationModelResource(ResourceResolver rr, String root, String name, String validatedResourceType, String[] applicableResourcePaths, TestProperty... properties) throws Exception { Modified: sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java?rev=1651744&r1=1651743&r2=1651744&view=diff ============================================================================== --- sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java (original) +++ sling/trunk/contrib/extensions/validation/core/src/test/java/org/apache/sling/validation/impl/setup/MockedResourceResolver.java Wed Jan 14 17:42:50 2015 @@ -45,7 +45,8 @@ public class MockedResourceResolver impl private static final Logger LOG = LoggerFactory.getLogger(MockedResourceResolver.class); - private static final String[] SEARCH_PATHS = new String[] {"/apps", "/libs"}; + // must end with a slash according to ResourceResolver.getSearchPaths() + private static final String[] SEARCH_PATHS = new String[] {"/apps/", "/libs/"}; public final RepositoryProvider repoProvider; private List<MockedResource> resources = new LinkedList<MockedResource>();