From b412aef36bda3f0a9e5ed73ab2206db0f819afa9 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Thu, 1 Dec 2022 08:22:40 +0100 Subject: [PATCH] Cancel form submission if a listener has failed --- .../org/argeo/app/image/ImageProcessor.java | 6 +-- .../app/xforms/FormSubmissionListener.java | 5 ++- .../app/servlet/odk/OdkSubmissionServlet.java | 43 +++++++++++-------- .../src/org/argeo/app/ui/SuiteUiUtils.java | 3 +- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java b/org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java index 7fd308d..72c334f 100644 --- a/org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java +++ b/org.argeo.app.core/src/org/argeo/app/image/ImageProcessor.java @@ -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) { diff --git a/org.argeo.app.core/src/org/argeo/app/xforms/FormSubmissionListener.java b/org.argeo.app.core/src/org/argeo/app/xforms/FormSubmissionListener.java index 7122892..0dff64c 100644 --- a/org.argeo.app.core/src/org/argeo/app/xforms/FormSubmissionListener.java +++ b/org.argeo.app.core/src/org/argeo/app/xforms/FormSubmissionListener.java @@ -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; } diff --git a/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java b/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java index a5864d8..8dd8b0b 100644 --- a/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java +++ b/org.argeo.app.servlet.odk/src/org/argeo/app/servlet/odk/OdkSubmissionServlet.java @@ -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 { diff --git a/swt/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java b/swt/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java index 59d1134..504e8ed 100644 --- a/swt/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java +++ b/swt/org.argeo.app.ui/src/org/argeo/app/ui/SuiteUiUtils.java @@ -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); -- 2.30.2