1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.webdav;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.InstancePool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.security.auth.PrincipalThreadLocal;
32  import com.liferay.portal.security.permission.PermissionChecker;
33  import com.liferay.portal.security.permission.PermissionCheckerFactory;
34  import com.liferay.portal.security.permission.PermissionThreadLocal;
35  import com.liferay.portal.service.UserLocalServiceUtil;
36  import com.liferay.portal.util.PropsValues;
37  import com.liferay.portal.webdav.methods.Method;
38  import com.liferay.portal.webdav.methods.MethodFactory;
39  
40  import javax.servlet.http.HttpServlet;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  /**
45   * <a href="WebDAVServlet.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Alexander Chow
49   *
50   */
51  public class WebDAVServlet extends HttpServlet {
52  
53      public void service(
54          HttpServletRequest request, HttpServletResponse response) {
55  
56          PermissionChecker permissionChecker = null;
57  
58          int status = HttpServletResponse.SC_PRECONDITION_FAILED;
59  
60          if (_log.isInfoEnabled()) {
61              _log.info(request.getMethod() + " " + request.getRequestURI());
62          }
63  
64          if (_log.isDebugEnabled()) {
65              _log.debug("User agent " + request.getHeader("User-agent"));
66          }
67  
68          try {
69              if (isIgnoredResource(request)) {
70                  status = HttpServletResponse.SC_NOT_FOUND;
71  
72                  return;
73              }
74  
75              WebDAVStorage storage = getStorage(request);
76  
77              // Set the path only if it has not already been set. This works
78              // if and only if the servlet is not mapped to more than one URL.
79  
80              if (storage.getRootPath() == null) {
81                  storage.setRootPath(getRootPath(request));
82              }
83  
84              // Permission checker
85  
86              String remoteUser = request.getRemoteUser();
87  
88              if (remoteUser != null) {
89                  PrincipalThreadLocal.setName(remoteUser);
90  
91                  long userId = GetterUtil.getLong(remoteUser);
92  
93                  User user = UserLocalServiceUtil.getUserById(userId);
94  
95                  permissionChecker = PermissionCheckerFactory.create(user, true);
96  
97                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
98              }
99  
100             // Get the method instance
101 
102             Method method = MethodFactory.create(request);
103 
104             // Process the method
105 
106             WebDAVRequest webDavRequest = new WebDAVRequest(
107                 storage, request, response, permissionChecker);
108 
109             status = method.process(webDavRequest);
110         }
111         catch (Exception e) {
112             _log.error(e, e);
113         }
114         finally {
115             if (status > 0) {
116                 response.setStatus(status);
117 
118                 if (_log.isInfoEnabled()) {
119                     _log.info("Status code " + status);
120                 }
121             }
122 
123             try {
124                 PermissionCheckerFactory.recycle(permissionChecker);
125             }
126             catch (Exception e) {
127             }
128         }
129     }
130 
131     protected String getRootPath(HttpServletRequest request) {
132         StringBuilder sb = new StringBuilder();
133 
134         sb.append(WebDAVUtil.fixPath(request.getContextPath()));
135         sb.append(WebDAVUtil.fixPath(request.getServletPath()));
136 
137         return sb.toString();
138     }
139 
140     protected WebDAVStorage getStorage(HttpServletRequest request)
141         throws WebDAVException {
142 
143         String[] pathArray = WebDAVUtil.getPathArray(
144             request.getPathInfo(), true);
145 
146         String storageClass = null;
147 
148         if (pathArray.length == 1) {
149             storageClass = CompanyWebDAVStorageImpl.class.getName();
150         }
151         else if (pathArray.length == 2) {
152             storageClass = GroupWebDAVStorageImpl.class.getName();
153         }
154         else if (pathArray.length >= 3) {
155             storageClass = WebDAVUtil.getStorageClass(pathArray[2]);
156         }
157 
158         if (Validator.isNull(storageClass)) {
159             throw new WebDAVException(
160                 "Invalid WebDAV path " + request.getPathInfo());
161         }
162 
163         return (WebDAVStorage)InstancePool.get(storageClass);
164     }
165 
166     protected boolean isIgnoredResource(HttpServletRequest request) {
167         String[] pathArray = WebDAVUtil.getPathArray(
168             request.getPathInfo(), true);
169 
170         if ((pathArray == null) || (pathArray.length <= 0)) {
171             return true;
172         }
173 
174         String resourceName = pathArray[pathArray.length - 1];
175 
176         for (String ignore : PropsValues.WEBDAV_IGNORE) {
177             if (ignore.equals(resourceName)) {
178                 if (_log.isDebugEnabled()) {
179                     _log.debug(
180                         "Skipping over " + request.getMethod() + " " +
181                             request.getPathInfo());
182                 }
183 
184                 return true;
185             }
186         }
187 
188         return false;
189     }
190 
191     private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
192 
193 }