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;
}
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);
}
}
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;
}
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"));
}
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"))));
}
Aggregations