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/03/29 09:12:37 UTC

svn commit: r1789300 - in /sling/trunk/bundles/extensions/validation: core/ core/src/main/java/org/apache/sling/validation/impl/postprocessor/ core/src/main/resources/SLING-INF/libs/sling/validation/i18n/ core/src/test/java/org/apache/sling/validation/...

Author: kwin
Date: Wed Mar 29 09:12:37 2017
New Revision: 1789300

URL: http://svn.apache.org/viewvc?rev=1789300&view=rev
Log:
SLING-6515 provide a PostProcessor which validates the newly generated resouce

Added:
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java   (with props)
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java   (with props)
    sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java   (with props)
Modified:
    sling/trunk/bundles/extensions/validation/core/pom.xml
    sling/trunk/bundles/extensions/validation/core/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json
    sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java
    sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java
    sling/trunk/bundles/extensions/validation/core/src/test/resources/logback.xml
    sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json

Modified: sling/trunk/bundles/extensions/validation/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/pom.xml?rev=1789300&r1=1789299&r2=1789300&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/pom.xml (original)
+++ sling/trunk/bundles/extensions/validation/core/pom.xml Wed Mar 29 09:12:37 2017
@@ -55,7 +55,7 @@
                         <!-- embed the commons.osgi bundle as described in http://njbartlett.name/2014/05/26/static-linking.html, 
                              to make this bundle compatible with older versions of Sling -->
                         <Conditional-Package>org.apache.sling.commons.osgi</Conditional-Package>
-                        <Sling-Initial-Content>SLING-INF;overwrite=true</Sling-Initial-Content>
+                        <Sling-Initial-Content>SLING-INF/libs/sling/validation/i18n;overwrite:=true;path:=/libs/sling/validation/i18n</Sling-Initial-Content>
                     </instructions>
                 </configuration>
             </plugin>
@@ -164,6 +164,13 @@
             <version>3.1</version>
             <scope>provided</scope>
         </dependency>
