This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.validation.test-services-1.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-validation-test-services.git
commit 591fdd962b18c9cea8f5ca7ecbb4c958cb5c5033 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Tue Oct 7 15:10:37 2014 +0000 SLING-2803 - validation module contribution, by Radu Cotescu, thanks! git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/validation/test-services@1629907 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 134 +++++++++++++++++++++ .../testservices/ValidationPostOperation.java | 80 ++++++++++++ .../testservices/ValidationPostResponse.java | 71 +++++++++++ .../ValidationPostResponseCreator.java | 22 ++++ .../apps/sling/validation/models.json | 3 + .../apps/sling/validation/models/model1.json | 25 ++++ 6 files changed, 335 insertions(+) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f484157 --- /dev/null +++ b/pom.xml @@ -0,0 +1,134 @@ +<?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. + --> +<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"> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>15</version> + <relativePath /> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>org.apache.sling.validation.test-services</artifactId> + <version>1.0.0-SNAPSHOT</version> + <packaging>bundle</packaging> + + <name>Apache Sling Validation Framework Testing Services</name> + + <properties> + <sling.java.version>6</sling.java.version> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <version>1.11.0</version> + <executions> + <execution> + <id>generate-scr-descriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Sling-Initial-Content>SLING-CONTENT;overwrite:=true</Sling-Initial-Content> + <Export-Package> + !org.apache.sling.validation.testservices + </Export-Package> + <Private-Package> + org.apache.sling.validation.testservices + </Private-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.compendium</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + <version>1.9.0</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.api</artifactId> + <version>2.4.2</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.servlets.post</artifactId> + <version>2.2.0</version> + <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>org.apache.sling</groupId> + <artifactId>org.apache.sling.commons.json</artifactId> + <version>2.0.6</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <scope>provided</scope> + </dependency> + <!-- test dependencies --> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.commons.testing</artifactId> + <version>2.0.14</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/org/apache/sling/validation/testservices/ValidationPostOperation.java b/src/main/java/org/apache/sling/validation/testservices/ValidationPostOperation.java new file mode 100644 index 0000000..408c058 --- /dev/null +++ b/src/main/java/org/apache/sling/validation/testservices/ValidationPostOperation.java @@ -0,0 +1,80 @@ +/* + * 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.validation.testservices; + +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Properties; +import org.apache.felix.scr.annotations.Property; +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.ValueMap; +import org.apache.sling.commons.json.JSONObject; +import org.apache.sling.servlets.post.AbstractPostOperation; +import org.apache.sling.servlets.post.Modification; +import org.apache.sling.servlets.post.PostOperation; +import org.apache.sling.servlets.post.PostResponse; +import org.apache.sling.validation.api.ValidationModel; +import org.apache.sling.validation.api.ValidationResult; +import org.apache.sling.validation.api.ValidationService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.jcr.RepositoryException; +import java.util.List; + +@Component() +@Service(PostOperation.class) +@Properties({ + @Property( + name = PostOperation.PROP_OPERATION_NAME, + value = "validation", + propertyPrivate = true + ) +}) +public class ValidationPostOperation extends AbstractPostOperation { + + private static final Logger LOG = LoggerFactory.getLogger(ValidationPostOperation.class); + + @Reference + private ValidationService validationService; + + @Override + protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException { + if (response instanceof ValidationPostResponse) { + ValidationPostResponse vpr = (ValidationPostResponse) response; + ValueMap requestParameters = request.adaptTo(ValueMap.class); + String[] resourceTypeValues = requestParameters.get("sling:resourceType", String[].class); + String resourceType = null; + if (resourceTypeValues != null && resourceTypeValues.length > 0) { + resourceType = resourceTypeValues[0]; + } + if (resourceType != null && !"".equals(resourceType)) { + String resourcePath = request.getRequestPathInfo().getResourcePath(); + ValidationModel vm = validationService.getValidationModel(resourceType, resourcePath); + if (vm != null) { + ValidationResult vr = validationService.validate(requestParameters, vm); + vpr.setValidationResult(vr); + } else { + LOG.error("No validation model for resourceType {} and resourcePath {} ", resourceType, resourcePath); + } + } + } + } +} diff --git a/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java b/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java new file mode 100644 index 0000000..be9a64d --- /dev/null +++ b/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponse.java @@ -0,0 +1,71 @@ +/* + * 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.validation.testservices; + +import org.apache.sling.commons.json.JSONException; +import org.apache.sling.commons.json.JSONObject; +import org.apache.sling.servlets.post.AbstractPostResponse; +import org.apache.sling.validation.api.ValidationResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; +import java.util.Map; + +public class ValidationPostResponse extends AbstractPostResponse { + + private ValidationResult validationResult; + private static final Logger LOG = LoggerFactory.getLogger(ValidationPostResponse.class); + + public void setValidationResult(ValidationResult validationResult) { + this.validationResult = validationResult; + } + + @Override + protected void doSend(HttpServletResponse response) throws IOException { + response.setContentType("application/json"); + PrintWriter printWriter = response.getWriter(); + JSONObject jsonResponse = new JSONObject(); + boolean validationError = false; + if (validationResult != null) { + try { + jsonResponse.put("valid", validationResult.isValid()); + for (Map.Entry<String, List<String>> entry : validationResult.getFailureMessages().entrySet()) { + jsonResponse.put(entry.getKey(), entry.getValue()); + } + } catch (JSONException e) { + LOG.error("JSON error during response send operation.", e); + } + } else { + validationError = true; + } + printWriter.write(jsonResponse.toString()); + if (validationError) { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + } + + @Override + public void onChange(String type, String... arguments) { + // NOOP + } +} diff --git a/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponseCreator.java b/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponseCreator.java new file mode 100644 index 0000000..27967ae --- /dev/null +++ b/src/main/java/org/apache/sling/validation/testservices/ValidationPostResponseCreator.java @@ -0,0 +1,22 @@ +package org.apache.sling.validation.testservices; + +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.servlets.post.PostResponse; +import org.apache.sling.servlets.post.PostResponseCreator; +import org.apache.sling.servlets.post.SlingPostConstants; + +@Component() +@Service(PostResponseCreator.class) +public class ValidationPostResponseCreator implements PostResponseCreator { + + @Override + public PostResponse createPostResponse(SlingHttpServletRequest request) { + String operation = request.getParameter(SlingPostConstants.RP_OPERATION); + if (operation != null && "validation".equals(operation)) { + return new ValidationPostResponse(); + } + return null; + } +} diff --git a/src/main/resources/SLING-CONTENT/apps/sling/validation/models.json b/src/main/resources/SLING-CONTENT/apps/sling/validation/models.json new file mode 100644 index 0000000..225e5ce --- /dev/null +++ b/src/main/resources/SLING-CONTENT/apps/sling/validation/models.json @@ -0,0 +1,3 @@ +{ + "jcr:primaryType" : "sling:Folder" +} diff --git a/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json b/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json new file mode 100644 index 0000000..8b59679 --- /dev/null +++ b/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json @@ -0,0 +1,25 @@ +{ + "validatedResourceType": "validation/test/resourceType1", + "applicablePaths": [ + "/validation/testing/fakeFolder1" + ], + "sling:resourceType": "sling/validation/model", + "properties": { + "jcr:primaryType" : "nt:unstructured", + "field1": { + "jcr:primaryType" : "nt:unstructured", + "propertyType": "string", + "validators": { + "jcr:primaryType" : "nt:unstructured", + "org.apache.sling.validation.impl.validators.RegexValidator": { + "jcr:primaryType" : "nt:unstructured", + "validatorArguments" : ["regex=^\\p{L}+$"] + } + } + }, + "field2" : { + "jcr:primaryType" : "nt:unstructured", + "propertyType" : "date" + } + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
