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 2021/12/02 16:24:56 UTC
[sling-org-apache-sling-validation-examples] branch master updated: Update to parent 46
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-validation-examples.git
The following commit(s) were added to refs/heads/master by this push:
new 162c2b0 Update to parent 46
162c2b0 is described below
commit 162c2b08b0a900edaea057ee855b25b37dded16a
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Thu Dec 2 17:24:48 2021 +0100
Update to parent 46
Use javax.json for JSON writing
---
README.md | 14 ++---
pom.xml | 69 ++++++++--------------
.../validation/examples/models/UserModel.java | 1 -
.../validation/examples/models/package-info.java | 20 +++++++
.../examples/servlets/ModifyUserServlet.java | 42 +++++++------
5 files changed, 75 insertions(+), 71 deletions(-)
diff --git a/README.md b/README.md
index 01dd1f9..263535a 100644
--- a/README.md
+++ b/README.md
@@ -7,15 +7,11 @@
This module is part of the [Apache Sling](https://sling.apache.org) project.
## 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:
+1. Start a Sling Starter (or some other distribution)
+2. Install the `org.apache.sling.validation.api` and `org.apache.sling.validation.core` bundles
+3. Build and install the `org.apache.sling.validation.examples` bundle
```bash
- cd ../api
- mvn clean package sling:install
- cd ../core
- mvn clean package sling:install
- cd ../examples
mvn clean package sling:install
```
@@ -24,11 +20,11 @@ This module is part of the [Apache Sling](https://sling.apache.org) project.
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.
+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.
+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
index e937c4a..5468ee0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,14 +21,13 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.sling</groupId>
- <artifactId>sling</artifactId>
- <version>30</version>
+ <artifactId>sling-bundle-parent</artifactId>
+ <version>46</version>
<relativePath />
</parent>
<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>
@@ -39,30 +38,18 @@
<url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-validation-examples.git</url>
</scm>
+ <properties>
+ <sling.java.version>8</sling.java.version>
+ <project.build.outputTimestamp>2020-01-22T15:10:38Z</project.build.outputTimestamp>
+ </properties>
+
<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>
+ <groupId>biz.aQute.bnd</groupId>
+ <artifactId>bnd-baseline-maven-plugin</artifactId>
<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>
- <Embed-Dependency>
- org.apache.felix.utils;inline=org/apache/felix/utils/json/JSONWriter.class
- </Embed-Dependency>
- </instructions>
+ <skip>true</skip>
</configuration>
</plugin>
</plugins>
@@ -75,52 +62,46 @@
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
<groupId>org.osgi</groupId>
- <artifactId>osgi.cmpn</artifactId>
+ <artifactId>org.osgi.service.component.annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>osgi.core</artifactId>
+ <artifactId>org.osgi.annotation.versioning</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.api</artifactId>
- <version>2.4.2</version>
+ <artifactId>org.apache.sling.servlets.annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
+ <groupId>javax.servlet</groupId>
+ <artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.scr.annotations</artifactId>
+ <groupId>javax.json</groupId>
+ <artifactId>javax.json-api</artifactId>
+ <version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.osgi</artifactId>
- <version>2.2.0</version>
+ <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.models.api</artifactId>
- <version>1.1.0</version>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>org.apache.felix</groupId>
- <artifactId>org.apache.felix.utils</artifactId>
- <version>1.9.0</version>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.models.api</artifactId>
+ <version>1.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
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
index 5cff7c3..423d817 100644
--- a/src/main/java/org/apache/sling/validation/examples/models/UserModel.java
+++ b/src/main/java/org/apache/sling/validation/examples/models/UserModel.java
@@ -20,7 +20,6 @@ package org.apache.sling.validation.examples.models;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
diff --git a/src/main/java/org/apache/sling/validation/examples/models/package-info.java b/src/main/java/org/apache/sling/validation/examples/models/package-info.java
new file mode 100644
index 0000000..d8c13c9
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/examples/models/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+@Version("1.0.0")
+package org.apache.sling.validation.examples.models;
+
+import org.osgi.annotation.versioning.Version;
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
index 71a5806..9ed18f9 100644
--- a/src/main/java/org/apache/sling/validation/examples/servlets/ModifyUserServlet.java
+++ b/src/main/java/org/apache/sling/validation/examples/servlets/ModifyUserServlet.java
@@ -19,32 +19,39 @@
package org.apache.sling.validation.examples.servlets;
import java.io.IOException;
-import java.util.List;
import java.util.Locale;
-import java.util.Map;
+import javax.json.Json;
+import javax.json.stream.JsonGenerator;
+import javax.servlet.Servlet;
import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.sling.SlingServlet;
-import org.apache.felix.utils.json.JSONWriter;
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.servlets.annotations.SlingServletResourceTypes;
import org.apache.sling.validation.ValidationFailure;
import org.apache.sling.validation.ValidationResult;
import org.apache.sling.validation.ValidationService;
import org.apache.sling.validation.model.ValidationModel;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
-@SlingServlet(
+@SlingServletResourceTypes(
resourceTypes = "/apps/validationdemo/components/user",
selectors = {"modify"},
methods = "POST"
)
+@Component(service = Servlet.class)
public class ModifyUserServlet extends SlingAllMethodsServlet {
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
@Reference
private ValidationService validationService;
@@ -67,19 +74,20 @@ public class ModifyUserServlet extends SlingAllMethodsServlet {
request.getRequestDispatcher(request.getResource(), options).forward(request, response);
} else {
response.setContentType("application/json");
- JSONWriter writer = new JSONWriter(response.getWriter());
- writer.object();
- writer.key("success").value(false);
- writer.key("failures").array();
+ JsonGenerator generator = Json.createGenerator(response.getWriter());
+
+ generator.writeStartObject();
+ generator.write("success", false);
+ generator.writeStartArray("failures");
for (ValidationFailure failure : vr.getFailures()) {
- writer.object();
- writer.key("message").value(failure.getMessage(request.getResourceBundle(Locale.US)));
- writer.key("location").value(failure.getLocation());
- writer.endObject();
+ generator.writeStartObject();
+ generator.write("message", failure.getMessage(request.getResourceBundle(Locale.US)));
+ generator.write("location", failure.getLocation());
+ generator.writeEnd();
}
- writer.endArray();
- writer.endObject();
- response.setStatus(400);
+ generator.writeEnd();
+ generator.writeEnd();
+ response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
}