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
.FilenameUtils
;
11 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
12 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
13 import org
.springframework
.web
.HttpRequestHandler
;
15 /** Returns one single result. */
16 public class GetAttachmentHandler
implements HttpRequestHandler
{
17 protected final String FORCE_DOWNLOAD
= "Content-Type: application/force-download";
19 private AttachmentsStorage attachmentsStorage
;
21 public void handleRequest(HttpServletRequest request
,
22 HttpServletResponse response
) throws ServletException
, IOException
{
23 String uuid
= request
.getParameter("uuid");
24 String contentType
= request
.getParameter("contentType");
25 String name
= request
.getParameter("name");
26 if (contentType
== null || "".equals(contentType
.trim())) {
28 contentType
= FORCE_DOWNLOAD
;
29 String ext
= FilenameUtils
.getExtension(name
);
30 // cf. http://en.wikipedia.org/wiki/Internet_media_type
31 if ("csv".equals(ext
))
32 contentType
= "text/csv";
33 else if ("pdf".equals(ext
))
34 contentType
= "application/pdf";
35 else if ("zip".equals(ext
))
36 contentType
= "application/zip";
37 else if ("html".equals(ext
))
38 contentType
= "application/html";
39 else if ("txt".equals(ext
))
40 contentType
= "text/plain";
41 else if ("doc".equals(ext
) || "docx".equals(ext
))
42 contentType
= "application/msword";
43 else if ("xls".equals(ext
) || "xlsx".equals(ext
))
44 contentType
= "application/vnd.ms-excel";
45 else if ("xml".equals(ext
))
46 contentType
= "text/xml";
51 contentType
= contentType
+ ";name=\"" + name
+ "\"";
52 response
.setHeader("Content-Disposition", "attachment; filename=\""
55 response
.setHeader("Expires", "0");
56 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
57 response
.setHeader("Pragma", "no-cache");
59 SimpleAttachment resourceDescriptor
= new SimpleAttachment();
60 resourceDescriptor
.setUuid(uuid
);
61 resourceDescriptor
.setContentType(contentType
);
63 response
.setContentType(contentType
);
64 ServletOutputStream outputStream
= response
.getOutputStream();
65 attachmentsStorage
.retrieveAttachment(resourceDescriptor
, outputStream
);
68 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
69 this.attachmentsStorage
= attachmentsStorage
;