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
.Node
;
22 import javax
.jcr
.Property
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.servlet
.ServletException
;
25 import javax
.servlet
.http
.HttpServlet
;
26 import javax
.servlet
.http
.HttpServletRequest
;
27 import javax
.servlet
.http
.HttpServletResponse
;
29 import org
.apache
.commons
.io
.FilenameUtils
;
30 import org
.apache
.commons
.io
.IOUtils
;
31 import org
.apache
.commons
.logging
.Log
;
32 import org
.apache
.commons
.logging
.LogFactory
;
33 import org
.argeo
.jcr
.ArgeoJcrException
;
34 import org
.argeo
.jcr
.Bin
;
35 import org
.argeo
.jcr
.JcrUtils
;
37 /** Wraps a proxy via HTTP */
38 public class ResourceProxyServlet
extends HttpServlet
{
39 private static final long serialVersionUID
= -8886549549223155801L;
41 private final static Log log
= LogFactory
42 .getLog(ResourceProxyServlet
.class);
44 private ResourceProxy proxy
;
46 private String contentTypeCharset
= "UTF-8";
49 protected void doGet(HttpServletRequest request
,
50 HttpServletResponse response
) throws ServletException
, IOException
{
51 String path
= request
.getPathInfo();
53 if (log
.isTraceEnabled()) {
54 log
.trace("path=" + path
);
55 log
.trace("UserPrincipal = " + request
.getUserPrincipal().getName());
56 log
.trace("SessionID = " + request
.getSession(false).getId());
57 log
.trace("ContextPath = " + request
.getContextPath());
58 log
.trace("ServletPath = " + request
.getServletPath());
59 log
.trace("PathInfo = " + request
.getPathInfo());
60 log
.trace("Method = " + request
.getMethod());
61 log
.trace("User-Agent = " + request
.getHeader("User-Agent"));
66 node
= proxy
.proxy(path
);
68 response
.sendError(404);
70 processResponse(node
, response
);
74 JcrUtils
.logoutQuietly(node
.getSession());
75 } catch (RepositoryException e
) {
82 /** Retrieve the content of the node. */
83 protected void processResponse(Node node
, HttpServletResponse response
) {
84 // Binary binary = null;
85 // InputStream in = null;
86 try(Bin binary
= new Bin( node
.getNode(Property
.JCR_CONTENT
)
87 .getProperty(Property
.JCR_DATA
));InputStream in
= binary
.getStream()) {
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
;