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.model.User;
25  import com.liferay.portal.security.auth.PrincipalThreadLocal;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
28  import com.liferay.portal.security.permission.PermissionThreadLocal;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.util.PropsValues;
31  import com.liferay.portal.webdav.methods.Method;
32  import com.liferay.portal.webdav.methods.MethodFactory;
33  
34  import javax.servlet.http.HttpServlet;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  /**
39   * <a href="WebDAVServlet.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Alexander Chow
43   */
44  public class WebDAVServlet extends HttpServlet {
45  
46      public void service(
47          HttpServletRequest request, HttpServletResponse response) {
48  
49          int status = HttpServletResponse.SC_PRECONDITION_FAILED;
50  
51          String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
52  
53          if (_log.isDebugEnabled()) {
54              _log.debug("User agent " + userAgent);
55          }
56  
57          try {
58              if (isIgnoredResource(request)) {
59                  status = HttpServletResponse.SC_NOT_FOUND;
60  
61                  return;
62              }
63  
64              WebDAVStorage storage = getStorage(request);
65  
66              if (storage == null) {
67                  if (_log.isDebugEnabled()) {
68                      _log.debug("Invalid WebDAV path " + request.getPathInfo());
69                  }
70  
71                  return;
72              }
73  
74              // Set the path only if it has not already been set. This works
75              // if and only if the servlet is not mapped to more than one URL.
76  
77              if (storage.getRootPath() == null) {
78                  storage.setRootPath(getRootPath(request));
79              }
80  
81              PermissionChecker permissionChecker = null;
82  
83              String remoteUser = request.getRemoteUser();
84  
85              if (remoteUser != null) {
86                  PrincipalThreadLocal.setName(remoteUser);
87  
88                  long userId = GetterUtil.getLong(remoteUser);
89  
90                  User user = UserLocalServiceUtil.getUserById(userId);
91  
92                  permissionChecker = PermissionCheckerFactoryUtil.create(
93                      user, true);
94  
95                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
96              }
97  
98              // Get the method instance
99  
100             Method method = MethodFactory.create(request);
101 
102             // Process the method
103 
104             try {
105                 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
106                     storage, request, response, userAgent, permissionChecker);
107 
108                 status = method.process(webDavRequest);
109             }
110             catch (WebDAVException wde) {
111                 if (_log.isWarnEnabled()) {
112                     _log.warn(wde, wde);
113                 }
114 
115                 status = HttpServletResponse.SC_PRECONDITION_FAILED;
116             }
117         }
118         catch (Exception e) {
119             _log.error(e, e);
120         }
121         finally {
122             response.setStatus(status);
123 
124             if (_log.isInfoEnabled()) {
125                 String xLitmus = GetterUtil.getString(
126                     request.getHeader("X-Litmus"));
127 
128                 if (Validator.isNotNull(xLitmus)) {
129                     xLitmus += " ";
130                 }
131 
132                 _log.info(
133                     xLitmus + request.getMethod() + " " +
134                         request.getRequestURI() + " " + status);
135             }
136         }
137     }
138 
139     protected String getRootPath(HttpServletRequest request) {
140         String contextPath = HttpUtil.fixPath(
141             request.getContextPath(), false, true);
142         String ServletPath = HttpUtil.fixPath(
143             request.getServletPath(), false, true);
144 
145         return contextPath.concat(ServletPath);
146     }
147 
148     protected WebDAVStorage getStorage(HttpServletRequest request) {
149         String[] pathArray = WebDAVUtil.getPathArray(
150             request.getPathInfo(), true);
151 
152         WebDAVStorage storage = null;
153 
154         if (pathArray.length == 0) {
155             storage = (WebDAVStorage)InstancePool.get(
156                 CompanyWebDAVStorageImpl.class.getName());
157         }
158         else if (pathArray.length == 1) {
159             storage = (WebDAVStorage)InstancePool.get(
160                 GroupWebDAVStorageImpl.class.getName());
161         }
162         else if (pathArray.length >= 2) {
163             storage = WebDAVUtil.getStorage(pathArray[1]);
164         }
165 
166         return storage;
167     }
168 
169     protected boolean isIgnoredResource(HttpServletRequest request) {
170         String[] pathArray = WebDAVUtil.getPathArray(
171             request.getPathInfo(), true);
172 
173         if ((pathArray == null) || (pathArray.length == 0)) {
174             return false;
175         }
176 
177         String resourceName = pathArray[pathArray.length - 1];
178 
179         for (String ignore : PropsValues.WEBDAV_IGNORE) {
180             if (ignore.equals(resourceName)) {
181                 if (_log.isDebugEnabled()) {
182                     _log.debug(
183                         "Skipping over " + request.getMethod() + " " +
184                             request.getPathInfo());
185                 }
186 
187                 return true;
188             }
189         }
190 
191         return false;
192     }
193 
194     private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
195 
196 }