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.StringMaker;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.security.auth.PrincipalThreadLocal;
31  import com.liferay.portal.security.permission.PermissionCheckerFactory;
32  import com.liferay.portal.security.permission.PermissionCheckerImpl;
33  import com.liferay.portal.security.permission.PermissionThreadLocal;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portal.util.PropsValues;
36  import com.liferay.portal.webdav.methods.Method;
37  import com.liferay.portal.webdav.methods.MethodFactory;
38  
39  import java.io.IOException;
40  
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServlet;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  /**
50   * <a href="WebDAVServlet.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Alexander Chow
54   *
55   */
56  public class WebDAVServlet extends HttpServlet {
57  
58      public void service(HttpServletRequest req, HttpServletResponse res)
59          throws IOException, ServletException {
60  
61          PermissionCheckerImpl permissionChecker = null;
62  
63          int status = HttpServletResponse.SC_NOT_IMPLEMENTED;
64  
65          try {
66              if (isIgnoredResource(req)) {
67                  status = HttpServletResponse.SC_NOT_FOUND;
68  
69                  return;
70              }
71  
72              WebDAVStorage storage = getStorage(req);
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(req));
79              }
80  
81              // Permission checker
82  
83              String remoteUser = req.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 = PermissionCheckerFactory.create(user, true);
93  
94                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
95              }
96  
97              // Get the method instance
98  
99              Method method = MethodFactory.create(req);
100 
101             // Process the method
102 
103             WebDAVRequest webDavReq = new WebDAVRequest(
104                 storage, req, res, permissionChecker);
105 
106             if (_log.isInfoEnabled()) {
107                 _log.info(
108                     "Remote user " + remoteUser + " " + req.getMethod() + " " +
109                         req.getRequestURI());
110             }
111 
112             status = method.process(webDavReq);
113         }
114         catch (Exception e) {
115             _log.error(e, e);
116         }
117         finally {
118             if (status > 0) {
119                 res.setStatus(status);
120 
121                 if (_log.isDebugEnabled()) {
122                     _log.debug("Returning status code " + status);
123                 }
124             }
125 
126             try {
127                 PermissionCheckerFactory.recycle(permissionChecker);
128             }
129             catch (Exception e) {
130             }
131         }
132     }
133 
134     protected String getRootPath(HttpServletRequest req) {
135         StringMaker sm = new StringMaker();
136 
137         sm.append(WebDAVUtil.fixPath(req.getContextPath()));
138         sm.append(WebDAVUtil.fixPath(req.getServletPath()));
139 
140         String rootPath = sm.toString();
141 
142         if (_log.isDebugEnabled()) {
143             _log.debug("Root path " + rootPath);
144         }
145 
146         return rootPath;
147     }
148 
149     protected WebDAVStorage getStorage(HttpServletRequest req)
150         throws WebDAVException {
151 
152         String[] pathArray = WebDAVUtil.getPathArray(req.getPathInfo(), true);
153 
154         String storageClass = null;
155 
156         if (pathArray.length == 1) {
157             storageClass = CompanyWebDAVStorageImpl.class.getName();
158         }
159         else if (pathArray.length == 2) {
160             storageClass = GroupWebDAVStorageImpl.class.getName();
161         }
162         else if (pathArray.length >= 3) {
163             storageClass = WebDAVUtil.getStorageClass(pathArray[2]);
164         }
165 
166         if (Validator.isNull(storageClass)) {
167             throw new WebDAVException(
168                 "Invalid WebDAV path " + req.getPathInfo());
169         }
170 
171         return (WebDAVStorage)InstancePool.get(storageClass);
172     }
173 
174     protected boolean isIgnoredResource(HttpServletRequest req) {
175         String[] pathArray = WebDAVUtil.getPathArray(req.getPathInfo(), true);
176         String resourceName = pathArray[pathArray.length - 1];
177 
178         for (String ignore : PropsValues.WEBDAV_IGNORE) {
179             if (ignore.equals(resourceName)) {
180                 if (_log.isDebugEnabled()) {
181                     _log.debug("Skipping over resource " + resourceName);
182                 }
183 
184                 return true;
185             }
186         }
187 
188         return false;
189     }
190 
191     private static Log _log = LogFactory.getLog(WebDAVServlet.class);
192 
193 }