package org.argeo.app.servlet.odk;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
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;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
+import org.argeo.api.cms.CmsLog;
import org.argeo.api.cms.CmsSession;
import org.argeo.app.core.SuiteUtils;
+import org.argeo.app.image.ImageProcessor;
import org.argeo.app.odk.OrxType;
import org.argeo.app.xforms.FormSubmissionListener;
-import org.argeo.api.cms.CmsLog;
import org.argeo.cms.auth.RemoteAuthRequest;
import org.argeo.cms.auth.RemoteAuthUtils;
import org.argeo.cms.jcr.CmsJcrUtils;
RemoteAuthRequest request = new ServletHttpRequest(req);
Session session = RemoteAuthUtils.doAs(() -> Jcr.login(repository, null), request);
- try {
-// Node submissions = JcrUtils.mkdirs(session,
-// "/" + EntityType.form.get() + "/" + EntityNames.SUBMISSIONS_BASE);
- CmsSession cmsSession = RemoteAuthUtils.getCmsSession(request);
+ CmsSession cmsSession = RemoteAuthUtils.getCmsSession(request);
-// ClassLoader currentContextCl = Thread.currentThread().getContextClassLoader();
-// Thread.currentThread().setContextClassLoader(CmsJcrUtils.class.getClassLoader());
- Session adminSession = null;
- try {
- // TODO centralise at a deeper level
- adminSession = CmsJcrUtils.openDataAdminSession(repository, null);
- SuiteUtils.getOrCreateCmsSessionNode(adminSession, cmsSession);
- } finally {
- Jcr.logout(adminSession);
-// Thread.currentThread().setContextClassLoader(currentContextCl);
- }
+ 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);
ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
} else {
- Node fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(), part.getInputStream());
+ Node fileNode;
+ if (part.getName().endsWith(".jpg")) {
+ // Fix metadata
+ Path temp = Files.createTempFile("image", ".jpg");
+ try {
+ ImageProcessor imageProcessor = new ImageProcessor(() -> part.getInputStream(),
+ () -> Files.newOutputStream(temp));
+ imageProcessor.process();
+ fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(),
+ Files.newInputStream(temp));
+ } finally {
+ Files.deleteIfExists(temp);
+ }
+ } else {
+ fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(), part.getInputStream());
+ }
String contentType = part.getContentType();
if (contentType != null) {
fileNode.addMixin(NodeType.MIX_MIMETYPE);
}
}
}
+
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 {