]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr.mvc/src/main/java/org/argeo/jcr/mvc/ResourceProxyServlet.java
Ignore MKCOL on repository and workspace
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr.mvc / src / main / java / 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.Session;
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 Session jcrSession;
49 private String contentTypeCharset = "UTF-8";
50
51 @Override
52 protected void doGet(HttpServletRequest request,
53 HttpServletResponse response) throws ServletException, IOException {
54 String path = request.getPathInfo();
55
56 String nodePath = proxy.getNodePath(path);
57 if (log.isTraceEnabled()) {
58 log.trace("path=" + path + ", nodePath=" + nodePath);
59 log.trace("UserPrincipal = " + request.getUserPrincipal().getName());
60 log.trace("SessionID = " + request.getSession().getId());
61 log.trace("ContextPath = " + request.getContextPath());
62 log.trace("ServletPath = " + request.getServletPath());
63 log.trace("PathInfo = " + request.getPathInfo());
64 log.trace("Method = " + request.getMethod());
65 log.trace("User-Agent = " + request.getHeader("User-Agent"));
66 }
67
68 Node node = proxy.proxy(jcrSession, path);
69 if (node == null)
70 response.sendError(404);
71 else
72 processResponse(nodePath, node, response);
73 }
74
75 /** Retrieve the content of the node. */
76 protected void processResponse(String path, Node node,
77 HttpServletResponse response) {
78 Binary binary = null;
79 InputStream in = null;
80 try {
81 String fileName = node.getName();
82 String ext = FilenameUtils.getExtension(fileName);
83
84 // TODO use a more generic / standard approach
85 // see http://svn.apache.org/viewvc/tomcat/trunk/conf/web.xml
86 String contentType;
87 if ("xml".equals(ext))
88 contentType = "text/xml;charset=" + contentTypeCharset;
89 else if ("jar".equals(ext))
90 contentType = "application/java-archive";
91 else if ("zip".equals(ext))
92 contentType = "application/zip";
93 else if ("gz".equals(ext))
94 contentType = "application/x-gzip";
95 else if ("tar".equals(ext))
96 contentType = "application/x-tar";
97 else
98 contentType = "application/octet-stream";
99 contentType = contentType + ";name=\"" + fileName + "\"";
100 response.setHeader("Content-Disposition", "attachment; filename=\""
101 + fileName + "\"");
102 response.setHeader("Expires", "0");
103 response.setHeader("Cache-Control", "no-cache, must-revalidate");
104 response.setHeader("Pragma", "no-cache");
105
106 response.setContentType(contentType);
107
108 try {
109 binary = node.getNode(Property.JCR_CONTENT)
110 .getProperty(Property.JCR_DATA).getBinary();
111 } catch (PathNotFoundException e) {
112 log.error("Node " + node + " as no data under content");
113 throw e;
114 }
115 in = binary.getStream();
116 IOUtils.copy(in, response.getOutputStream());
117 } catch (Exception e) {
118 throw new ArgeoException("Cannot download " + node, e);
119 } finally {
120 IOUtils.closeQuietly(in);
121 JcrUtils.closeQuietly(binary);
122 }
123 }
124
125 public void setJcrSession(Session jcrSession) {
126 this.jcrSession = jcrSession;
127 }
128
129 public void setProxy(ResourceProxy resourceProxy) {
130 this.proxy = resourceProxy;
131 }
132
133 }