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
.mvc
;
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
.ArgeoException
;
36 import org
.argeo
.jcr
.JcrUtils
;
37 import org
.argeo
.jcr
.proxy
.ResourceProxy
;
39 /** Wraps a proxy via HTTP */
40 public class ResourceProxyServlet
extends HttpServlet
{
41 private static final long serialVersionUID
= -8886549549223155801L;
43 private final static Log log
= LogFactory
44 .getLog(ResourceProxyServlet
.class);
46 private ResourceProxy proxy
;
48 private String contentTypeCharset
= "UTF-8";
51 protected void doGet(HttpServletRequest request
,
52 HttpServletResponse response
) throws ServletException
, IOException
{
53 String path
= request
.getPathInfo();
55 if (log
.isTraceEnabled()) {
56 log
.trace("path=" + path
);
57 log
.trace("UserPrincipal = " + request
.getUserPrincipal().getName());
58 log
.trace("SessionID = " + request
.getSession().getId());
59 log
.trace("ContextPath = " + request
.getContextPath());
60 log
.trace("ServletPath = " + request
.getServletPath());
61 log
.trace("PathInfo = " + request
.getPathInfo());
62 log
.trace("Method = " + request
.getMethod());
63 log
.trace("User-Agent = " + request
.getHeader("User-Agent"));
68 node
= proxy
.proxy(path
);
70 response
.sendError(404);
72 processResponse(node
, response
);
76 JcrUtils
.logoutQuietly(node
.getSession());
77 } catch (RepositoryException e
) {
84 /** Retrieve the content of the node. */
85 protected void processResponse(Node node
, HttpServletResponse response
) {
87 InputStream in
= null;
89 String fileName
= node
.getName();
90 String ext
= FilenameUtils
.getExtension(fileName
);
92 // TODO use a more generic / standard approach
93 // see http://svn.apache.org/viewvc/tomcat/trunk/conf/web.xml
95 if ("xml".equals(ext
))
96 contentType
= "text/xml;charset=" + contentTypeCharset
;
97 else if ("jar".equals(ext
))
98 contentType
= "application/java-archive";
99 else if ("zip".equals(ext
))
100 contentType
= "application/zip";
101 else if ("gz".equals(ext
))
102 contentType
= "application/x-gzip";
103 else if ("bz2".equals(ext
))
104 contentType
= "application/x-bzip2";
105 else if ("tar".equals(ext
))
106 contentType
= "application/x-tar";
107 else if ("rpm".equals(ext
))
108 contentType
= "application/x-redhat-package-manager";
110 contentType
= "application/octet-stream";
111 contentType
= contentType
+ ";name=\"" + fileName
+ "\"";
112 response
.setHeader("Content-Disposition", "attachment; filename=\""
114 response
.setHeader("Expires", "0");
115 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
116 response
.setHeader("Pragma", "no-cache");
118 response
.setContentType(contentType
);
121 binary
= node
.getNode(Property
.JCR_CONTENT
)
122 .getProperty(Property
.JCR_DATA
).getBinary();
123 } catch (PathNotFoundException e
) {
124 log
.error("Node " + node
+ " as no data under content");
127 in
= binary
.getStream();
128 IOUtils
.copy(in
, response
.getOutputStream());
129 } catch (Exception e
) {
130 throw new ArgeoException("Cannot download " + node
, e
);
132 IOUtils
.closeQuietly(in
);
133 JcrUtils
.closeQuietly(binary
);
137 public void setProxy(ResourceProxy resourceProxy
) {
138 this.proxy
= resourceProxy
;