1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.webdav;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.HttpHeaders;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.InstancePool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.kernel.webdav.WebDAVException;
25  import com.liferay.portal.kernel.webdav.WebDAVRequest;
26  import com.liferay.portal.kernel.webdav.WebDAVStorage;
27  import com.liferay.portal.kernel.webdav.WebDAVUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.security.auth.PrincipalThreadLocal;
30  import com.liferay.portal.security.permission.PermissionChecker;
31  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
32  import com.liferay.portal.security.permission.PermissionThreadLocal;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portal.util.PropsValues;
35  import com.liferay.portal.webdav.methods.Method;
36  import com.liferay.portal.webdav.methods.MethodFactory;
37  
38  import javax.servlet.http.HttpServlet;
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  /**
43   * <a href="WebDAVServlet.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Alexander Chow
47   */
48  public class WebDAVServlet extends HttpServlet {
49  
50      public void service(
51          HttpServletRequest request, HttpServletResponse response) {
52  
53          int status = HttpServletResponse.SC_PRECONDITION_FAILED;
54  
55          String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
56  
57          if (_log.isDebugEnabled()) {
58              _log.debug("User agent " + userAgent);
59          }
60  
61          try {
62              if (isIgnoredResource(request)) {
63                  status = HttpServletResponse.SC_NOT_FOUND;
64  
65                  return;
66              }
67  
68              WebDAVStorage storage = getStorage(request);
69  
70              if (storage == null) {
71                  if (_log.isDebugEnabled()) {
72                      _log.debug("Invalid WebDAV path " + request.getPathInfo());
73                  }
74  
75                  return;
76              }
77  
78              // Set the path only if it has not already been set. This works
79              // if and only if the servlet is not mapped to more than one URL.
80  
81              if (storage.getRootPath() == null) {
82                  storage.setRootPath(getRootPath(request));
83              }
84  
85              PermissionChecker permissionChecker = null;
86  
87              String remoteUser = request.getRemoteUser();
88  
89              if (remoteUser != null) {
90                  PrincipalThreadLocal.setName(remoteUser);
91  
92                  long userId = GetterUtil.getLong(remoteUser);
93  
94                  User user = UserLocalServiceUtil.getUserById(userId);
95  
96                  permissionChecker = PermissionCheckerFactoryUtil.create(
97                      user, true);
98  
99                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
100             }
101 
102             // Get the method instance
103 
104             Method method = MethodFactory.create(request);
105 
106             // Process the method
107 
108             try {
109                 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
110                     storage, request, response, userAgent, permissionChecker);
111 
112                 status = method.process(webDavRequest);
113             }
114             catch (WebDAVException wde) {
115                 if (_log.isWarnEnabled()) {
116                     _log.warn(wde, wde);
117                 }
118 
119                 status = HttpServletResponse.SC_PRECONDITION_FAILED;
120             }
121         }
122         catch (Exception e) {
123             _log.error(e, e);
124         }
125         finally {
126             response.setStatus(status);
127 
128             if (_log.isInfoEnabled()) {
129                 String xLitmus = GetterUtil.getString(
130                     request.getHeader("X-Litmus"));
131 
132                 if (Validator.isNotNull(xLitmus)) {
133                     xLitmus += " ";
134                 }
135 
136                 _log.info(
137                     xLitmus + request.getMethod() + " " +
138                         request.getRequestURI() + " " + status);
139             }
140         }
141     }
142 
143     protected String getRootPath(HttpServletRequest request) {
144         String contextPath = HttpUtil.fixPath(
145             request.getContextPath(), false, true);
146         String ServletPath = HttpUtil.fixPath(
147             request.getServletPath(), false, true);
148 
149         return contextPath.concat(ServletPath);
150     }
151 
152     protected WebDAVStorage getStorage(HttpServletRequest request) {
153         String[] pathArray = WebDAVUtil.getPathArray(
154             request.getPathInfo(), true);
155 
156         WebDAVStorage storage = null;
157 
158         if (pathArray.length == 0) {
159             storage = (WebDAVStorage)InstancePool.get(
160                 CompanyWebDAVStorageImpl.class.getName());
161         }
162         else if (pathArray.length == 1) {
163             storage = (WebDAVStorage)InstancePool.get(
164                 GroupWebDAVStorageImpl.class.getName());
165         }
166         else if (pathArray.length >= 2) {
167             storage = WebDAVUtil.getStorage(pathArray[1]);
168         }
169 
170         return storage;
171     }
172 
173     protected boolean isIgnoredResource(HttpServletRequest request) {
174         String[] pathArray = WebDAVUtil.getPathArray(
175             request.getPathInfo(), true);
176 
177         if ((pathArray == null) || (pathArray.length == 0)) {
178             return false;
179         }
180 
181         String resourceName = pathArray[pathArray.length - 1];
182 
183         for (String ignore : PropsValues.WEBDAV_IGNORE) {
184             if (ignore.equals(resourceName)) {
185                 if (_log.isDebugEnabled()) {
186                     _log.debug(
187                         "Skipping over " + request.getMethod() + " " +
188                             request.getPathInfo());
189                 }
190 
191                 return true;
192             }
193         }
194 
195         return false;
196     }
197 
198     private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
199 
200 }