]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/org/argeo/jcr/proxy/ResourceProxyServlet.java
Introduce Argeo 2 security model-
[lgpl/argeo-commons.git] / org.argeo.server.jcr / src / org / argeo / jcr / proxy / ResourceProxyServlet.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.jcr.proxy;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20
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;
30
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
38 /** Wraps a proxy via HTTP */
39 public class ResourceProxyServlet extends HttpServlet {
40 private static final long serialVersionUID = -8886549549223155801L;
41
42 private final static Log log = LogFactory
43 .getLog(ResourceProxyServlet.class);
44
45 private ResourceProxy proxy;
46
47 private String contentTypeCharset = "UTF-8";
48
49 @Override
50 protected void doGet(HttpServletRequest request,
51 HttpServletResponse response) throws ServletException, IOException {
52 String path = request.getPathInfo();
53
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"));
63 }
64
65 Node node = null;
66 try {
67 node = proxy.proxy(path);
68 if (node == null)
69 response.sendError(404);
70 else
71 processResponse(node, response);
72 } finally {
73 if (node != null)
74 try {
75 JcrUtils.logoutQuietly(node.getSession());
76 } catch (RepositoryException e) {
77 // silent
78 }
79 }
80
81 }
82
83 /** Retrieve the content of the node. */
84 protected void processResponse(Node node, HttpServletResponse response) {
85 Binary binary = null;
86 InputStream in = null;
87 try {
88 String fileName = node.getName();
89 String ext = FilenameUtils.getExtension(fileName);
90
91 // TODO use a more generic / standard approach
92 // see http://svn.apache.org/viewvc/tomcat/trunk/conf/web.xml
93 String contentType;
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";
108 else
109 contentType = "application/octet-stream";
110 contentType = contentType + ";name=\"" + fileName + "\"";
111 response.setHeader("Content-Disposition", "attachment; filename=\""
112 + fileName + "\"");
113 response.setHeader("Expires", "0");
114 response.setHeader("Cache-Control", "no-cache, must-revalidate");
115 response.setHeader("Pragma", "no-cache");
116
117 response.setContentType(contentType);
118
119 try {
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");
124 throw e;
125 }
126 in = binary.getStream();
127 IOUtils.copy(in, response.getOutputStream());
128 } catch (Exception e) {
129 throw new ArgeoException("Cannot download " + node, e);
130 } finally {
131 IOUtils.closeQuietly(in);
132 JcrUtils.closeQuietly(binary);
133 }
134 }
135
136 public void setProxy(ResourceProxy resourceProxy) {
137 this.proxy = resourceProxy;
138 }
139
140 }