Cancel form submission if a listener has failed
authorMathieu <mbaudier@argeo.org>
Thu, 1 Dec 2022 07:22:40 +0000 (08:22 +0100)
committerMathieu <mbaudier@argeo.org>
Thu, 1 Dec 2022 07:22:40 +0000 (08:22 +0100)
org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java
org.argeo.app.core/src/org/argeo/app/xforms/FormSubmissionListener.java
org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java
swt/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java

index 7fd308de8d1e6c87b21113b0aaf9b20d223e159f..72c334f31b63bea9c5a8ac56c47dab0be6403e1a 100644 (file)
@@ -66,9 +66,9 @@ public class ImageProcessor {
                                                Files.deleteIfExists(temp);
                                        }
                                } else {
-                                       try (OutputStream out = outSupplier.call()) {
-                                               copyWithMetadata(() -> in, metadata);
-                                       }
+//                                     try (OutputStream out = outSupplier.call()) {
+                                       copyWithMetadata(() -> in, metadata);
+//                                     }
                                }
                        }
                } catch (Exception e) {
index 71228926d0bb075c70f2aa079f7435a628cce8f8..0dff64c772ec9ec1f6a894fb203a370065fe252a 100644 (file)
@@ -5,6 +5,9 @@ import javax.jcr.RepositoryException;
 
 /** Called when a user has received a new form submission. */
 public interface FormSubmissionListener {
-       /** Called after a form submission has been stored in the user area. */
+       /**
+        * Called after a form submission has been stored in the user area. The
+        * submission will be deleted if any exception is thrown.
+        */
        void formSubmissionReceived(Node node) throws RepositoryException;
 }
index a5864d83336206d6e81c7229c05aea3febb3ef97..8dd8b0b7ddcbdcd5f31f04dfc93b9a3255b81619 100644 (file)
@@ -14,7 +14,6 @@ import javax.jcr.ImportUUIDBehavior;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.Repository;
-import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.nodetype.NodeType;
 import javax.servlet.ServletException;
@@ -60,24 +59,24 @@ public class OdkSubmissionServlet extends HttpServlet {
                RemoteAuthRequest request = new ServletHttpRequest(req);
                Session session = RemoteAuthUtils.doAs(() -> Jcr.login(repository, null), request);
 
-               try {
-                       CmsSession cmsSession = RemoteAuthUtils.getCmsSession(request);
+               CmsSession cmsSession = RemoteAuthUtils.getCmsSession(request);
 
-                       Session adminSession = null;
-                       try {
-                               // TODO centralise at a deeper level
-                               adminSession = CmsJcrUtils.openDataAdminSession(repository, null);
-                               SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
-                       } finally {
-                               Jcr.logout(adminSession);
-                       }
+               Session adminSession = null;
+               try {
+                       // TODO centralise at a deeper level
+                       adminSession = CmsJcrUtils.openDataAdminSession(repository, null);
+                       SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
+               } finally {
+                       Jcr.logout(adminSession);
+               }
 
+               try {
                        Node cmsSessionNode = SuiteUtils.getCmsSessionNode(session, cmsSession);
                        Node submission = cmsSessionNode.addNode(submissionNameFormatter.format(Instant.now()),
                                        OrxType.submission.get());
                        for (Part part : req.getParts()) {
-                               if (log.isDebugEnabled())
-                                       log.debug("Part: " + part.getName() + ", " + part.getContentType());
+                               if (log.isTraceEnabled())
+                                       log.trace("Part: " + part.getName() + ", " + part.getContentType());
 
                                if (part.getName().equals(XML_SUBMISSION_FILE)) {
                                        Node xml = submission.addNode(XML_SUBMISSION_FILE, NodeType.NT_UNSTRUCTURED);
@@ -112,12 +111,22 @@ public class OdkSubmissionServlet extends HttpServlet {
                                        }
                                }
                        }
+
                        session.save();
-                       for (FormSubmissionListener submissionListener : submissionListeners) {
-                               submissionListener.formSubmissionReceived(submission);
+                       try {
+                               for (FormSubmissionListener submissionListener : submissionListeners) {
+                                       submissionListener.formSubmissionReceived(submission);
+                               }
+                       } catch (Exception e) {
+                               log.error("Cannot save submision, cancelling...", e);
+                               submission.remove();
+                               session.save();
+                               resp.setStatus(503);
+                               return;
                        }
-               } catch (RepositoryException e) {
-                       e.printStackTrace();
+
+               } catch (Exception e) {
+                       log.error("Cannot save submision", e);
                        resp.setStatus(503);
                        return;
                } finally {
index 59d11343285ed30c6e605906ffe9752cc8258e68..504e8eda75c87f6ea07a5b91508cad0c93bc35c0 100644 (file)
@@ -242,7 +242,8 @@ public class SuiteUiUtils {
                boolean test = false;
                if (test) {
                        try (InputStream in = JcrUtils.getFileAsStream(fileNode);
-                                       OutputStream out = Files.newOutputStream(Paths.get("/home/mbaudier/tmp/" + fileNode.getName()));) {
+                                       OutputStream out = Files.newOutputStream(
+                                                       Paths.get(System.getProperty("user.home") + "/tmp/" + fileNode.getName()));) {
 //                             BufferedImage img = ImageIO.read(in);
 //                             System.out.println(fileNode.getName() + ": width=" + img.getWidth() + ", height=" + img.getHeight());
                                IOUtils.copy(in, out);