1 package org
.argeo
.slc
.web
.mvc
.attachment
;
3 import java
.io
.IOException
;
5 import javax
.servlet
.ServletException
;
6 import javax
.servlet
.ServletOutputStream
;
7 import javax
.servlet
.http
.HttpServletRequest
;
8 import javax
.servlet
.http
.HttpServletResponse
;
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
;
16 /** Returns one single result. */
17 public class GetAttachmentHandler
implements HttpRequestHandler
{
18 protected final String FORCE_DOWNLOAD
= "Content-Type: application/force-download";
20 private AttachmentsStorage attachmentsStorage
;
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())) {
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";
52 contentType
= contentType
+ ";name=\"" + name
+ "\"";
53 response
.setHeader("Content-Disposition", "attachment; filename=\""
56 response
.setHeader("Expires", "0");
57 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
58 response
.setHeader("Pragma", "no-cache");
60 SimpleAttachment resourceDescriptor
= new SimpleAttachment();
61 resourceDescriptor
.setUuid(uuid
);
62 resourceDescriptor
.setContentType(contentType
);
64 response
.setContentType(contentType
);
65 ServletOutputStream outputStream
= response
.getOutputStream();
66 attachmentsStorage
.retrieveAttachment(resourceDescriptor
, outputStream
);
69 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
70 this.attachmentsStorage
= attachmentsStorage
;