93f41479ded820342874e326a9ced9631af4db66
[gpl/argeo-suite.git] / knowledge / org.argeo.support.odk / src / org / argeo / support / odk / servlet / OdkFormServlet.java
1 package org.argeo.support.odk.servlet;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Map;
9
10 import javax.jcr.Repository;
11 import javax.jcr.RepositoryException;
12 import javax.jcr.Session;
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17
18 import org.apache.commons.io.FilenameUtils;
19 import org.argeo.cms.auth.ServletAuthUtils;
20 import org.argeo.jcr.Jcr;
21 import org.argeo.support.odk.OdkForm;
22
23 /** Retrieves a single form. */
24 public class OdkFormServlet extends HttpServlet {
25         private static final long serialVersionUID = 7838305967987687370L;
26
27         private Repository repository;
28         private Map<String, OdkForm> odkForms = Collections.synchronizedMap(new HashMap<>());
29
30         @Override
31         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
32                 resp.setContentType("text/xml; charset=utf-8");
33
34                 Session session = ServletAuthUtils.doAs(() -> Jcr.login(repository, null), req);
35
36                 String pathInfo = req.getPathInfo();
37
38                 try {
39                         if (session.nodeExists(pathInfo)) {
40                                 session.exportDocumentView(pathInfo + "/h:html", resp.getOutputStream(), true, false);
41                         } else {
42
43                                 String fileName = FilenameUtils.getName(pathInfo);
44                                 OdkForm form = odkForms.get(fileName);
45                                 if (form == null)
46                                         throw new IllegalArgumentException("No form named " + fileName + " was found");
47
48                                 byte[] buffer = new byte[1024];
49                                 try (InputStream in = form.openStream(); OutputStream out = resp.getOutputStream();) {
50                                         int bytesRead;
51                                         while ((bytesRead = in.read(buffer)) != -1)
52                                                 out.write(buffer, 0, bytesRead);
53                                 }
54                         }
55                 } catch (RepositoryException e) {
56                         e.printStackTrace();
57                         // TODO error message
58                         resp.sendError(500);
59                 } finally {
60                         Jcr.logout(session);
61                 }
62         }
63
64         public void addForm(OdkForm odkForm) {
65                 odkForms.put(odkForm.getFileName(), odkForm);
66         }
67
68         public void removeForm(OdkForm odkForm) {
69                 odkForms.remove(odkForm.getFileName());
70         }
71
72         public void setRepository(Repository repository) {
73                 this.repository = repository;
74         }
75
76 }