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.Validator;
024 import com.liferay.portal.kernel.webdav.WebDAVException;
025 import com.liferay.portal.kernel.webdav.WebDAVRequest;
026 import com.liferay.portal.kernel.webdav.WebDAVStorage;
027 import com.liferay.portal.kernel.webdav.WebDAVUtil;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.security.auth.PrincipalThreadLocal;
030 import com.liferay.portal.security.permission.PermissionChecker;
031 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
032 import com.liferay.portal.security.permission.PermissionThreadLocal;
033 import com.liferay.portal.service.UserLocalServiceUtil;
034 import com.liferay.portal.util.PropsValues;
035 import com.liferay.portal.webdav.methods.Method;
036 import com.liferay.portal.webdav.methods.MethodFactory;
037
038 import javax.servlet.http.HttpServlet;
039 import javax.servlet.http.HttpServletRequest;
040 import javax.servlet.http.HttpServletResponse;
041
042
046 public class WebDAVServlet extends HttpServlet {
047
048 public void service(
049 HttpServletRequest request, HttpServletResponse response) {
050
051 int status = HttpServletResponse.SC_PRECONDITION_FAILED;
052
053 String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
054
055 if (_log.isDebugEnabled()) {
056 _log.debug("User agent " + userAgent);
057 }
058
059 try {
060 if (isIgnoredResource(request)) {
061 status = HttpServletResponse.SC_NOT_FOUND;
062
063 return;
064 }
065
066 WebDAVStorage storage = getStorage(request);
067
068 if (storage == null) {
069 if (_log.isDebugEnabled()) {
070 _log.debug("Invalid WebDAV path " + request.getPathInfo());
071 }
072
073 return;
074 }
075
076
077
078
079 if (storage.getRootPath() == null) {
080 storage.setRootPath(getRootPath(request));
081 }
082
083 PermissionChecker permissionChecker = null;
084
085 String remoteUser = request.getRemoteUser();
086
087 if (remoteUser != null) {
088 PrincipalThreadLocal.setName(remoteUser);
089
090 long userId = GetterUtil.getLong(remoteUser);
091
092 User user = UserLocalServiceUtil.getUserById(userId);
093
094 permissionChecker = PermissionCheckerFactoryUtil.create(
095 user, true);
096
097 PermissionThreadLocal.setPermissionChecker(permissionChecker);
098 }
099
100
101
102 Method method = MethodFactory.create(request);
103
104
105
106 try {
107 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
108 storage, request, response, userAgent, permissionChecker);
109
110 status = method.process(webDavRequest);
111 }
112 catch (WebDAVException wde) {
113 if (_log.isWarnEnabled()) {
114 _log.warn(wde, wde);
115 }
116
117 status = HttpServletResponse.SC_PRECONDITION_FAILED;
118 }
119 }
120 catch (Exception e) {
121 _log.error(e, e);
122 }
123 finally {
124 response.setStatus(status);
125
126 if (_log.isInfoEnabled()) {
127 String xLitmus = GetterUtil.getString(
128 request.getHeader("X-Litmus"));
129
130 if (Validator.isNotNull(xLitmus)) {
131 xLitmus += " ";
132 }
133
134 _log.info(
135 xLitmus + request.getMethod() + " " +
136 request.getRequestURI() + " " + status);
137 }
138 }
139 }
140
141 protected String getRootPath(HttpServletRequest request) {
142 String contextPath = HttpUtil.fixPath(
143 request.getContextPath(), false, true);
144 String ServletPath = HttpUtil.fixPath(
145 request.getServletPath(), false, true);
146
147 return contextPath.concat(ServletPath);
148 }
149
150 protected WebDAVStorage getStorage(HttpServletRequest request) {
151 String[] pathArray = WebDAVUtil.getPathArray(
152 request.getPathInfo(), true);
153
154 WebDAVStorage storage = null;
155
156 if (pathArray.length == 0) {
157 storage = (WebDAVStorage)InstancePool.get(
158 CompanyWebDAVStorageImpl.class.getName());
159 }
160 else if (pathArray.length == 1) {
161 storage = (WebDAVStorage)InstancePool.get(
162 GroupWebDAVStorageImpl.class.getName());
163 }
164 else if (pathArray.length >= 2) {
165 storage = WebDAVUtil.getStorage(pathArray[1]);
166 }
167
168 return storage;
169 }
170
171 protected boolean isIgnoredResource(HttpServletRequest request) {
172 String[] pathArray = WebDAVUtil.getPathArray(
173 request.getPathInfo(), true);
174
175 if ((pathArray == null) || (pathArray.length == 0)) {
176 return false;
177 }
178
179 String resourceName = pathArray[pathArray.length - 1];
180
181 for (String ignore : PropsValues.WEBDAV_IGNORE) {
182 if (ignore.equals(resourceName)) {
183 if (_log.isDebugEnabled()) {
184 _log.debug(
185 "Skipping over " + request.getMethod() + " " +
186 request.getPathInfo());
187 }
188
189 return true;
190 }
191 }
192
193 return false;
194 }
195
196 private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
197
198 }