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