1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.model.User;
30  import com.liferay.portal.security.auth.PrincipalThreadLocal;
31  import com.liferay.portal.security.permission.PermissionChecker;
32  import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
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 javax.servlet.http.HttpServlet;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  /**
44   * <a href="WebDAVServlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Alexander Chow
48   */
49  public class WebDAVServlet extends HttpServlet {
50  
51      public void service(
52          HttpServletRequest request, HttpServletResponse response) {
53  
54          int status = HttpServletResponse.SC_PRECONDITION_FAILED;
55  
56          if (_log.isInfoEnabled()) {
57              _log.info(request.getMethod() + " " + request.getRequestURI());
58          }
59  
60          if (_log.isDebugEnabled()) {
61              _log.debug("User agent " + request.getHeader("User-agent"));
62          }
63  
64          try {
65              if (isIgnoredResource(request)) {
66                  status = HttpServletResponse.SC_NOT_FOUND;
67  
68                  return;
69              }
70  
71              WebDAVStorage storage = getStorage(request);
72  
73              // Set the path only if it has not already been set. This works
74              // if and only if the servlet is not mapped to more than one URL.
75  
76              if (storage.getRootPath() == null) {
77                  storage.setRootPath(getRootPath(request));
78              }
79  
80              // Permission checker
81  
82              PermissionChecker permissionChecker = null;
83  
84              String remoteUser = request.getRemoteUser();
85  
86              if (remoteUser != null) {
87                  PrincipalThreadLocal.setName(remoteUser);
88  
89                  long userId = GetterUtil.getLong(remoteUser);
90  
91                  User user = UserLocalServiceUtil.getUserById(userId);
92  
93                  permissionChecker = PermissionCheckerFactoryUtil.create(
94                      user, true);
95  
96                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
97              }
98  
99              // Get the method instance
100 
101             Method method = MethodFactory.create(request);
102 
103             // Process the method
104 
105             WebDAVRequest webDavRequest = new WebDAVRequestImpl(
106                 storage, request, response, permissionChecker);
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.isInfoEnabled()) {
118                     _log.info("Status code " + status);
119                 }
120             }
121         }
122     }
123 
124     protected String getRootPath(HttpServletRequest request) {
125         StringBuilder sb = new StringBuilder();
126 
127         sb.append(WebDAVUtil.fixPath(request.getContextPath()));
128         sb.append(WebDAVUtil.fixPath(request.getServletPath()));
129 
130         return sb.toString();
131     }
132 
133     protected WebDAVStorage getStorage(HttpServletRequest request)
134         throws WebDAVException {
135 
136         String[] pathArray = WebDAVUtil.getPathArray(
137             request.getPathInfo(), true);
138 
139         WebDAVStorage storage = null;
140 
141         if (pathArray.length == 1) {
142             storage = (WebDAVStorage)InstancePool.get(
143                 CompanyWebDAVStorageImpl.class.getName());
144         }
145         else if (pathArray.length == 2) {
146             storage = (WebDAVStorage)InstancePool.get(
147                 GroupWebDAVStorageImpl.class.getName());
148         }
149         else if (pathArray.length >= 3) {
150             storage = WebDAVUtil.getStorage(pathArray[2]);
151         }
152 
153         if (storage == null) {
154             throw new WebDAVException(
155                 "Invalid WebDAV path " + request.getPathInfo());
156         }
157 
158         return storage;
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 true;
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 }