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   */
50  public class WebDAVServlet extends HttpServlet {
51  
52      public void service(
53          HttpServletRequest request, HttpServletResponse response) {
54  
55          int status = HttpServletResponse.SC_PRECONDITION_FAILED;
56  
57          if (_log.isInfoEnabled()) {
58              _log.info(request.getMethod() + " " + request.getRequestURI());
59          }
60  
61          if (_log.isDebugEnabled()) {
62              _log.debug("User agent " + request.getHeader("User-agent"));
63          }
64  
65          try {
66              if (isIgnoredResource(request)) {
67                  status = HttpServletResponse.SC_NOT_FOUND;
68  
69                  return;
70              }
71  
72              WebDAVStorage storage = getStorage(request);
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(request));
79              }
80  
81              // Permission checker
82  
83              PermissionChecker permissionChecker = null;
84  
85              String remoteUser = request.getRemoteUser();
86  
87              if (remoteUser != null) {
88                  PrincipalThreadLocal.setName(remoteUser);
89  
90                  long userId = GetterUtil.getLong(remoteUser);
91  
92                  User user = UserLocalServiceUtil.getUserById(userId);
93  
94                  permissionChecker = PermissionCheckerFactoryUtil.create(
95                      user, true);
96  
97                  PermissionThreadLocal.setPermissionChecker(permissionChecker);
98              }
99  
100             // Get the method instance
101 
102             Method method = MethodFactory.create(request);
103 
104             // Process the method
105 
106             WebDAVRequest webDavRequest = new WebDAVRequestImpl(
107                 storage, request, response, permissionChecker);
108 
109             status = method.process(webDavRequest);
110         }
111         catch (Exception e) {
112             _log.error(e, e);
113         }
114         finally {
115             if (status > 0) {
116                 response.setStatus(status);
117 
118                 if (_log.isInfoEnabled()) {
119                     _log.info("Status code " + status);
120                 }
121             }
122         }
123     }
124 
125     protected String getRootPath(HttpServletRequest request) {
126         StringBuilder sb = new StringBuilder();
127 
128         sb.append(WebDAVUtil.fixPath(request.getContextPath()));
129         sb.append(WebDAVUtil.fixPath(request.getServletPath()));
130 
131         return sb.toString();
132     }
133 
134     protected WebDAVStorage getStorage(HttpServletRequest request)
135         throws WebDAVException {
136 
137         String[] pathArray = WebDAVUtil.getPathArray(
138             request.getPathInfo(), true);
139 
140         WebDAVStorage storage = null;
141 
142         if (pathArray.length == 1) {
143             storage = (WebDAVStorage)InstancePool.get(
144                 CompanyWebDAVStorageImpl.class.getName());
145         }
146         else if (pathArray.length == 2) {
147             storage = (WebDAVStorage)InstancePool.get(
148                 GroupWebDAVStorageImpl.class.getName());
149         }
150         else if (pathArray.length >= 3) {
151             storage = WebDAVUtil.getStorage(pathArray[2]);
152         }
153 
154         if (storage == null) {
155             throw new WebDAVException(
156                 "Invalid WebDAV path " + request.getPathInfo());
157         }
158 
159         return storage;
160     }
161 
162     protected boolean isIgnoredResource(HttpServletRequest request) {
163         String[] pathArray = WebDAVUtil.getPathArray(
164             request.getPathInfo(), true);
165 
166         if ((pathArray == null) || (pathArray.length <= 0)) {
167             return true;
168         }
169 
170         String resourceName = pathArray[pathArray.length - 1];
171 
172         for (String ignore : PropsValues.WEBDAV_IGNORE) {
173             if (ignore.equals(resourceName)) {
174                 if (_log.isDebugEnabled()) {
175                     _log.debug(
176                         "Skipping over " + request.getMethod() + " " +
177                             request.getPathInfo());
178                 }
179 
180                 return true;
181             }
182         }
183 
184         return false;
185     }
186 
187     private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
188 
189 }