Search in sources :

Example 1 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationModelRetrieverImpl method getValidationModel.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.sling.validation.impl.ValidationModelRetriever#getModels(java.lang.String, java.lang.String)
     */
@CheckForNull
public ValidationModel getValidationModel(@Nonnull String resourceType, String resourcePath, boolean considerResourceSuperTypeModels) {
    // first get model for exactly the requested resource type
    ValidationModel baseModel = getModel(resourceType, resourcePath);
    String currentResourceType = resourceType;
    if (considerResourceSuperTypeModels) {
        Collection<ValidationModel> modelsToMerge = new ArrayList<ValidationModel>();
        ResourceResolver resourceResolver = null;
        try {
            resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
            while ((currentResourceType = resourceResolver.getParentResourceType(currentResourceType)) != null) {
                LOG.debug("Retrieving validation models for resource super type {}...", currentResourceType);
                ValidationModel modelToMerge = getModel(currentResourceType, resourcePath);
                if (modelToMerge != null) {
                    if (baseModel == null) {
                        baseModel = modelToMerge;
                    } else {
                        modelsToMerge.add(modelToMerge);
                    }
                }
            }
            if (!modelsToMerge.isEmpty()) {
                return new MergedValidationModel(baseModel, modelsToMerge.toArray(new ValidationModel[modelsToMerge.size()]));
            }
        } catch (LoginException e) {
            throw new IllegalStateException("Could not get service resource resolver", e);
        } finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
    }
    return baseModel;
}
Also used : MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ValidationModel(org.apache.sling.validation.model.ValidationModel) MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ArrayList(java.util.ArrayList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) CheckForNull(javax.annotation.CheckForNull)

Example 2 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationResourceVisitor method visit.

@Override
protected void visit(Resource resource) {
    if (isValidSubResource(resource)) {
        @SuppressWarnings("null") ValidationModel model = validationService.getValidationModel(resource, considerResourceSuperTypeModels);
        if (model == null) {
            if (enforceValidation) {
                throw new IllegalArgumentException("No model for resource type " + resource.getResourceType() + " found.");
            }
            return;
        }
        // calculate the property name correctly from the root
        // the relative path must not end with a slash and not start with a slash
        @Nonnull final String relativePath;
        if (resource.getPath().startsWith(rootResourcePath)) {
            relativePath = resource.getPath().substring(rootResourcePath.length());
        } else {
            relativePath = "";
        }
        ValidationResult localResult = validationService.validate(resource, model, relativePath);
        result.addValidationResult(localResult);
    }
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Nonnull(javax.annotation.Nonnull) ValidationResult(org.apache.sling.validation.ValidationResult)

Example 3 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class ValidationModelRetrieverImpl method getModel.

@CheckForNull
private ValidationModel getModel(@Nonnull String resourceType, String resourcePath) {
    PatriciaTrie<ValidationModel> modelsForResourceType = fillTrieForResourceType(resourceType);
    ValidationModel model = null;
    // for empty/null resource paths, always return the entry stored for ""
    if (StringUtils.isEmpty(resourcePath)) {
        model = modelsForResourceType.get("");
    } else {
        // get longest prefix entry, which still matches
        SortedMap<String, ValidationModel> modelMap = modelsForResourceType.subMap("", resourcePath + "/");
        if (!modelMap.isEmpty()) {
            model = modelMap.get(modelMap.lastKey());
        }
    }
    if (model == null && !modelsForResourceType.isEmpty()) {
        LOG.warn("Although at least one model for resource type '{}' is available, none of them are allowed to be applied to path {}", resourceType, resourcePath);
    }
    return model;
}
Also used : MergedValidationModel(org.apache.sling.validation.impl.model.MergedValidationModel) ValidationModel(org.apache.sling.validation.model.ValidationModel) CheckForNull(javax.annotation.CheckForNull)

Example 4 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class MergedValidationModelTest method testMoreSpecificApplicationPathInModelToMerge.

@Test(expected = IllegalArgumentException.class)
public void testMoreSpecificApplicationPathInModelToMerge() {
    modelBuilder.addApplicablePath("/base/path").addApplicablePath("/base/path2");
    ValidationModel baseValidationModel = modelBuilder.build("base", "some source");
    modelBuilder.setApplicablePath("/base/path3");
    new MergedValidationModel(baseValidationModel, modelBuilder.build("superType", "some source"));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) Test(org.junit.Test)

Example 5 with ValidationModel

use of org.apache.sling.validation.model.ValidationModel in project sling by apache.

the class MergedValidationModelTest method testOverwritingChildrenAndResourceProperties.

@Test
public void testOverwritingChildrenAndResourceProperties() {
    modelBuilder.resourceProperty(propertyBuilder.nameRegex("overwrittenNameToOverwrite").build("nameToOverwrite"));
    modelBuilder.childResource(childResourceBuilder.nameRegex("overwrittenNameToOverwrite").build("nameToOverwrite"));
    ValidationModel baseValidationModel = modelBuilder.build("base", "some source");
    modelBuilder = new ValidationModelBuilder();
    modelBuilder.resourceProperty(propertyBuilder.nameRegex("originalNameToOverwrite").build("nameToOverwrite"));
    modelBuilder.childResource(childResourceBuilder.nameRegex("originalNameToOverwrite").build("nameToOverwrite"));
    modelBuilder.resourceProperty(propertyBuilder.nameRegex("originalNameNotOverwritten").build("nameNotOverwritten"));
    modelBuilder.childResource(childResourceBuilder.nameRegex("originalNameNotOverwritten").build("nameNotOverwritten"));
    ValidationModel mergedModel = new MergedValidationModel(baseValidationModel, modelBuilder.build("superType", "some source"));
    Assert.assertThat(mergedModel.getResourceProperties(), Matchers.containsInAnyOrder(Arrays.asList(new ResourcePropertyNameRegexMatcher("overwrittenNameToOverwrite"), new ResourcePropertyNameRegexMatcher("originalNameNotOverwritten"))));
    Assert.assertThat(mergedModel.getChildren(), Matchers.containsInAnyOrder(Arrays.asList(new ChildResourceNameRegexMatcher("overwrittenNameToOverwrite"), new ChildResourceNameRegexMatcher("originalNameNotOverwritten"))));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) ResourcePropertyNameRegexMatcher(org.apache.sling.validation.impl.util.ResourcePropertyNameRegexMatcher) ChildResourceNameRegexMatcher(org.apache.sling.validation.impl.util.ChildResourceNameRegexMatcher) Test(org.junit.Test)

Aggregations

ValidationModel (org.apache.sling.validation.model.ValidationModel)53 Test (org.junit.Test)43 ValidationResult (org.apache.sling.validation.ValidationResult)22 Resource (org.apache.sling.api.resource.Resource)20 ChildResource (org.apache.sling.validation.model.ChildResource)20 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)17 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)15 HashMap (java.util.HashMap)13 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)12 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)12 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)11 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)9 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)9 ResourcePropertyBuilder (org.apache.sling.validation.impl.model.ResourcePropertyBuilder)8 ChildResourceImpl (org.apache.sling.validation.impl.model.ChildResourceImpl)7 ResourceProperty (org.apache.sling.validation.model.ResourceProperty)7 ValidationModelBuilder (org.apache.sling.validation.impl.model.ValidationModelBuilder)6 CheckForNull (javax.annotation.CheckForNull)5 Nonnull (javax.annotation.Nonnull)5 ValueMap (org.apache.sling.api.resource.ValueMap)4