]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/proxy/ResourceProxyServlet.java
Move file system support to JCR bundle.
[lgpl/argeo-commons.git] / org.argeo.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.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;
28
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;
36
37 /** Wraps a proxy via HTTP */
38 public class ResourceProxyServlet extends HttpServlet {
39 private static final long serialVersionUID = -8886549549223155801L;
40
41 private final static Log log = LogFactory
42 .getLog(ResourceProxyServlet.class);
43
44 private ResourceProxy proxy;
45
46 private String contentTypeCharset = "UTF-8";
47
48 @Override
49 protected void doGet(HttpServletRequest request,
50 HttpServletResponse response) throws ServletException, IOException {
51 String path = request.getPathInfo();
52
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"));
62 }
63
64 Node node = null;
65 try {
66 node = proxy.proxy(path);
67 if (node == null)
68 response.sendError(404);
69 else
70 processResponse(node, response);
71 } finally {
72 if (node != null)
73 try {
74 JcrUtils.logoutQuietly(node.getSession());
75 } catch (RepositoryException e) {
76 // silent
77 }
78 }
79
80 }
81
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);
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 ArgeoJcrException("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 }