Introduce graphical support based on SVG naming.
[gpl/argeo-suite.git] / knowledge / org.argeo.support.odk / src / org / argeo / support / odk / servlet / OdkSubmissionServlet.java
1 package org.argeo.support.odk.servlet;
2
3 import java.io.IOException;
4 import java.time.Instant;
5 import java.time.ZoneId;
6 import java.time.ZoneOffset;
7 import java.time.format.DateTimeFormatter;
8
9 import javax.jcr.ImportUUIDBehavior;
10 import javax.jcr.Node;
11 import javax.jcr.Property;
12 import javax.jcr.Repository;
13 import javax.jcr.RepositoryException;
14 import javax.jcr.Session;
15 import javax.jcr.nodetype.NodeType;
16 import javax.servlet.ServletException;
17 import javax.servlet.http.HttpServlet;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20 import javax.servlet.http.Part;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.argeo.cms.servlet.ServletAuthUtils;
25 import org.argeo.entity.EntityNames;
26 import org.argeo.entity.EntityType;
27 import org.argeo.jcr.Jcr;
28 import org.argeo.jcr.JcrUtils;
29 import org.argeo.support.odk.OrxType;
30
31 /** Receives a form submission. */
32 public class OdkSubmissionServlet extends HttpServlet {
33         private static final long serialVersionUID = 7834401404691302385L;
34         private final static Log log = LogFactory.getLog(OdkSubmissionServlet.class);
35
36         private final static String XML_SUBMISSION_FILE = "xml_submission_file";
37
38         private DateTimeFormatter submissionNameFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd-HHmmssSSS")
39                         .withZone(ZoneId.from(ZoneOffset.UTC));
40
41         private Repository repository;
42
43         @Override
44         protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
45                 resp.setContentType("text/xml; charset=utf-8");
46                 resp.setHeader("X-OpenRosa-Version", "1.0");
47                 resp.setDateHeader("Date", System.currentTimeMillis());
48                 resp.setIntHeader("X-OpenRosa-Accept-Content-Length", 1024 * 1024);
49
50                 Session session = ServletAuthUtils.doAs(() -> Jcr.login(repository, null), req);
51
52                 try {
53                         Node submissions = JcrUtils.mkdirs(session,
54                                         "/" + EntityType.form.get() + "/" + EntityNames.SUBMISSIONS_BASE);
55                         Node submission = submissions.addNode(submissionNameFormatter.format(Instant.now()),
56                                         OrxType.submission.get());
57                         for (Part part : req.getParts()) {
58                                 if (log.isDebugEnabled())
59                                         log.debug("Part: " + part.getName() + ", " + part.getContentType());
60
61                                 if (part.getName().equals(XML_SUBMISSION_FILE)) {
62                                         Node xml = submission.addNode(XML_SUBMISSION_FILE, NodeType.NT_UNSTRUCTURED);
63                                         session.importXML(xml.getPath(), part.getInputStream(),
64                                                         ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
65
66                                 } else {
67                                         Node fileNode = JcrUtils.copyStreamAsFile(submission, part.getName(), part.getInputStream());
68                                         String contentType = part.getContentType();
69                                         if (contentType != null) {
70                                                 fileNode.addMixin(NodeType.MIX_MIMETYPE);
71                                                 fileNode.setProperty(Property.JCR_MIMETYPE, contentType);
72
73                                         }
74                                         if (part.getName().endsWith(".jpg") || part.getName().endsWith(".png")) {
75                                                 // TODO meta data and thumbnails
76                                         }
77                                 }
78                         }
79                         session.save();
80                 } catch (RepositoryException e) {
81                         e.printStackTrace();
82                         resp.setStatus(503);
83                         return;
84                 }
85
86                 resp.setStatus(201);
87                 resp.getWriter().write("<OpenRosaResponse xmlns=\"http://openrosa.org/http/response\">"
88                                 + "<message>Form Received!</message>" + "</OpenRosaResponse>");
89
90         }
91
92         public void setRepository(Repository repository) {
93                 this.repository = repository;
94         }
95
96 }