This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-validation-impl.git
commit b1ec8f462bf63999df19014dd29dfa7512a12b09 Author: Konrad Windszus <[email protected]> AuthorDate: Mon Feb 20 16:08:15 2017 +0000 SLING-6531 allow to configure a threshold for the validation failure severities. Only failures equal or above the threshold let Sling Models consider the model invalid. Update to latest parent. Migrate to OSGi annotations. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1783780 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 50 ++++++++++------------ .../impl/ModelValidationConfiguration.java | 30 +++++++++++++ .../validation/impl/ModelValidationImpl.java | 40 ++++++++++++++--- 3 files changed, 86 insertions(+), 34 deletions(-) diff --git a/pom.xml b/pom.xml index 2ee155f..0e105ce 100644 --- a/pom.xml +++ b/pom.xml @@ -1,30 +1,31 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.apache.sling</groupId> <artifactId>sling</artifactId> - <version>26</version> + <version>29</version> <relativePath/> </parent> @@ -48,10 +49,6 @@ <plugins> <plugin> <groupId>org.apache.felix</groupId> - <artifactId>maven-scr-plugin</artifactId> - </plugin> - <plugin> - <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> </plugin> @@ -66,7 +63,12 @@ </dependency> <dependency> <groupId>javax.servlet</groupId> - <artifactId>servlet-api</artifactId> + <artifactId>javax.servlet-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>osgi.cmpn</artifactId> <scope>provided</scope> </dependency> <dependency> @@ -89,12 +91,6 @@ <dependency> <groupId>com.google.code.findbugs</groupId> <artifactId>jsr305</artifactId> - <version>3.0.0</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.scr.annotations</artifactId> <scope>provided</scope> </dependency> <dependency> diff --git a/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java b/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java new file mode 100644 index 0000000..c7e2b88 --- /dev/null +++ b/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.models.validation.impl; + +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; + +@ObjectClassDefinition(name = "Apache Sling Models Validation Configuration (for Sling Validation)", description = "Allows to configure how Sling Models are validated with the help of Sling Validation") +public @interface ModelValidationConfiguration { + @AttributeDefinition(name = "Disabled") + boolean disabled() default false; + @AttributeDefinition(name = "Severity Threshold", description = "This threshold specifies the minimum severity of the underlying Sling validation failures for making a Sling Model invalid. If all validation failures are below that threshold the model is considered valid.") + int severityThreshold() default 0; +} diff --git a/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java b/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java index 3b71768..6f5b9da 100644 --- a/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java +++ b/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java @@ -16,19 +16,21 @@ */ package org.apache.sling.models.validation.impl; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.factory.InvalidModelException; import org.apache.sling.models.factory.ValidationException; import org.apache.sling.models.spi.ModelValidation; import org.apache.sling.models.validation.InvalidResourceException; +import org.apache.sling.validation.SlingValidationException; +import org.apache.sling.validation.ValidationFailure; import org.apache.sling.validation.ValidationResult; import org.apache.sling.validation.ValidationService; -import org.apache.sling.validation.SlingValidationException; import org.apache.sling.validation.model.ValidationModel; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.metatype.annotations.Designate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,17 +39,24 @@ import org.slf4j.LoggerFactory; * It enforces a validation of the resource which is adapted to the model. * @see <a href="http://sling.apache.org/documentation/bundles/validation.html">Sling Validation</a> */ -@Service @Component +@Designate(ocd=ModelValidationConfiguration.class) public class ModelValidationImpl implements ModelValidation { @Reference private ValidationService validation; + private ModelValidationConfiguration configuration; + private static final Logger log = LoggerFactory.getLogger(ModelValidationImpl.class); + @Activate + protected void activate(ModelValidationConfiguration configuration) { + this.configuration = configuration; + } + /** - * Triggers validation for the given model on the given adaptable. Instead of the generic + * Triggers validation for the given model on the given adaptable. * @param adaptable {@inheritDoc} * @param modelClass {@inheritDoc} * @param required {@inheritDoc} @@ -56,6 +65,10 @@ public class ModelValidationImpl implements ModelValidation { * Or a {@link InvalidResourceException} in case the given resource (in the adaptable) could not be validated through the {@link ModelValidation}. */ public <ModelType> RuntimeException validate(Object adaptable, Class<ModelType> modelClass, boolean required) throws ValidationException, InvalidModelException { + if (!configuration.disabled()) { + log.debug("Skip validation of model {}, because validation is disabled through the OSGi configuration for ModelValidationConfiguration", modelClass); + return null; + } Resource resource = null; if (adaptable instanceof SlingHttpServletRequest) { resource = ((SlingHttpServletRequest)adaptable).getResource(); @@ -83,7 +96,20 @@ public class ModelValidationImpl implements ModelValidation { try { ValidationResult validationResult = validation.validate(resource, validationModel); if (!validationResult.isValid()) { - return new InvalidResourceException("Model is invalid", validationResult, resource.getPath()); + boolean shouldThrow = false; + // evaluate all severities + for (ValidationFailure failure : validationResult.getFailures()) { + if (failure.getSeverity() >= configuration.severityThreshold()) { + shouldThrow = true; + break; + } + } + if (shouldThrow) { + return new InvalidResourceException("Sling Model is invalid", validationResult, resource.getPath()); + } else { + log.debug("Although the resource {} is considered invalid by Sling Validation, all validation failures have a severity below the threshold '{}', " + + "therefore considering this Sling Model valid.", resource.getPath(), configuration.severityThreshold()); + } } } catch (SlingValidationException e) { return new ValidationException(e); -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
