001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.webdav;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.HttpHeaders;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.InstancePool;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.webdav.WebDAVException;
026    import com.liferay.portal.kernel.webdav.WebDAVRequest;
027    import com.liferay.portal.kernel.webdav.WebDAVStorage;
028    import com.liferay.portal.kernel.webdav.WebDAVUtil;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.security.auth.PrincipalException;
031    import com.liferay.portal.security.auth.PrincipalThreadLocal;
032    import com.liferay.portal.security.permission.PermissionChecker;
033    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
034    import com.liferay.portal.security.permission.PermissionThreadLocal;
035    import com.liferay.portal.service.UserLocalServiceUtil;
036    import com.liferay.portal.util.PropsValues;
037    import com.liferay.portal.webdav.methods.Method;
038    import com.liferay.portal.webdav.methods.MethodFactory;
039    
040    import javax.servlet.http.HttpServlet;
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Alexander Chow
047     */
048    public class WebDAVServlet extends HttpServlet {
049    
050            @Override
051            public void service(
052                    HttpServletRequest request, HttpServletResponse response) {
053    
054                    int status = HttpServletResponse.SC_PRECONDITION_FAILED;
055    
056                    String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
057    
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("User agent " + userAgent);
060                    }
061    
062                    try {
063                            if (isIgnoredResource(request)) {
064                                    status = HttpServletResponse.SC_NOT_FOUND;
065    
066                                    return;
067                            }
068    
069                            WebDAVStorage storage = getStorage(request);
070    
071                            if (storage == null) {
072                                    if (_log.isDebugEnabled()) {
073                                            _log.debug("Invalid WebDAV path " + request.getPathInfo());
074                                    }
075    
076                                    return;
077                            }
078    
079                            // Set the path only if it has not already been set. This works
080                            // if and only if the servlet is not mapped to more than one URL.
081    
082                            if (storage.getRootPath() == null) {
083                                    storage.setRootPath(getRootPath(request));
084                            }
085    
086                            PermissionChecker permissionChecker = null;
087    
088                            String remoteUser = request.getRemoteUser();
089    
090                            if (remoteUser != null) {
091                                    PrincipalThreadLocal.setName(remoteUser);
092    
093                                    long userId = GetterUtil.getLong(remoteUser);
094    
095                                    User user = UserLocalServiceUtil.getUserById(userId);
096    
097                                    permissionChecker = PermissionCheckerFactoryUtil.create(user);
098    
099                                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
100                            }
101    
102                            // Get the method instance
103    
104                            Method method = MethodFactory.create(request);
105    
106                            // Process the method
107    
108                            try {
109                                    WebDAVRequest webDavRequest = new WebDAVRequestImpl(
110                                            storage, request, response, userAgent, permissionChecker);
111    
112                                    status = method.process(webDavRequest);
113                            }
114                            catch (WebDAVException wde) {
115                                    boolean logError = false;
116    
117                                    Throwable cause = wde;
118    
119                                    while (cause != null) {
120                                            if (cause instanceof PrincipalException) {
121                                                    logError = true;
122                                            }
123    
124                                            cause = cause.getCause();
125                                    }
126    
127                                    if (logError) {
128                                            _log.error(wde, wde);
129                                    }
130                                    else if (_log.isWarnEnabled()) {
131                                            _log.warn(wde, wde);
132                                    }
133    
134                                    status = HttpServletResponse.SC_PRECONDITION_FAILED;
135                            }
136                    }
137                    catch (Exception e) {
138                            _log.error(e, e);
139                    }
140                    finally {
141                            response.setStatus(status);
142    
143                            if (_log.isInfoEnabled()) {
144                                    String xLitmus = GetterUtil.getString(
145                                            request.getHeader("X-Litmus"));
146    
147                                    if (Validator.isNotNull(xLitmus)) {
148                                            xLitmus += " ";
149                                    }
150    
151                                    _log.info(
152                                            xLitmus + request.getMethod() + " " +
153                                                    request.getRequestURI() + " " + status);
154                            }
155                    }
156            }
157    
158            protected String getRootPath(HttpServletRequest request) {
159                    String contextPath = HttpUtil.fixPath(
160                            request.getContextPath(), false, true);
161                    String ServletPath = HttpUtil.fixPath(
162                            request.getServletPath(), false, true);
163    
164                    return contextPath.concat(ServletPath);
165            }
166    
167            protected WebDAVStorage getStorage(HttpServletRequest request) {
168                    String[] pathArray = WebDAVUtil.getPathArray(
169                            request.getPathInfo(), true);
170    
171                    WebDAVStorage storage = null;
172    
173                    if (pathArray.length == 0) {
174                            storage = (WebDAVStorage)InstancePool.get(
175                                    CompanyWebDAVStorageImpl.class.getName());
176                    }
177                    else if (pathArray.length == 1) {
178                            storage = (WebDAVStorage)InstancePool.get(
179                                    GroupWebDAVStorageImpl.class.getName());
180                    }
181                    else if (pathArray.length >= 2) {
182                            storage = WebDAVUtil.getStorage(pathArray[1]);
183                    }
184    
185                    return storage;
186            }
187    
188            protected boolean isIgnoredResource(HttpServletRequest request) {
189                    String[] pathArray = WebDAVUtil.getPathArray(
190                            request.getPathInfo(), true);
191    
192                    if ((pathArray == null) || (pathArray.length == 0)) {
193                            return false;
194                    }
195    
196                    for (String ignore : PropsValues.WEBDAV_IGNORE) {
197                            String[] ignoreArray = ignore.split(StringPool.SLASH);
198    
199                            if (ignoreArray.length > pathArray.length) {
200                                    continue;
201                            }
202    
203                            boolean match = true;
204    
205                            for (int i = 1; i <= ignoreArray.length; i++) {
206                                    if (!pathArray[pathArray.length - i].equals(
207                                                    ignoreArray[ignoreArray.length - i])) {
208    
209                                            match = false;
210    
211                                            break;
212                                    }
213                            }
214    
215                            if (match) {
216                                    if (_log.isDebugEnabled()) {
217                                            _log.debug(
218                                                    "Skipping over " + request.getMethod() + " " +
219                                                            request.getPathInfo());
220                                    }
221    
222                                    return true;
223                            }
224                    }
225    
226                    return false;
227            }
228    
229            private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
230    
231    }