1 package org
.argeo
.jcr
.mvc
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import javax
.jcr
.Binary
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.Session
;
10 import javax
.servlet
.ServletException
;
11 import javax
.servlet
.http
.HttpServlet
;
12 import javax
.servlet
.http
.HttpServletRequest
;
13 import javax
.servlet
.http
.HttpServletResponse
;
15 import org
.apache
.commons
.io
.FilenameUtils
;
16 import org
.apache
.commons
.io
.IOUtils
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.ArgeoException
;
20 import org
.argeo
.jcr
.ArgeoNames
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.argeo
.jcr
.proxy
.ResourceProxy
;
24 /** Wraps a proxy via HTTP */
25 public class ResourceProxyServlet
extends HttpServlet
implements ArgeoNames
{
26 private static final long serialVersionUID
= -8886549549223155801L;
28 private final static Log log
= LogFactory
29 .getLog(ResourceProxyServlet
.class);
31 private ResourceProxy proxy
;
33 private Session jcrSession
;
34 private String contentTypeCharset
= "UTF-8";
37 protected void doGet(HttpServletRequest request
,
38 HttpServletResponse response
) throws ServletException
, IOException
{
39 String path
= request
.getPathInfo();
41 String nodePath
= proxy
.getNodePath(path
);
42 if (log
.isTraceEnabled())
43 log
.trace("path=" + path
+ ", nodePath=" + nodePath
);
45 Node node
= proxy
.proxy(jcrSession
, path
);
47 response
.sendError(404);
49 processResponse(nodePath
, node
, response
);
52 /** Retrieve the content of the node. */
53 protected void processResponse(String path
, Node node
,
54 HttpServletResponse response
) {
56 InputStream in
= null;
58 String fileName
= node
.getName();
59 String ext
= FilenameUtils
.getExtension(fileName
);
61 // TODO use a more generic / standard approach
62 // see http://svn.apache.org/viewvc/tomcat/trunk/conf/web.xml
64 if ("xml".equals(ext
))
65 contentType
= "text/xml;charset=" + contentTypeCharset
;
66 else if ("jar".equals(ext
))
67 contentType
= "application/java-archive";
68 else if ("zip".equals(ext
))
69 contentType
= "application/zip";
70 else if ("gz".equals(ext
))
71 contentType
= "application/x-gzip";
72 else if ("tar".equals(ext
))
73 contentType
= "application/x-tar";
75 contentType
= "application/octet-stream";
76 contentType
= contentType
+ ";name=\"" + fileName
+ "\"";
77 response
.setHeader("Content-Disposition", "attachment; filename=\""
79 response
.setHeader("Expires", "0");
80 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
81 response
.setHeader("Pragma", "no-cache");
83 response
.setContentType(contentType
);
85 binary
= node
.getNode(Property
.JCR_CONTENT
)
86 .getProperty(Property
.JCR_DATA
).getBinary();
87 in
= binary
.getStream();
88 IOUtils
.copy(in
, response
.getOutputStream());
89 } catch (Exception e
) {
90 throw new ArgeoException("Cannot download " + node
, e
);
92 IOUtils
.closeQuietly(in
);
93 JcrUtils
.closeQuietly(binary
);
97 public void setJcrSession(Session jcrSession
) {
98 this.jcrSession
= jcrSession
;
101 public void setProxy(ResourceProxy resourceProxy
) {
102 this.proxy
= resourceProxy
;