001
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
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
080
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
103
104 Method method = MethodFactory.create(request);
105
106
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 pathInfo = WebDAVUtil.stripOfficeExtension(
169 request.getPathInfo());
170
171 String[] pathArray = WebDAVUtil.getPathArray(pathInfo, true);
172
173 WebDAVStorage storage = null;
174
175 if (pathArray.length == 0) {
176 storage = (WebDAVStorage)InstancePool.get(
177 CompanyWebDAVStorageImpl.class.getName());
178 }
179 else if (pathArray.length == 1) {
180 storage = (WebDAVStorage)InstancePool.get(
181 GroupWebDAVStorageImpl.class.getName());
182 }
183 else if (pathArray.length >= 2) {
184 storage = WebDAVUtil.getStorage(pathArray[1]);
185 }
186
187 return storage;
188 }
189
190 protected boolean isIgnoredResource(HttpServletRequest request) {
191 String[] pathArray = WebDAVUtil.getPathArray(
192 request.getPathInfo(), true);
193
194 if ((pathArray == null) || (pathArray.length == 0)) {
195 return false;
196 }
197
198 for (String ignore : PropsValues.WEBDAV_IGNORE) {
199 String[] ignoreArray = ignore.split(StringPool.SLASH);
200
201 if (ignoreArray.length > pathArray.length) {
202 continue;
203 }
204
205 boolean match = true;
206
207 for (int i = 1; i <= ignoreArray.length; i++) {
208 if (!pathArray[pathArray.length - i].equals(
209 ignoreArray[ignoreArray.length - i])) {
210
211 match = false;
212
213 break;
214 }
215 }
216
217 if (match) {
218 if (_log.isDebugEnabled()) {
219 _log.debug(
220 "Skipping over " + request.getMethod() + " " +
221 request.getPathInfo());
222 }
223
224 return true;
225 }
226 }
227
228 return false;
229 }
230
231 private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
232
233 }