001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.webdav;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.InstancePool;
025: import com.liferay.portal.kernel.util.StringMaker;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.security.auth.PrincipalThreadLocal;
028: import com.liferay.portal.security.permission.PermissionCheckerFactory;
029: import com.liferay.portal.security.permission.PermissionCheckerImpl;
030: import com.liferay.portal.security.permission.PermissionThreadLocal;
031: import com.liferay.portal.service.UserLocalServiceUtil;
032: import com.liferay.portal.webdav.methods.Method;
033: import com.liferay.portal.webdav.methods.MethodFactory;
034:
035: import java.io.IOException;
036:
037: import javax.servlet.ServletConfig;
038: import javax.servlet.ServletException;
039: import javax.servlet.http.HttpServlet;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042:
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: /**
047: * <a href="WebDAVServlet.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: * @author Alexander Chow
051: *
052: */
053: public class WebDAVServlet extends HttpServlet {
054:
055: public void init(ServletConfig config) throws ServletException {
056: super .init(config);
057:
058: String storageClass = config.getInitParameter("storage-class");
059:
060: _storage = (WebDAVStorage) InstancePool.get(storageClass);
061: }
062:
063: public void service(HttpServletRequest req, HttpServletResponse res)
064: throws IOException, ServletException {
065:
066: PermissionCheckerImpl permissionChecker = null;
067:
068: int status = HttpServletResponse.SC_NOT_IMPLEMENTED;
069:
070: try {
071:
072: // Set the path only if it hasn't already been set. This works if
073: // and only if the servlet is not mapped to more than one URL.
074:
075: if (_storage.getRootPath() == null) {
076: _storage.setRootPath(getRootPath(req));
077: }
078:
079: // Permission checker
080:
081: String remoteUser = req.getRemoteUser();
082:
083: if (remoteUser != null) {
084: PrincipalThreadLocal.setName(remoteUser);
085:
086: long userId = GetterUtil.getLong(remoteUser);
087:
088: User user = UserLocalServiceUtil.getUserById(userId);
089:
090: permissionChecker = PermissionCheckerFactory.create(
091: user, true);
092:
093: PermissionThreadLocal
094: .setPermissionChecker(permissionChecker);
095: }
096:
097: // Get the method instance
098:
099: Method method = MethodFactory.create(req);
100:
101: // Process the method
102:
103: WebDAVRequest webDavReq = new WebDAVRequest(_storage, req,
104: res, permissionChecker);
105:
106: if (_log.isInfoEnabled()) {
107: _log.info("Remote user " + remoteUser + " "
108: + req.getMethod() + " " + req.getRequestURI());
109: }
110:
111: status = method.process(webDavReq);
112: } catch (Exception e) {
113: _log.error(e, e);
114: } finally {
115: if (status > 0) {
116: res.setStatus(status);
117:
118: if (_log.isInfoEnabled()) {
119: _log.info("Returning status code " + status);
120: }
121: }
122:
123: try {
124: PermissionCheckerFactory.recycle(permissionChecker);
125: } catch (Exception e) {
126: }
127: }
128: }
129:
130: protected String getRootPath(HttpServletRequest req) {
131: StringMaker sm = new StringMaker();
132:
133: sm.append(WebDAVUtil.fixPath(req.getContextPath()));
134: sm.append(WebDAVUtil.fixPath(req.getServletPath()));
135:
136: String rootPath = sm.toString();
137:
138: if (_log.isDebugEnabled()) {
139: _log.debug("Root path " + rootPath);
140: }
141:
142: return rootPath;
143: }
144:
145: private static Log _log = LogFactory.getLog(WebDAVServlet.class);
146:
147: private WebDAVStorage _storage;
148:
149: }
|