You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2017/02/20 16:08:15 UTC
svn commit: r1783780 - in
/sling/trunk/bundles/extensions/models/validation-impl: ./
src/main/java/org/apache/sling/models/validation/impl/
Author: kwin
Date: Mon Feb 20 16:08:15 2017
New Revision: 1783780
URL: http://svn.apache.org/viewvc?rev=1783780&view=rev
Log:
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.
Added:
sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java (with props)
Modified:
sling/trunk/bundles/extensions/models/validation-impl/pom.xml
sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java
Modified: sling/trunk/bundles/extensions/models/validation-impl/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl/pom.xml?rev=1783780&r1=1783779&r2=1783780&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/validation-impl/pom.xml (original)
+++ sling/trunk/bundles/extensions/models/validation-impl/pom.xml Mon Feb 20 16:08:15 2017
@@ -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
-
- 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.
+ 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.
-->
-<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>
Added: sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java?rev=1783780&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java (added)
+++ sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java Mon Feb 20 16:08:15 2017
@@ -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;
+}
Propchange: sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationConfiguration.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java?rev=1783780&r1=1783779&r2=1783780&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java (original)
+++ sling/trunk/bundles/extensions/models/validation-impl/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java Mon Feb 20 16:08:15 2017
@@ -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 impleme
* 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 impleme
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);