You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/20 14:39:50 UTC
[sling-org-apache-sling-validation-examples] 01/24: SLING 4606 move
validation from contrib to /
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-validation-examples.git
commit 73d0803df20e7e44a003e9d9fc27d782dcce0db5
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Apr 10 13:22:08 2015 +0000
SLING 4606 move validation from contrib to /
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1672647 13f79535-47bb-0310-9956-ffa450edef68
---
README.md | 26 ++++
pom.xml | 141 +++++++++++++++++++++
.../validation/examples/models/UserModel.java | 91 +++++++++++++
.../examples/servlets/ModifyUserServlet.java | 92 ++++++++++++++
src/main/resources/SLING-INF/apps/sling.json | 3 +
.../resources/SLING-INF/apps/sling/validation.json | 3 +
.../SLING-INF/apps/sling/validation/models.json | 3 +
.../apps/sling/validation/models/user.admin.json | 57 +++++++++
.../apps/sling/validation/models/user.regular.json | 44 +++++++
.../resources/SLING-INF/apps/validationdemo.json | 3 +
.../SLING-INF/apps/validationdemo/components.json | 3 +
.../apps/validationdemo/components/user.json | 3 +
.../apps/validationdemo/components/user/user.jsp | 42 ++++++
.../content/validationdemo/users/admin/neo.json | 11 ++
.../content/validationdemo/users/johnsmith.json | 8 ++
15 files changed, 530 insertions(+)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..1bc89a7
--- /dev/null
+++ b/README.md
@@ -0,0 +1,26 @@
+## How To Run the Examples
+1. Start a Sling launchpad
+2. Install the `org.apache.sling.validation.api` and `org.apache.sling.validation.core` bundles:
+
+ ```bash
+ cd ../api
+ mvn clean package sling:install
+ cd ../core
+ mvn clean package sling:install
+ cd ../examples
+ mvn clean package sling:install
+ ```
+
+## Invalid POST request
+```bash
+curl -u admin:admin -Fsling:resourceType=/apps/validationdemo/components/user -Fusername=johnsmith -FfirstName=John204 -FlastName=Smith http://127.0.0.1:8080/content/validationdemo/users/johnsmith.modify.html
+```
+
+Check that the resource has not been modified at http://127.0.0.1:8080/content/validationdemo/users/johnsmith.html.
+
+## Valid POST request
+```bash
+curl -u admin:admin -Fsling:resourceType=/apps/validationdemo/components/user -Fusername=johnsmith -FfirstName=Johnny -FlastName=Bravo http://127.0.0.1:8080/content/validationdemo/users/johnsmith.modify.html
+```
+
+Check that the resource has been modified at http://127.0.0.1:8080/content/validationdemo/users/johnsmith.html.
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..ae1b515
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,141 @@
+<?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.examples</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <packaging>bundle</packaging>
+
+ <name>Apache Sling Validation Framework Examples</name>
+ <description>Demo Examples for the Apache Sling Validation Framework</description>
+
+ <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>
+ <Private-Package>
+ org.apache.sling.validation.examples.*
+ </Private-Package>
+ <Export-Package>
+ org.apache.sling.validation.examples.models;version=1.0.0
+ </Export-Package>
+ <Sling-Model-Packages>org.apache.sling.validation.examples.models</Sling-Model-Packages>
+ <Sling-Initial-Content>SLING-INF;</Sling-Initial-Content>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <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>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>javax.jcr</groupId>
+ <artifactId>jcr</artifactId>
+ <version>2.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.slf4j</groupId>
+ <artifactId>slf4j-api</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.commons.osgi</artifactId>
+ <version>2.2.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.models.api</artifactId>
+ <version>1.1.0</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>
+ </dependencies>
+
+</project>
diff --git a/src/main/java/org/apache/sling/validation/examples/models/UserModel.java b/src/main/java/org/apache/sling/validation/examples/models/UserModel.java
new file mode 100644
index 0000000..bd6d362
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/examples/models/UserModel.java
@@ -0,0 +1,91 @@
+/*
+ * 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.examples.models;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.annotation.PostConstruct;
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.validation.api.ValidationModel;
+import org.apache.sling.validation.api.ValidationResult;
+import org.apache.sling.validation.api.ValidationService;
+
+@Model(adaptables = Resource.class)
+public class UserModel {
+
+ private static final String INVALID = "INVALID";
+
+ private Resource resource;
+ private Map<String, List<String>> errors = new HashMap<String, List<String>>();
+
+ @Inject
+ private ValidationService validationService;
+
+ @Inject
+ private String username;
+
+ @Inject
+ private String firstName;
+
+ @Inject
+ private String lastName;
+
+ @Inject
+ private boolean isAdmin;
+
+ public UserModel(Resource resource) {
+ this.resource = resource;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public boolean isAdmin() {
+ return isAdmin;
+ }
+
+ public Map<String, List<String>> getErrors() {
+ return errors;
+ }
+
+ @PostConstruct
+ protected void validate() {
+ ValidationModel model = validationService.getValidationModel(resource);
+ if (model != null) {
+ ValidationResult result = validationService.validate(resource, model);
+ if (!result.isValid()) {
+ errors = result.getFailureMessages();
+ }
+ }
+ }
+}
diff --git a/src/main/java/org/apache/sling/validation/examples/servlets/ModifyUserServlet.java b/src/main/java/org/apache/sling/validation/examples/servlets/ModifyUserServlet.java
new file mode 100644
index 0000000..3acc333
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/examples/servlets/ModifyUserServlet.java
@@ -0,0 +1,92 @@
+/*
+ * 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.examples.servlets;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.sling.SlingServlet;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.apache.sling.commons.json.JSONArray;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.validation.api.ValidationModel;
+import org.apache.sling.validation.api.ValidationResult;
+import org.apache.sling.validation.api.ValidationService;
+
+@SlingServlet(
+ resourceTypes = "/apps/validationdemo/components/user",
+ selectors = {"modify"},
+ methods = "POST"
+)
+public class ModifyUserServlet extends SlingAllMethodsServlet {
+
+ @Reference
+ private ValidationService validationService;
+
+ @Override
+ protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
+ 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);
+ if (vr.isValid()) {
+ RequestDispatcherOptions options = new RequestDispatcherOptions();
+ options.setReplaceSelectors(" ");
+ request.getRequestDispatcher(request.getResource(), options).forward(request, response);
+ } else {
+ response.setContentType("application/json");
+ JSONObject json = new JSONObject();
+ try {
+ json.put("success", false);
+ JSONObject messages = new JSONObject();
+ for (Map.Entry<String, List<String>> entry : vr.getFailureMessages().entrySet()) {
+ String key = entry.getKey();
+ JSONArray errors = new JSONArray();
+ for (String message : entry.getValue()) {
+ errors.put(message);
+ }
+ messages.put(key, errors);
+ }
+ json.put("messages", messages);
+ response.getWriter().print(json.toString());
+ response.setStatus(400);
+ } catch (JSONException e) {
+ throw new IOException(e);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/resources/SLING-INF/apps/sling.json b/src/main/resources/SLING-INF/apps/sling.json
new file mode 100644
index 0000000..f1d1b47
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/sling.json
@@ -0,0 +1,3 @@
+{
+ "jcr:primaryType":"sling:Folder"
+}
diff --git a/src/main/resources/SLING-INF/apps/sling/validation.json b/src/main/resources/SLING-INF/apps/sling/validation.json
new file mode 100644
index 0000000..f1d1b47
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/sling/validation.json
@@ -0,0 +1,3 @@
+{
+ "jcr:primaryType":"sling:Folder"
+}
diff --git a/src/main/resources/SLING-INF/apps/sling/validation/models.json b/src/main/resources/SLING-INF/apps/sling/validation/models.json
new file mode 100644
index 0000000..f1d1b47
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/sling/validation/models.json
@@ -0,0 +1,3 @@
+{
+ "jcr:primaryType":"sling:Folder"
+}
diff --git a/src/main/resources/SLING-INF/apps/sling/validation/models/user.admin.json b/src/main/resources/SLING-INF/apps/sling/validation/models/user.admin.json
new file mode 100644
index 0000000..03c9ec7
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/sling/validation/models/user.admin.json
@@ -0,0 +1,57 @@
+{
+ "jcr:primaryType": "nt:unstructured",
+ "sling:resourceType": "sling/validation/model",
+ "applicablePaths": [
+ "/content/validationdemo/users/admin"
+ ],
+ "validatedResourceType": "/apps/validationdemo/components/user",
+ "properties": {
+ "jcr:primaryType": "nt:unstructured",
+ "firstName": {
+ "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}+$"]
+ }
+ }
+ },
+ "lastName": {
+ "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}+$"]
+ }
+ }
+ },
+ "username": {
+ "jcr:primaryType": "nt:unstructured",
+ "propertyType": "string",
+ "validators": {
+ "jcr:primaryType": "nt:unstructured",
+ "org.apache.sling.validation.impl.validators.RegexValidator": {
+ "jcr:primaryType": "nt:unstructured",
+ "validatorArguments": ["regex=^[a-z.]+$"]
+ }
+ }
+ }
+ },
+ "children": {
+ "jcr:primaryType": "nt:unstructured",
+ "token": {
+ "jcr:primaryType": "nt:unstructured",
+ "properties": {
+ "jcr:primaryType": "nt:unstructured",
+ "id": {
+ "jcr:primaryType": "nt:unstructured",
+ "propertyType": "string"
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/resources/SLING-INF/apps/sling/validation/models/user.regular.json b/src/main/resources/SLING-INF/apps/sling/validation/models/user.regular.json
new file mode 100644
index 0000000..35ec247
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/sling/validation/models/user.regular.json
@@ -0,0 +1,44 @@
+{
+ "jcr:primaryType": "nt:unstructured",
+ "sling:resourceType": "sling/validation/model",
+ "applicablePaths": [
+ "/content/validationdemo/users/"
+ ],
+ "validatedResourceType": "/apps/validationdemo/components/user",
+ "properties": {
+ "jcr:primaryType": "nt:unstructured",
+ "firstName": {
+ "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}+$"]
+ }
+ }
+ },
+ "lastName": {
+ "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}+$"]
+ }
+ }
+ },
+ "username": {
+ "jcr:primaryType": "nt:unstructured",
+ "propertyType": "string",
+ "validators": {
+ "jcr:primaryType": "nt:unstructured",
+ "org.apache.sling.validation.impl.validators.RegexValidator": {
+ "jcr:primaryType": "nt:unstructured",
+ "validatorArguments": ["regex=^[a-z.]+$"]
+ }
+ }
+ }
+ }
+}
diff --git a/src/main/resources/SLING-INF/apps/validationdemo.json b/src/main/resources/SLING-INF/apps/validationdemo.json
new file mode 100644
index 0000000..f1d1b47
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/validationdemo.json
@@ -0,0 +1,3 @@
+{
+ "jcr:primaryType":"sling:Folder"
+}
diff --git a/src/main/resources/SLING-INF/apps/validationdemo/components.json b/src/main/resources/SLING-INF/apps/validationdemo/components.json
new file mode 100644
index 0000000..f1d1b47
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/validationdemo/components.json
@@ -0,0 +1,3 @@
+{
+ "jcr:primaryType":"sling:Folder"
+}
diff --git a/src/main/resources/SLING-INF/apps/validationdemo/components/user.json b/src/main/resources/SLING-INF/apps/validationdemo/components/user.json
new file mode 100644
index 0000000..f1d1b47
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/validationdemo/components/user.json
@@ -0,0 +1,3 @@
+{
+ "jcr:primaryType":"sling:Folder"
+}
diff --git a/src/main/resources/SLING-INF/apps/validationdemo/components/user/user.jsp b/src/main/resources/SLING-INF/apps/validationdemo/components/user/user.jsp
new file mode 100644
index 0000000..5c585cb
--- /dev/null
+++ b/src/main/resources/SLING-INF/apps/validationdemo/components/user/user.jsp
@@ -0,0 +1,42 @@
+<%@page session="false" import="
+ java.util.Map,
+ java.util.List,
+ org.apache.sling.api.resource.Resource,
+ org.apache.sling.validation.examples.models.UserModel
+ "%>
+<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling" %>
+<sling:defineObjects />
+<%
+ UserModel user = resource.adaptTo(UserModel.class);
+%>
+<table>
+ <tr>
+ <td><strong>Username:</strong></td>
+ <td><%= user.getUsername() %></td>
+ </tr>
+ <tr>
+ <td><strong>First Name:</strong></td>
+ <td><%= user.getFirstName() %></td>
+ </tr>
+ <tr>
+ <td><strong>Last Name:</strong></td>
+ <td><%= user.getLastName() %></td>
+ </tr>
+ <tr>
+ <td><strong>Admin:</strong></td>
+ <td><%= user.isAdmin() %></td>
+ </tr>
+</table>
+<h3>Validation Errors</h3>
+<%
+ for (Map.Entry<String, List<String>> entry : user.getErrors().entrySet()) {
+%>
+<span><strong>Key:</strong> <%= entry.getKey() %></span><br>
+<%
+ for (String message : entry.getValue()) {
+%>
+<span><%= message %></span><br>
+<%
+ }
+ }
+%>
\ No newline at end of file
diff --git a/src/main/resources/SLING-INF/content/validationdemo/users/admin/neo.json b/src/main/resources/SLING-INF/content/validationdemo/users/admin/neo.json
new file mode 100644
index 0000000..9c4048c
--- /dev/null
+++ b/src/main/resources/SLING-INF/content/validationdemo/users/admin/neo.json
@@ -0,0 +1,11 @@
+{
+ "jcr:primaryType":"nt:unstructured",
+ "sling:resourceType":"/apps/validationdemo/components/user",
+ "firstName":"Thomas",
+ "isAdmin":"true",
+ "lastName":"Anderson",
+ "username":"neo",
+ "token" : {
+ "jcr:primaryType":"nt:unstructured"
+ }
+}
diff --git a/src/main/resources/SLING-INF/content/validationdemo/users/johnsmith.json b/src/main/resources/SLING-INF/content/validationdemo/users/johnsmith.json
new file mode 100644
index 0000000..f3b665f
--- /dev/null
+++ b/src/main/resources/SLING-INF/content/validationdemo/users/johnsmith.json
@@ -0,0 +1,8 @@
+{
+ "jcr:primaryType":"nt:unstructured",
+ "sling:resourceType":"/apps/validationdemo/components/user",
+ "firstName":"John",
+ "isAdmin":false,
+ "lastName":"Smith",
+ "username":"johnsmith"
+}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.