--- /dev/null
+/**\r
+ * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.\r
+ *\r
+ * This library is free software; you can redistribute it and/or modify it under\r
+ * the terms of the GNU Lesser General Public License as published by the Free\r
+ * Software Foundation; either version 2.1 of the License, or (at your option)\r
+ * any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more\r
+ * details.\r
+ */\r
+\r
+package com.liferay.portal.webdav;\r
+\r
+import javax.servlet.http.HttpServlet;\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import com.liferay.portal.kernel.log.Log;\r
+import com.liferay.portal.kernel.log.LogFactoryUtil;\r
+import com.liferay.portal.kernel.servlet.HttpHeaders;\r
+import com.liferay.portal.kernel.util.GetterUtil;\r
+import com.liferay.portal.kernel.util.HttpUtil;\r
+import com.liferay.portal.kernel.util.InstancePool;\r
+import com.liferay.portal.kernel.util.StringPool;\r
+import com.liferay.portal.kernel.util.Validator;\r
+import com.liferay.portal.kernel.webdav.WebDAVException;\r
+import com.liferay.portal.kernel.webdav.WebDAVRequest;\r
+import com.liferay.portal.kernel.webdav.WebDAVStorage;\r
+import com.liferay.portal.kernel.webdav.WebDAVUtil;\r
+import com.liferay.portal.model.User;\r
+import com.liferay.portal.security.auth.PrincipalException;\r
+import com.liferay.portal.security.auth.PrincipalThreadLocal;\r
+import com.liferay.portal.security.permission.PermissionChecker;\r
+import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;\r
+import com.liferay.portal.security.permission.PermissionThreadLocal;\r
+import com.liferay.portal.service.UserLocalServiceUtil;\r
+import com.liferay.portal.util.PropsValues;\r
+import com.liferay.portal.webdav.methods.Method;\r
+import com.liferay.portal.webdav.methods.MethodFactory;\r
+\r
+/**\r
+ * @author Brian Wing Shun Chan\r
+ * @author Alexander Chow\r
+ */\r
+public class WebDAVServlet extends HttpServlet {\r
+\r
+ @Override\r
+ public void service(\r
+ HttpServletRequest request, HttpServletResponse response) {\r
+\r
+ int status = HttpServletResponse.SC_PRECONDITION_FAILED;\r
+\r
+ String userAgent = request.getHeader(HttpHeaders.USER_AGENT);\r
+\r
+ if (_log.isDebugEnabled()) {\r
+ _log.debug("User agent " + userAgent);\r
+ }\r
+\r
+ try {\r
+ if (isIgnoredResource(request)) {\r
+ status = HttpServletResponse.SC_NOT_FOUND;\r
+\r
+ return;\r
+ }\r
+ \r
+ String methodCall = request.getMethod().toUpperCase();\r
+ if(request.getPathInfo() == null){\r
+ response.setStatus(HttpServletResponse.SC_OK);\r
+ status = HttpServletResponse.SC_OK;\r
+ } else {\r
+ if(request.getPathInfo().contains("._") || methodCall.equals("LOCK") || methodCall.equals("UNLOCK")){\r
+ // Cas d'un fichier system MAC\r
+ // Cas de l'appel a la methode LOCK --> ne fonctionne pas top sur le client MAC et pas du tout sous Windows\r
+ // Cas de l'appel a la methode UNLOCK --> car LOCK ne fonctionne pas \r
+ status = HttpServletResponse.SC_OK;\r
+ } else {\r
+ WebDAVStorage storage = getStorage(request);\r
+ \r
+ if (storage == null) {\r
+ if (_log.isDebugEnabled()) {\r
+ _log.debug("Invalid WebDAV path " + request.getPathInfo());\r
+ }\r
+\r
+ return;\r
+ }\r
+ \r
+ // Set the path only if it has not already been set. This works\r
+ // if and only if the servlet is not mapped to more than one URL.\r
+ \r
+ if(storage != null){\r
+ if (storage.getRootPath() == null) {\r
+ storage.setRootPath(getRootPath(request));\r
+ }\r
+ }\r
+ \r
+ PermissionChecker permissionChecker = null;\r
+\r
+ String remoteUser = request.getRemoteUser();\r
+ if (remoteUser != null) {\r
+ PrincipalThreadLocal.setName(remoteUser);\r
+\r
+ long userId = GetterUtil.getLong(remoteUser);\r
+\r
+ User user = UserLocalServiceUtil.getUserById(userId);\r
+ permissionChecker = PermissionCheckerFactoryUtil.create(user);\r
+\r
+ PermissionThreadLocal.setPermissionChecker(permissionChecker);\r
+ }\r
+ \r
+ // Get the method instance\r
+\r
+ Method method = MethodFactory.create(request);\r
+ \r
+ // Process the method\r
+\r
+ try {\r
+ WebDAVRequest webDavRequest = new WebDAVRequestImpl(\r
+ storage, request, response, userAgent, permissionChecker);\r
+\r
+ status = method.process(webDavRequest);\r
+ }\r
+ catch (WebDAVException wde) {\r
+ boolean logError = false;\r
+\r
+ Throwable cause = wde;\r
+\r
+ while (cause != null) {\r
+ if (cause instanceof PrincipalException) {\r
+ logError = true;\r
+ }\r
+\r
+ cause = cause.getCause();\r
+ }\r
+\r
+ if (logError) {\r
+ _log.error(wde, wde);\r
+ }\r
+ else if (_log.isWarnEnabled()) {\r
+ _log.warn(wde, wde);\r
+ }\r
+\r
+ status = HttpServletResponse.SC_PRECONDITION_FAILED;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ catch (Exception e) {\r
+ _log.error(e, e);\r
+ }\r
+ finally {\r
+ response.setStatus(status);\r
+\r
+ if (_log.isInfoEnabled()) {\r
+ String xLitmus = GetterUtil.getString(\r
+ request.getHeader("X-Litmus"));\r
+\r
+ if (Validator.isNotNull(xLitmus)) {\r
+ xLitmus += " ";\r
+ }\r
+\r
+ _log.info(\r
+ xLitmus + request.getMethod() + " " +\r
+ request.getRequestURI() + " " + status);\r
+ }\r
+ }\r
+ }\r
+\r
+ protected String getRootPath(HttpServletRequest request) {\r
+ String contextPath = HttpUtil.fixPath(\r
+ request.getContextPath(), false, true);\r
+ String ServletPath = HttpUtil.fixPath(\r
+ request.getServletPath(), false, true);\r
+\r
+ return contextPath.concat(ServletPath);\r
+ }\r
+\r
+ protected WebDAVStorage getStorage(HttpServletRequest request) {\r
+ String[] pathArray = WebDAVUtil.getPathArray(\r
+ request.getPathInfo(), true);\r
+\r
+ WebDAVStorage storage = null;\r
+\r
+ if (pathArray.length == 0) {\r
+ storage = (WebDAVStorage)InstancePool.get(\r
+ CompanyWebDAVStorageImpl.class.getName());\r
+ }\r
+ else if (pathArray.length == 1) {\r
+ storage = (WebDAVStorage)InstancePool.get(\r
+ GroupWebDAVStorageImpl.class.getName());\r
+ }\r
+ else if (pathArray.length >= 2) {\r
+ storage = WebDAVUtil.getStorage(pathArray[1]);\r
+ }\r
+\r
+ return storage;\r
+ }\r
+\r
+ protected boolean isIgnoredResource(HttpServletRequest request) {\r
+ String[] pathArray = null;\r
+ \r
+ if(request.getPathInfo() != null){\r
+ pathArray = WebDAVUtil.getPathArray(request.getPathInfo(), true);\r
+ }\r
+\r
+ if ((pathArray == null) || (pathArray.length == 0)) {\r
+ return false;\r
+ }\r
+\r
+ for (String ignore : PropsValues.WEBDAV_IGNORE) {\r
+ String[] ignoreArray = ignore.split(StringPool.SLASH);\r
+\r
+ if (ignoreArray.length > pathArray.length) {\r
+ continue;\r
+ }\r
+\r
+ boolean match = true;\r
+\r
+ for (int i = 1; i <= ignoreArray.length; i++) {\r
+ if (!pathArray[pathArray.length - i].equals(\r
+ ignoreArray[ignoreArray.length - i])) {\r
+\r
+ match = false;\r
+\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (match) {\r
+ if (_log.isDebugEnabled()) {\r
+ _log.debug(\r
+ "Skipping over " + request.getMethod() + " " +\r
+ request.getPathInfo());\r
+ }\r
+\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+ }\r
+\r
+ private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);\r
+\r
+}
\ No newline at end of file