2 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
\r
4 * This library is free software; you can redistribute it and/or modify it under
\r
5 * the terms of the GNU Lesser General Public License as published by the Free
\r
6 * Software Foundation; either version 2.1 of the License, or (at your option)
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT
\r
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
\r
15 package com.liferay.portal.webdav;
\r
17 import javax.servlet.http.HttpServlet;
\r
18 import javax.servlet.http.HttpServletRequest;
\r
19 import javax.servlet.http.HttpServletResponse;
\r
21 import com.liferay.portal.kernel.log.Log;
\r
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
\r
23 import com.liferay.portal.kernel.servlet.HttpHeaders;
\r
24 import com.liferay.portal.kernel.util.GetterUtil;
\r
25 import com.liferay.portal.kernel.util.HttpUtil;
\r
26 import com.liferay.portal.kernel.util.InstancePool;
\r
27 import com.liferay.portal.kernel.util.StringPool;
\r
28 import com.liferay.portal.kernel.util.Validator;
\r
29 import com.liferay.portal.kernel.webdav.WebDAVException;
\r
30 import com.liferay.portal.kernel.webdav.WebDAVRequest;
\r
31 import com.liferay.portal.kernel.webdav.WebDAVStorage;
\r
32 import com.liferay.portal.kernel.webdav.WebDAVUtil;
\r
33 import com.liferay.portal.model.User;
\r
34 import com.liferay.portal.security.auth.PrincipalException;
\r
35 import com.liferay.portal.security.auth.PrincipalThreadLocal;
\r
36 import com.liferay.portal.security.permission.PermissionChecker;
\r
37 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
\r
38 import com.liferay.portal.security.permission.PermissionThreadLocal;
\r
39 import com.liferay.portal.service.UserLocalServiceUtil;
\r
40 import com.liferay.portal.util.PropsValues;
\r
41 import com.liferay.portal.webdav.methods.Method;
\r
42 import com.liferay.portal.webdav.methods.MethodFactory;
\r
45 * @author Brian Wing Shun Chan
\r
46 * @author Alexander Chow
\r
48 public class WebDAVServlet extends HttpServlet {
\r
51 public void service(
\r
52 HttpServletRequest request, HttpServletResponse response) {
\r
54 int status = HttpServletResponse.SC_PRECONDITION_FAILED;
\r
56 String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
\r
58 if (_log.isDebugEnabled()) {
\r
59 _log.debug("User agent " + userAgent);
\r
63 if (isIgnoredResource(request)) {
\r
64 status = HttpServletResponse.SC_NOT_FOUND;
\r
69 String methodCall = request.getMethod().toUpperCase();
\r
70 if(request.getPathInfo() == null){
\r
71 response.setStatus(HttpServletResponse.SC_OK);
\r
72 status = HttpServletResponse.SC_OK;
\r
74 if(request.getPathInfo().contains("._") || methodCall.equals("LOCK") || methodCall.equals("UNLOCK")){
\r
75 // Cas d'un fichier system MAC
\r
76 // Cas de l'appel a la methode LOCK --> ne fonctionne pas top sur le client MAC et pas du tout sous Windows
\r
77 // Cas de l'appel a la methode UNLOCK --> car LOCK ne fonctionne pas
\r
78 status = HttpServletResponse.SC_OK;
\r
80 WebDAVStorage storage = getStorage(request);
\r
82 if (storage == null) {
\r
83 if (_log.isDebugEnabled()) {
\r
84 _log.debug("Invalid WebDAV path " + request.getPathInfo());
\r
90 // Set the path only if it has not already been set. This works
\r
91 // if and only if the servlet is not mapped to more than one URL.
\r
93 if(storage != null){
\r
94 if (storage.getRootPath() == null) {
\r
95 storage.setRootPath(getRootPath(request));
\r
99 PermissionChecker permissionChecker = null;
\r
101 String remoteUser = request.getRemoteUser();
\r
102 if (remoteUser != null) {
\r
103 PrincipalThreadLocal.setName(remoteUser);
\r
105 long userId = GetterUtil.getLong(remoteUser);
\r
107 User user = UserLocalServiceUtil.getUserById(userId);
\r
108 permissionChecker = PermissionCheckerFactoryUtil.create(user);
\r
110 PermissionThreadLocal.setPermissionChecker(permissionChecker);
\r
113 // Get the method instance
\r
115 Method method = MethodFactory.create(request);
\r
117 // Process the method
\r
120 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
\r
121 storage, request, response, userAgent, permissionChecker);
\r
123 status = method.process(webDavRequest);
\r
125 catch (WebDAVException wde) {
\r
126 boolean logError = false;
\r
128 Throwable cause = wde;
\r
130 while (cause != null) {
\r
131 if (cause instanceof PrincipalException) {
\r
135 cause = cause.getCause();
\r
139 _log.error(wde, wde);
\r
141 else if (_log.isWarnEnabled()) {
\r
142 _log.warn(wde, wde);
\r
145 status = HttpServletResponse.SC_PRECONDITION_FAILED;
\r
150 catch (Exception e) {
\r
154 response.setStatus(status);
\r
156 if (_log.isInfoEnabled()) {
\r
157 String xLitmus = GetterUtil.getString(
\r
158 request.getHeader("X-Litmus"));
\r
160 if (Validator.isNotNull(xLitmus)) {
\r
165 xLitmus + request.getMethod() + " " +
\r
166 request.getRequestURI() + " " + status);
\r
171 protected String getRootPath(HttpServletRequest request) {
\r
172 String contextPath = HttpUtil.fixPath(
\r
173 request.getContextPath(), false, true);
\r
174 String ServletPath = HttpUtil.fixPath(
\r
175 request.getServletPath(), false, true);
\r
177 return contextPath.concat(ServletPath);
\r
180 protected WebDAVStorage getStorage(HttpServletRequest request) {
\r
181 String[] pathArray = WebDAVUtil.getPathArray(
\r
182 request.getPathInfo(), true);
\r
184 WebDAVStorage storage = null;
\r
186 if (pathArray.length == 0) {
\r
187 storage = (WebDAVStorage)InstancePool.get(
\r
188 CompanyWebDAVStorageImpl.class.getName());
\r
190 else if (pathArray.length == 1) {
\r
191 storage = (WebDAVStorage)InstancePool.get(
\r
192 GroupWebDAVStorageImpl.class.getName());
\r
194 else if (pathArray.length >= 2) {
\r
195 storage = WebDAVUtil.getStorage(pathArray[1]);
\r
201 protected boolean isIgnoredResource(HttpServletRequest request) {
\r
202 String[] pathArray = null;
\r
204 if(request.getPathInfo() != null){
\r
205 pathArray = WebDAVUtil.getPathArray(request.getPathInfo(), true);
\r
208 if ((pathArray == null) || (pathArray.length == 0)) {
\r
212 for (String ignore : PropsValues.WEBDAV_IGNORE) {
\r
213 String[] ignoreArray = ignore.split(StringPool.SLASH);
\r
215 if (ignoreArray.length > pathArray.length) {
\r
219 boolean match = true;
\r
221 for (int i = 1; i <= ignoreArray.length; i++) {
\r
222 if (!pathArray[pathArray.length - i].equals(
\r
223 ignoreArray[ignoreArray.length - i])) {
\r
232 if (_log.isDebugEnabled()) {
\r
234 "Skipping over " + request.getMethod() + " " +
\r
235 request.getPathInfo());
\r
245 private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
\r