1   /**
2    * Copyright (c) 2000-2008 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.util.GetterUtil;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.kernel.util.Validator;
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.PermissionCheckerFactory;
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  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  /**
46   * <a href="WebDAVServlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Alexander Chow
50   *
51   */
52  public class WebDAVServlet extends HttpServlet {
53  
54      public void service(
55          HttpServletRequest request, HttpServletResponse response) {
56  
57          PermissionChecker permissionChecker = null;
58  
59          int status = HttpServletResponse.SC_NOT_IMPLEMENTED;
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              // Set the path only if it has not already been set. This works
71              // if and only if the servlet is not mapped to more than one URL.
72  
73              if (storage.getRootPath() == null) {
74                  storage.setRootPath(getRootPath(request));
75              }
76  
77              // Permission checker
78  
79              String remoteUser = request.getRemoteUser();
80  
81              if (remoteUser != null) {
82                  PrincipalThreadLocal.setName(remoteUser);
83  
84                  long userId = GetterUtil.getLong(remoteUser);
85  
86                  User user = UserLocalServiceUtil.getUserById(userId);
87  
88                  permissionChecker = PermissionCheckerFactory.create(user, true);
89  
90                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
91              }
92  
93              // Get the method instance
94  
95              Method method = MethodFactory.create(request);
96  
97              // Process the method
98  
99              WebDAVRequest webDavRequest = new WebDAVRequest(
100                 storage, request, response, permissionChecker);
101 
102             if (_log.isInfoEnabled()) {
103                 _log.info(
104                     "Remote user " + remoteUser + " " + request.getMethod() +
105                         " " + request.getRequestURI());
106             }
107 
108             status = method.process(webDavRequest);
109         }
110         catch (Exception e) {
111             _log.error(e, e);
112         }
113         finally {
114             if (status > 0) {
115                 response.setStatus(status);
116 
117                 if (_log.isDebugEnabled()) {
118                     _log.debug("Returning status code " + status);
119                 }
120             }
121 
122             try {
123                 PermissionCheckerFactory.recycle(permissionChecker);
124             }
125             catch (Exception e) {
126             }
127         }
128     }
129 
130     protected String getRootPath(HttpServletRequest request) {
131         StringBuilder sb = new StringBuilder();
132 
133         sb.append(WebDAVUtil.fixPath(request.getContextPath()));
134         sb.append(WebDAVUtil.fixPath(request.getServletPath()));
135 
136         String rootPath = sb.toString();
137 
138         if (_log.isDebugEnabled()) {
139             _log.debug("Root path " + rootPath);
140         }
141 
142         return rootPath;
143     }
144 
145     protected WebDAVStorage getStorage(HttpServletRequest request)
146         throws WebDAVException {
147 
148         String[] pathArray = WebDAVUtil.getPathArray(
149             request.getPathInfo(), true);
150 
151         String storageClass = null;
152 
153         if (pathArray.length == 1) {
154             storageClass = CompanyWebDAVStorageImpl.class.getName();
155         }
156         else if (pathArray.length == 2) {
157             storageClass = GroupWebDAVStorageImpl.class.getName();
158         }
159         else if (pathArray.length >= 3) {
160             storageClass = WebDAVUtil.getStorageClass(pathArray[2]);
161         }
162 
163         if (Validator.isNull(storageClass)) {
164             throw new WebDAVException(
165                 "Invalid WebDAV path " + request.getPathInfo());
166         }
167 
168         return (WebDAVStorage)InstancePool.get(storageClass);
169     }
170 
171     protected boolean isIgnoredResource(HttpServletRequest request) {
172         String[] pathArray = WebDAVUtil.getPathArray(
173             request.getPathInfo(), true);
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("Skipping over resource " + resourceName);
180                 }
181 
182                 return true;
183             }
184         }
185 
186         return false;
187     }
188 
189     private static Log _log = LogFactory.getLog(WebDAVServlet.class);
190 
191 }