1 package org
.argeo
.jcr
.proxy
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.RepositoryException
;
9 import javax
.servlet
.ServletException
;
10 import javax
.servlet
.http
.HttpServlet
;
11 import javax
.servlet
.http
.HttpServletRequest
;
12 import javax
.servlet
.http
.HttpServletResponse
;
14 import org
.apache
.commons
.io
.FilenameUtils
;
15 import org
.apache
.commons
.io
.IOUtils
;
16 import org
.apache
.commons
.logging
.Log
;
17 import org
.apache
.commons
.logging
.LogFactory
;
18 import org
.argeo
.jcr
.JcrException
;
19 import org
.argeo
.jcr
.Bin
;
20 import org
.argeo
.jcr
.JcrUtils
;
22 /** Wraps a proxy via HTTP */
23 public class ResourceProxyServlet
extends HttpServlet
{
24 private static final long serialVersionUID
= -8886549549223155801L;
26 private final static Log log
= LogFactory
27 .getLog(ResourceProxyServlet
.class);
29 private ResourceProxy proxy
;
31 private String contentTypeCharset
= "UTF-8";
34 protected void doGet(HttpServletRequest request
,
35 HttpServletResponse response
) throws ServletException
, IOException
{
36 String path
= request
.getPathInfo();
38 if (log
.isTraceEnabled()) {
39 log
.trace("path=" + path
);
40 log
.trace("UserPrincipal = " + request
.getUserPrincipal().getName());
41 log
.trace("SessionID = " + request
.getSession(false).getId());
42 log
.trace("ContextPath = " + request
.getContextPath());
43 log
.trace("ServletPath = " + request
.getServletPath());
44 log
.trace("PathInfo = " + request
.getPathInfo());
45 log
.trace("Method = " + request
.getMethod());
46 log
.trace("User-Agent = " + request
.getHeader("User-Agent"));
51 node
= proxy
.proxy(path
);
53 response
.sendError(404);
55 processResponse(node
, response
);
59 JcrUtils
.logoutQuietly(node
.getSession());
60 } catch (RepositoryException e
) {
67 /** Retrieve the content of the node. */
68 protected void processResponse(Node node
, HttpServletResponse response
) {
69 // Binary binary = null;
70 // InputStream in = null;
71 try(Bin binary
= new Bin( node
.getNode(Property
.JCR_CONTENT
)
72 .getProperty(Property
.JCR_DATA
));InputStream in
= binary
.getStream()) {
73 String fileName
= node
.getName();
74 String ext
= FilenameUtils
.getExtension(fileName
);
76 // TODO use a more generic / standard approach
77 // see http://svn.apache.org/viewvc/tomcat/trunk/conf/web.xml
79 if ("xml".equals(ext
))
80 contentType
= "text/xml;charset=" + contentTypeCharset
;
81 else if ("jar".equals(ext
))
82 contentType
= "application/java-archive";
83 else if ("zip".equals(ext
))
84 contentType
= "application/zip";
85 else if ("gz".equals(ext
))
86 contentType
= "application/x-gzip";
87 else if ("bz2".equals(ext
))
88 contentType
= "application/x-bzip2";
89 else if ("tar".equals(ext
))
90 contentType
= "application/x-tar";
91 else if ("rpm".equals(ext
))
92 contentType
= "application/x-redhat-package-manager";
94 contentType
= "application/octet-stream";
95 contentType
= contentType
+ ";name=\"" + fileName
+ "\"";
96 response
.setHeader("Content-Disposition", "attachment; filename=\""
98 response
.setHeader("Expires", "0");
99 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
100 response
.setHeader("Pragma", "no-cache");
102 response
.setContentType(contentType
);
104 IOUtils
.copy(in
, response
.getOutputStream());
105 } catch (RepositoryException e
) {
106 throw new JcrException("Cannot download " + node
, e
);
107 } catch (IOException e
) {
108 throw new RuntimeException("Cannot download " + node
, e
);
112 public void setProxy(ResourceProxy resourceProxy
) {
113 this.proxy
= resourceProxy
;