SLING-6515 provide a PostProcessor which validates the newly generated resouce

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1789300 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 095079d..a441312 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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 -->
diff --git a/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java b/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java
new file mode 100644
index 0000000..861fbf8
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/impl/postprocessor/InvalidResourcePostProcessorException.java
@@ -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());
+    }
+}
diff --git a/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java b/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java
new file mode 100644
index 0000000..214de8d
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessor.java
@@ -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());
+        }
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java b/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java
new file mode 100644
index 0000000..dc07af3
--- /dev/null
+++ b/src/main/java/org/apache/sling/validation/impl/postprocessor/ValidationPostProcessorConfiguration.java
@@ -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;
+}
diff --git a/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json b/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json
index a8f10f1..e24da61 100644
--- a/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json
+++ b/src/main/resources/SLING-INF/libs/sling/validation/i18n/en.json
@@ -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
diff --git a/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java b/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java
index 758351b..3c5fe28 100644
--- a/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java
+++ b/src/test/java/org/apache/sling/validation/core/it/tests/ValidationServiceIT.java
@@ -69,7 +69,8 @@
         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 @@
         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"));
+    }
 }
diff --git a/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java b/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java
index 95d8db5..560cc4e 100644
--- a/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java
+++ b/src/test/java/org/apache/sling/validation/core/it/tests/ValidationTestSupport.java
@@ -40,6 +40,7 @@
 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 @@
                     "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(),
diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml
index d46a4ae..699879a 100644
--- a/src/test/resources/logback.xml
+++ b/src/test/resources/logback.xml
@@ -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>