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