This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-post.git


The following commit(s) were added to refs/heads/master by this push:
     new d5ab02b  SLING-10087 convert more persistenceexceptions (#11)
d5ab02b is described below

commit d5ab02be070ce57bdafb7ac03ff575bfe986f727
Author: Jörg Hoh <[email protected]>
AuthorDate: Thu Apr 1 18:43:47 2021 +0200

    SLING-10087 convert more persistenceexceptions (#11)
    
    within the PostServlet, do not set the response code directly, but throw an 
PersistenceException instead
---
 .../apache/sling/servlets/post/PostOperation.java  |  4 +++-
 .../PreconditionViolatedPersistenceException.java  |  4 ++++
 .../sling/servlets/post/impl/SlingPostServlet.java | 16 +++++++--------
 .../impl/helper/SlingPropertyValueHandler.java     |  3 ++-
 .../impl/operations/AbstractPostOperation.java     | 23 +++++++++++-----------
 5 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/src/main/java/org/apache/sling/servlets/post/PostOperation.java 
b/src/main/java/org/apache/sling/servlets/post/PostOperation.java
index e2b42b3..76a7131 100644
--- a/src/main/java/org/apache/sling/servlets/post/PostOperation.java
+++ b/src/main/java/org/apache/sling/servlets/post/PostOperation.java
@@ -19,6 +19,7 @@
 package org.apache.sling.servlets.post;
 
 import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.PersistenceException;
 import 
org.apache.sling.servlets.post.exceptions.PreconditionViolatedPersistenceException;
 import org.apache.sling.servlets.post.exceptions.TemporaryPersistenceException;
 
@@ -74,6 +75,7 @@ public interface PostOperation {
      * @param processors The {@link SlingPostProcessor} services to be called
      *            after applying the operation. This may be <code>null</code> 
if
      *            there are none.
+     * @throws PersistenceException 
      * @throws org.apache.sling.api.resource.ResourceNotFoundException May be
      *             thrown if the operation requires an existing request
      *             resource. If this exception is thrown the Sling POST servlet
@@ -83,5 +85,5 @@ public interface PostOperation {
      *             occurrs running the operation.
      */
     void run(SlingHttpServletRequest request, PostResponse response,
-            SlingPostProcessor[] processors) throws 
PreconditionViolatedPersistenceException, TemporaryPersistenceException;
+            SlingPostProcessor[] processors) throws 
PreconditionViolatedPersistenceException, TemporaryPersistenceException, 
PersistenceException;
 }
diff --git 
a/src/main/java/org/apache/sling/servlets/post/exceptions/PreconditionViolatedPersistenceException.java
 
b/src/main/java/org/apache/sling/servlets/post/exceptions/PreconditionViolatedPersistenceException.java
index 459c69e..3b6253b 100644
--- 
a/src/main/java/org/apache/sling/servlets/post/exceptions/PreconditionViolatedPersistenceException.java
+++ 
b/src/main/java/org/apache/sling/servlets/post/exceptions/PreconditionViolatedPersistenceException.java
@@ -43,6 +43,10 @@ public class PreconditionViolatedPersistenceException 
extends PersistenceExcepti
             super(msg,cause,resourcePath,propertyName);
             
             }
+
+public PreconditionViolatedPersistenceException(String msg) {
+    super(msg);
+}
   
 
 }
diff --git 
a/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java 
b/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
index 4f378a0..55fffb6 100644
--- a/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
+++ b/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
@@ -245,8 +245,8 @@ public class SlingPostServlet extends 
SlingAllMethodsServlet {
                 htmlResponse.setStatus(HttpServletResponse.SC_NOT_FOUND,
                     rnfe.getMessage());
             } catch (final PreconditionViolatedPersistenceException e) {
-                log.warn("Exception while handling POST {} with {}",
-                        new Object[] 
{request.getResource().getPath(),operation.getClass().getName()},e);
+                log.warn("Exception while handling POST on path [{}] with 
operation [{}]",
+                        
request.getResource().getPath(),operation.getClass().getName(),e);
                 if (backwardsCompatibleStatuscode) {
                     htmlResponse.setError(e);
                 } else {
@@ -254,17 +254,17 @@ public class SlingPostServlet extends 
SlingAllMethodsServlet {
                 }
             } catch (final PersistenceException e) {
                 // also catches the  RetryableOperationException, as the 
handling is the same
-                log.warn("Exception while handling POST {} with {}",
-                        new Object[] 
{request.getResource().getPath(),operation.getClass().getName()},e);
+                log.warn("Exception while handling POST on path [{}] with 
operation [{}]",
+                        
request.getResource().getPath(),operation.getClass().getName(),e);
                 if (backwardsCompatibleStatuscode) {
                     htmlResponse.setError(e);
                 } else {
                     htmlResponse.setStatus(HttpServletResponse.SC_CONFLICT, 
"repository state conflicting with request");
                 }
-            } catch (final Exception exception) {
-                log.warn("Exception while handling POST {} with {}",
-                        new Object[] 
{request.getResource().getPath(),operation.getClass().getName()},exception);
-                htmlResponse.setError(exception);
+            } catch (final Exception e) {
+                log.warn("Exception while handling POST on path [{}] with 
operation [{}]",
+                        
request.getResource().getPath(),operation.getClass().getName(),e);
+                htmlResponse.setError(e);
             }
 
         }