+        <!-- for the SlingPostProcessor (https://issues.apache.org/jira/browse/SLING-594)-->
+        <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.hamcrest</groupId>
             <artifactId>hamcrest-library</artifactId>
@@ -230,13 +237,6 @@
             <version>5.6.2</version>
             <scope>test</scope>
         </dependency>
-        <!-- Apache Sling -->
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.servlets.post</artifactId>
-            <version>2.2.0</version>
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.testing.paxexam</artifactId>
@@ -246,7 +246,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.testing.tools</artifactId>
-            <version>1.0.14</version>
+            <version>1.0.15-SNAPSHOT</version>
             <scope>test</scope>
         </dependency>
         <!-- testing -->

Added: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java?rev=1789300&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java (added)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java Wed Mar 29 09:12:37 2017
@@ -0,0 +1,68 @@
+/*
+ * 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.impl.postprocessor;
+
+import java.text.MessageFormat;
+import java.util.ResourceBundle;
+
+import javax.annotation.Nonnull;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.validation.ValidationFailure;
+import org.apache.sling.validation.ValidationResult;
+
+/** Exception embedding a {@link ValidationResult} from Sling Validation. */
+public class InvalidResourcePostProcessorException extends RuntimeException {
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 213928457248325245L;
+    private final @Nonnull ValidationResult result;
+    private final @Nonnull ResourceBundle resourceBundle;
+    
+    private static final String KEY_MESSAGE= "sling.validator.invalid-resource-post-processor-exception";
+    
+    public InvalidResourcePostProcessorException(@Nonnull ValidationResult result, ResourceBundle resourceBundle) {
+        super();
+        this.result = result;
+        this.resourceBundle = resourceBundle;
+    }
+
+    /** @return the underlying {@link ValidationResult} */
+    public ValidationResult getResult() {
+        return result;
+    }
+
+    public String getMessage() {
+        StringBuilder builder = new StringBuilder();
+        boolean isFirst = true;
+        for (ValidationFailure failure : result.getFailures()) {
+            if (isFirst) {
+                isFirst = false;
+            } else {
+                builder.append(", ");
+            }
+            if (StringUtils.isNotEmpty(failure.getLocation())) {
+                builder.append(failure.getLocation() + " : ");
+            }
+            builder.append(failure.getMessage(resourceBundle));
+        }
+        return MessageFormat.format(resourceBundle.getString(KEY_MESSAGE), builder.toString());
+    }
+}

Propchange: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java?rev=1789300&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java (added)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java Wed Mar 29 09:12:37 2017
@@ -0,0 +1,102 @@
+/*
+ * 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.impl.postprocessor;
+
+import java.util.List;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.servlets.post.Modification;
+import org.apache.sling.servlets.post.SlingPostProcessor;
+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;
+import org.osgi.service.metatype.annotations.Designate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Designate(ocd = ValidationPostProcessorConfiguration.class)
+public class ValidationPostProcessor implements SlingPostProcessor {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ValidationPostProcessor.class);
+
+    private ValidationPostProcessorConfiguration configuration;
+
+    @Reference
+    protected ValidationService validationService;
+
+    protected void activate(ValidationPostProcessorConfiguration configuration) {
+        this.configuration = configuration;
+    }
+
+    private boolean enabledForPath(String path) {
+        // this might be null in case the property is not set (https://osgi.org/bugzilla/show_bug.cgi?id=208)
+        String[] enabledPathPrefixes = configuration.enabledForPathPrefix();
+        if (enabledPathPrefixes == null) {
+            return false;
+        }
+        for (String enabledPathPrefix : enabledPathPrefixes) {
+            if (path.startsWith(enabledPathPrefix)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public void process(SlingHttpServletRequest request, List<Modification> changes) throws Exception {
+        // is this globally disabled?
+        if (configuration.disabled()) {
+            LOG.debug("ValidationPostProcessor globally disabled!");
+            return;
+        }
+        
+        String path = request.getResource().getPath();
+        if (enabledForPath(path)) {
+            LOG.debug("ValidationPostProcessor is enabled for path {}", path);
+        } else {
+            LOG.debug("ValidationPostProcessor is not enabled for path {}", path);
+            return;
+        }
+
+        // request.getResource() contains the old resource (might even be the non-existing one), 
+        // therefore retrieve the transient new resource at the same path
+        Resource newResource = request.getResourceResolver().getResource(request.getResource().getPath());
+        // get model for resource type
+        ValidationModel model = validationService.getValidationModel(newResource, configuration.considerResourceSuperTypes());
+        if (model == null) {
+            if (configuration.failForMissingValidationModels()) {
+                throw new IllegalStateException("Could not find validation model for resource type " + request.getResource().getResourceType());
+            } else {
+                LOG.debug("Could not find validation model for resource type {} -> skip validation", request.getResource().getResourceType());
+                return;
+            }
+        }
+        ValidationResult validationResult = validationService.validate(newResource, model);
+        if (!validationResult.isValid()) {
+            throw new InvalidResourcePostProcessorException(validationResult, request.getResourceBundle(null));
+        } else {
+            LOG.debug("Successfully validated modified/created resource at '{}'", request.getResource().getPath());
+        }
+    }
+
+}

Propchange: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java?rev=1789300&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java (added)
+++ sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java Wed Mar 29 09:12:37 2017
@@ -0,0 +1,35 @@
+/*
+ * 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.impl.postprocessor;
+
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+
+@ObjectClassDefinition(name = "Apache Sling Validation Post Processor", description = "Allows to influence the validation behaviour when using the Sling POST servlet")
+public @interface ValidationPostProcessorConfiguration {
+    @AttributeDefinition(name = "Disabled", description = "If set to true, no POST toward the Sling POST servlet will ever be validated through Sling Validation.")
+    boolean disabled() default false;
+    @AttributeDefinition(name = "Enabled for path prefixes", description = "If not globally disabled, a POST towards a path which starts with any of the given prefixes is automatically validated. No wildcards (*,?) supported.")
+    String[] enabledForPathPrefix();
+    @AttributeDefinition(name = "Consider resource super types", description = "If set to false the Post Processor will not use the validation models of any of the resource super types.")
+    boolean considerResourceSuperTypes() default true;
+    @AttributeDefinition(name = "Fail for missing validation models", description = "In case validation should be performed but no validation model could be found this is either silently ignored (false) or leads to an error (true)")
+    boolean failForMissingValidationModels() default false;
+}

Propchange: sling/trunk/bundles/extensions/validation/core/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sling/trunk/bundles/extensions/validation/core/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json?rev=1789300&r1=1789299&r2=1789300&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json (original)
+++ sling/trunk/bundles/extensions/validation/core/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json Wed Mar 29 09:12:37 2017
@@ -29,5 +29,9 @@
 	"sling.validator.wrong-property-type" : {
 		"jcr:primaryType": "sling:MessageEntry",
 		"sling:message": "Property was expected to be of type \"{0}\" but it cannot be converted to that type."
+	},
+	"sling.validator.invalid-resource-post-processor-exception" : {
+		"jcr:primaryType": "sling:MessageEntry",
+		"sling:message": "Validation errors: {0}"
 	}
 }
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java?rev=1789300&r1=1789299&r2=1789300&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java (original)
+++ sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java Wed Mar 29 09:12:37 2017
@@ -69,7 +69,8 @@ public class ValidationServiceIT extends
         entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
         RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest
                 ("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
-        JSONObject jsonResponse = new JSONObject(re.getContent());
+        String content = re.getContent();
+        JSONObject jsonResponse = new JSONObject(content);
         assertTrue(jsonResponse.getBoolean("valid"));
     }
 
@@ -95,4 +96,21 @@ public class ValidationServiceIT extends
         assertEquals("", failure.get("location")); // location is empty as the property is not found (property name is part of the message rather)
         assertEquals(0, failure.get("severity"));
     }
+    
+    @Test
+    public void testPostProcessorWithInvalidModel() throws IOException, JSONException {
+        MultipartEntity entity = new MultipartEntity();
+        entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
+        entity.addPart("field1", new StringBody("Hello World"));
+        final String url = String.format("http://localhost:%s", httpPort());
+        RequestBuilder requestBuilder = new RequestBuilder(url);
+        // test JSON response, because the HTML response overwrites the original exception (https://issues.apache.org/jira/browse/SLING-6703)
+        RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest
+                ("/content/validated/invalidresource").withEntity(entity).withHeader("Accept", "application/json").withCredentials("admin", "admin")).assertStatus(500);
+        String content = re.getContent();
+        JSONObject jsonResponse = new JSONObject(content);
+        JSONObject error = jsonResponse.getJSONObject("error");
+        assertEquals("org.apache.sling.validation.impl.postprocessor.InvalidResourcePostProcessorException", error.getString("class"));
+        assertEquals("Validation errors: field1 : Property does not match the pattern \"^\\p{Upper}+$\"., Missing required property with name \"field2\".", error.getString("message"));
+    }
 }

Modified: sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java?rev=1789300&r1=1789299&r2=1789300&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java (original)
+++ sling/trunk/bundles/extensions/validation/core/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java Wed Mar 29 09:12:37 2017
@@ -40,6 +40,7 @@ import static org.ops4j.pax.exam.CoreOpt
 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
 import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.factoryConfiguration;
+import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.newConfiguration;
 
 public class ValidationTestSupport extends TestSupport {
 
@@ -74,6 +75,11 @@ public class ValidationTestSupport exten
                     "org.apache.sling.validation.test-services=sling-validation"
                 })
                 .asOption(),
+            // configure post processor
+            newConfiguration("org.apache.sling.validation.impl.postprocessor.ValidationPostProcessor")
+                .put("enabledForPathPrefix", new String[] {"/content/validated"})
+                .put("failForMissingValidationModels", Boolean.TRUE)
+                .asOption(),
             // testing
             mavenBundle().groupId("org.apache.sling").artifactId("org.apache.sling.testing.tools").versionAsInProject(),
             mavenBundle().groupId("org.apache.servicemix.bundles").artifactId("org.apache.servicemix.bundles.hamcrest").versionAsInProject(),

Modified: sling/trunk/bundles/extensions/validation/core/src/test/resources/logback.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/core/src/test/resources/logback.xml?rev=1789300&r1=1789299&r2=1789300&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/core/src/test/resources/logback.xml (original)
+++ sling/trunk/bundles/extensions/validation/core/src/test/resources/logback.xml Wed Mar 29 09:12:37 2017
@@ -24,7 +24,9 @@
       <pattern>%date %level [%thread] %logger{10} [%file : %line] %msg%n</pattern>
     </encoder>
   </appender>
-  <root level="debug">
+  <logger name="org.apache.sling.validation" level="DEBUG"/>
+  
+  <root level="INFO">
     <appender-ref ref="file"/>
   </root>
 </configuration>

Modified: sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json?rev=1789300&r1=1789299&r2=1789300&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json (original)
+++ sling/trunk/bundles/extensions/validation/test-services/src/main/resources/SLING-CONTENT/apps/sling/validation/models/model1.json Wed Mar 29 09:12:37 2017
@@ -1,7 +1,8 @@
 {
     "validatedResourceType": "validation/test/resourceType1",
     "applicablePaths": [
-        "/validation/testing/fakeFolder1"
+        "/validation/testing/fakeFolder1",
+		"/content/validated"
     ],
     "sling:resourceType": "sling/validation/model",
     "properties": {