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