2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jcr
.proxy
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStream
;
21 import javax
.jcr
.Binary
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.PathNotFoundException
;
24 import javax
.jcr
.Property
;
25 import javax
.jcr
.RepositoryException
;
26 import javax
.servlet
.ServletException
;
27 import javax
.servlet
.http
.HttpServlet
;
28 import javax
.servlet
.http
.HttpServletRequest
;
29 import javax
.servlet
.http
.HttpServletResponse
;
31 import org
.apache
.commons
.io
.FilenameUtils
;
32 import org
.apache
.commons
.io
.IOUtils
;
33 import org
.apache
.commons
.logging
.Log
;
34 import org
.apache
.commons
.logging
.LogFactory
;
35 import org
.argeo
.jcr
.ArgeoJcrException
;
36 import org
.argeo
.jcr
.JcrUtils
;
38 /** Wraps a proxy via HTTP */
39 public class ResourceProxyServlet
extends HttpServlet
{
40 private static final long serialVersionUID
= -8886549549223155801L;
42 private final static Log log
= LogFactory
43 .getLog(ResourceProxyServlet
.class);
45 private ResourceProxy proxy
;
47 private String contentTypeCharset
= "UTF-8";
50 protected void doGet(HttpServletRequest request
,
51 HttpServletResponse response
) throws ServletException
, IOException
{
52 String path
= request
.getPathInfo();
54 if (log
.isTraceEnabled()) {
55 log
.trace("path=" + path
);
56 log
.trace("UserPrincipal = " + request
.getUserPrincipal().getName());
57 log
.trace("SessionID = " + request
.getSession().getId());
58 log
.trace("ContextPath = " + request
.getContextPath());
59 log
.trace("ServletPath = " + request
.getServletPath());
60 log
.trace("PathInfo = " + request
.getPathInfo());
61 log
.trace("Method = " + request
.getMethod());
62 log
.trace("User-Agent = " + request
.getHeader("User-Agent"));
67 node
= proxy
.proxy(path
);
69 response
.sendError(404);
71 processResponse(node
, response
);
75 JcrUtils
.logoutQuietly(node
.getSession());
76 } catch (RepositoryException e
) {
83 /** Retrieve the content of the node. */
84 protected void processResponse(Node node
, HttpServletResponse response
) {
86 InputStream in
= null;
88 String fileName
= node
.getName();
89 String ext
= FilenameUtils
.getExtension(fileName
);
91 // TODO use a more generic / standard approach
92 // see http://svn.apache.org/viewvc/tomcat/trunk/conf/web.xml
94 if ("xml".equals(ext
))
95 contentType
= "text/xml;charset=" + contentTypeCharset
;
96 else if ("jar".equals(ext
))
97 contentType
= "application/java-archive";
98 else if ("zip".equals(ext
))
99 contentType
= "application/zip";
100 else if ("gz".equals(ext
))
101 contentType
= "application/x-gzip";
102 else if ("bz2".equals(ext
))
103 contentType
= "application/x-bzip2";
104 else if ("tar".equals(ext
))
105 contentType
= "application/x-tar";
106 else if ("rpm".equals(ext
))
107 contentType
= "application/x-redhat-package-manager";
109 contentType
= "application/octet-stream";
110 contentType
= contentType
+ ";name=\"" + fileName
+ "\"";
111 response
.setHeader("Content-Disposition", "attachment; filename=\""
113 response
.setHeader("Expires", "0");
114 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
115 response
.setHeader("Pragma", "no-cache");
117 response
.setContentType(contentType
);
120 binary
= node
.getNode(Property
.JCR_CONTENT
)
121 .getProperty(Property
.JCR_DATA
).getBinary();
122 } catch (PathNotFoundException e
) {
123 log
.error("Node " + node
+ " as no data under content");
126 in
= binary
.getStream();
127 IOUtils
.copy(in
, response
.getOutputStream());
128 } catch (Exception e
) {
129 throw new ArgeoJcrException("Cannot download " + node
, e
);
131 IOUtils
.closeQuietly(in
);
132 JcrUtils
.closeQuietly(binary
);
136 public void setProxy(ResourceProxy resourceProxy
) {
137 this.proxy
= resourceProxy
;