diff --git 
a/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingPropertyValueHandler.java
 
b/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingPropertyValueHandler.java
index c5c69fb..50abeb8 100644
--- 
a/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingPropertyValueHandler.java
+++ 
b/src/main/java/org/apache/sling/servlets/post/impl/helper/SlingPropertyValueHandler.java
@@ -32,6 +32,7 @@ import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.servlets.post.Modification;
 import org.apache.sling.servlets.post.SlingPostConstants;
+import 
org.apache.sling.servlets.post.exceptions.PreconditionViolatedPersistenceException;
 
 /**
  * Sets a property on the given resource, in some cases with a specific type 
and
@@ -111,7 +112,7 @@ public class SlingPropertyValueHandler {
         mod.node = jcrSupport.getNode(parent);
         mod.valueMap = parent.adaptTo(ModifiableValueMap.class);
         if ( mod.valueMap == null ) {
-            throw new PersistenceException("Resource at '" + parent.getPath() 
+ "' is not modifiable.");
+            throw new PreconditionViolatedPersistenceException("Resource at '" 
+ parent.getPath() + "' is not modifiable.");
         }
 
         final String name = prop.getName();
diff --git 
a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
 
b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
index 493bf71..0c0bdb4 100644
--- 
a/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
+++ 
b/src/main/java/org/apache/sling/servlets/post/impl/operations/AbstractPostOperation.java
@@ -83,7 +83,7 @@ public abstract class AbstractPostOperation implements 
PostOperation {
     @Override
     public void run(final SlingHttpServletRequest request,
                     final PostResponse response,
-                    final SlingPostProcessor[] processors) throws 
PreconditionViolatedPersistenceException, TemporaryPersistenceException {
+                    final SlingPostProcessor[] processors) throws 
PreconditionViolatedPersistenceException, TemporaryPersistenceException, 
PersistenceException {
         final VersioningConfiguration versionableConfiguration = 
getVersioningConfiguration(request);
 
         try {
@@ -105,10 +105,16 @@ public abstract class AbstractPostOperation implements 
PostOperation {
             doRun(request, response, changes);
 
             // invoke processors
-            if (processors != null) {
-                for (SlingPostProcessor processor : processors) {
-                    processor.process(request, changes);
+            try {
+                if (processors != null) {
+                    for (SlingPostProcessor processor : processors) {
+                        processor.process(request, changes);
+                    }
                 }
+            } catch 
(PreconditionViolatedPersistenceException|TemporaryPersistenceException e) {
+                throw e;
+            } catch (Exception e) {
+                throw new PersistenceException("Exception during response 
processing",e);
             }
 
             // check modifications for remaining postfix and store the base 
path
@@ -129,10 +135,8 @@ public abstract class AbstractPostOperation implements 
PostOperation {
             if (modificationSourcesContainingPostfix.size() > 0) {
                 for (final Map.Entry<String, String> sourceToCheck : 
modificationSourcesContainingPostfix.entrySet()) {
                     if 
(allModificationSources.contains(sourceToCheck.getKey())) {
-                        
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
-                                "Postfix-containing path " + 
sourceToCheck.getValue() +
+                        throw new PersistenceException("Postfix-containing 
path " + sourceToCheck.getValue() +
                                 " contained in the modification list. Check 
configuration.");
-                        return;
                     }
                 }
             }
@@ -179,11 +183,6 @@ public abstract class AbstractPostOperation implements 
PostOperation {
                 }
             }
 
-        } catch (Exception e) {
-
-            log.error("Exception during response processing.", e);
-            response.setError(e);
-
         } finally {
             if (isResourceResolverCommitRequired(request)) {
                 request.getResourceResolver().revert();

Reply via email to