]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/attachment/GetAttachmentHandler.java
Stabilize attachments and events
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / attachment / GetAttachmentHandler.java
1 package org.argeo.slc.web.mvc.attachment;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.ServletOutputStream;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.apache.commons.io.FileUtils;
11 import org.apache.commons.io.FilenameUtils;
12 import org.argeo.slc.core.attachment.AttachmentsStorage;
13 import org.argeo.slc.core.attachment.SimpleAttachment;
14 import org.springframework.web.HttpRequestHandler;
15
16 /** Returns one single result. */
17 public class GetAttachmentHandler implements HttpRequestHandler {
18 protected final String FORCE_DOWNLOAD = "Content-Type: application/force-download";
19
20 private AttachmentsStorage attachmentsStorage;
21
22 public void handleRequest(HttpServletRequest request,
23 HttpServletResponse response) throws ServletException, IOException {
24 String uuid = request.getParameter("uuid");
25 String contentType = request.getParameter("contentType");
26 String name = request.getParameter("name");
27 if (contentType == null || "".equals(contentType.trim())) {
28 if (name != null) {
29 contentType = FORCE_DOWNLOAD;
30 String ext = FilenameUtils.getExtension(name);
31 // cf. http://en.wikipedia.org/wiki/Internet_media_type
32 if ("csv".equals(ext))
33 contentType = "text/csv";
34 else if ("pdf".equals(ext))
35 contentType = "application/pdf";
36 else if ("zip".equals(ext))
37 contentType = "application/zip";
38 else if ("html".equals(ext))
39 contentType = "application/html";
40 else if ("txt".equals(ext))
41 contentType = "text/plain";
42 else if ("doc".equals(ext) || "docx".equals(ext))
43 contentType = "application/msword";
44 else if ("xls".equals(ext) || "xlsx".equals(ext))
45 contentType = "application/vnd.ms-excel";
46 else if ("xml".equals(ext))
47 contentType = "text/xml";
48 }
49 }
50
51 if (name != null) {
52 contentType = contentType + ";name=\"" + name + "\"";
53 response.setHeader("Content-Disposition", "attachment; filename=\""
54 + name + "\"");
55 }
56 response.setHeader("Expires", "0");
57 response.setHeader("Cache-Control", "no-cache, must-revalidate");
58 response.setHeader("Pragma", "no-cache");
59
60 SimpleAttachment resourceDescriptor = new SimpleAttachment();
61 resourceDescriptor.setUuid(uuid);
62 resourceDescriptor.setContentType(contentType);
63
64 response.setContentType(contentType);
65 ServletOutputStream outputStream = response.getOutputStream();
66 attachmentsStorage.retrieveAttachment(resourceDescriptor, outputStream);
67 }
68
69 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
70 this.attachmentsStorage = attachmentsStorage;
71 }
72
73 }