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 771e43c8d8fb6e7f4f246839ec8e34c3e1676f7d Author: Konrad Windszus <[email protected]> AuthorDate: Mon Jun 1 16:24:13 2015 +0000 SLING-4161 move validation to separate bundle git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1682946 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 118 +++++++++++++++++++++ .../validation/InvalidResourceException.java | 69 ++++++++++++ .../validation/impl/ModelValidationImpl.java | 99 +++++++++++++++++ 3 files changed, 286 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3b50f6c --- /dev/null +++ b/pom.xml @@ -0,0 +1,118 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>22</version> + <relativePath/> + </parent> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.models.validation-impl</artifactId> + <packaging>bundle</packaging> + <version>1.0.0-SNAPSHOT</version> + <name>Apache Sling Models Validation Implementation</name> + <description>Validation Implementation which leverages Sling Validation</description> + <properties> + <sling.java.version>6</sling.java.version> + </properties> + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/validation-impl</connection> + <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/validation-impl</developerConnection> + <url>http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/validation-impl</url> + </scm> + <build> + <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> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.models.api</artifactId> + <version>1.1.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.5</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.api</artifactId> + <version>2.2.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.validation.api</artifactId> + <version>1.0.0-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + <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> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>1.9.5</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java b/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java new file mode 100644 index 0000000..34c374e --- /dev/null +++ b/src/main/java/org/apache/sling/models/validation/InvalidResourceException.java @@ -0,0 +1,69 @@ +/* + * 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; + +import java.util.List; +import java.util.Map.Entry; + +import org.apache.commons.lang.StringUtils; +import org.apache.sling.models.factory.InvalidModelException; +import org.apache.sling.validation.api.ValidationResult; + +/** + * Exception embedding a ValidationResult from Sling Validation. + * + */ +public class InvalidResourceException extends InvalidModelException { + + /** + * + */ + private static final long serialVersionUID = -5134512515466089688L; + private final ValidationResult result; + private final String path; + + public InvalidResourceException(String message, ValidationResult result, String path) { + super(message); + this.result = result; + this.path = path; + } + /** + * + * @return the underlying {@link ValidationResult} + */ + public ValidationResult getResult() { + return result; + } + + /** + * + * @return the path of the resource which was considered invalid + */ + public String getPath() { + return path; + } + + @Override + public String getMessage() { + StringBuilder builder = new StringBuilder("Validation errors for "); + builder.append("'" + path +"':"); + for (Entry<String, List<String>> entry : result.getFailureMessages().entrySet()) { + builder.append("\n" + entry.getKey() + ":" + StringUtils.join(entry.getValue(), "\n\t")); + } + return builder.toString(); + } +} 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 new file mode 100644 index 0000000..0f33e88 --- /dev/null +++ b/src/main/java/org/apache/sling/models/validation/impl/ModelValidationImpl.java @@ -0,0 +1,99 @@ +/* + * 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.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.api.ValidationModel; +import org.apache.sling.validation.api.ValidationResult; +import org.apache.sling.validation.api.ValidationService; +import org.apache.sling.validation.api.exceptions.SlingValidationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Provides a validation service for Sling Models based on Sling Validation. + * 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 +public class ModelValidationImpl implements ModelValidation { + + @Reference + private ValidationService validation; + + private static final Logger log = LoggerFactory.getLogger(ModelValidationImpl.class); + + /** + * Triggers validation for the given model on the given adaptable. Instead of the generic + * @param adaptable {@inheritDoc} + * @param modelClass {@inheritDoc} + * @param required {@inheritDoc} + * @return a {@link ValidationException} in case validation could not be performed if validation model could not be performed but + * required=true or if the adaptable is neither a Resource nor a SlingHttpServletRequest. + * 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 { + Resource resource = null; + if (adaptable instanceof SlingHttpServletRequest) { + resource = ((SlingHttpServletRequest)adaptable).getResource(); + } else if (adaptable instanceof Resource) { + resource = (Resource)adaptable; + } + if (resource != null) { + return validate(resource, required); + } else { + return new ValidationException("Sling Validation can only be performed if model is adapted from either SlingHttpServletRequest or Resource."); + } + } + + private RuntimeException validate(Resource resource, boolean required) { + try { + ValidationModel validationModel = validation.getValidationModel(resource); + if (validationModel == null) { + String error = String.format("Could not find validation model for resource '%s' with type '%s'", resource.getPath(), resource.getResourceType()); + if (required) { + return new ValidationException(error); + } else { + log.warn(error); + } + } else { + try { + ValidationResult validationResult = validation.validate(resource, validationModel); + if (!validationResult.isValid()) { + return new InvalidResourceException("Model is invalid", validationResult, resource.getPath()); + } + } catch (SlingValidationException e) { + return new ValidationException(e); + } + } + } catch (IllegalStateException e) { + return new ValidationException(e); + } + + return null; + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
