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