1
14
15 package com.liferay.portal.webdav;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.servlet.HttpHeaders;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.InstancePool;
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portal.kernel.util.Validator;
24 import com.liferay.portal.model.User;
25 import com.liferay.portal.security.auth.PrincipalThreadLocal;
26 import com.liferay.portal.security.permission.PermissionChecker;
27 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
28 import com.liferay.portal.security.permission.PermissionThreadLocal;
29 import com.liferay.portal.service.UserLocalServiceUtil;
30 import com.liferay.portal.util.PropsValues;
31 import com.liferay.portal.webdav.methods.Method;
32 import com.liferay.portal.webdav.methods.MethodFactory;
33
34 import javax.servlet.http.HttpServlet;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37
38
44 public class WebDAVServlet extends HttpServlet {
45
46 public void service(
47 HttpServletRequest request, HttpServletResponse response) {
48
49 int status = HttpServletResponse.SC_PRECONDITION_FAILED;
50
51 String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
52
53 if (_log.isDebugEnabled()) {
54 _log.debug("User agent " + userAgent);
55 }
56
57 try {
58 if (isIgnoredResource(request)) {
59 status = HttpServletResponse.SC_NOT_FOUND;
60
61 return;
62 }
63
64 WebDAVStorage storage = getStorage(request);
65
66
69 if (storage.getRootPath() == null) {
70 storage.setRootPath(getRootPath(request));
71 }
72
73 PermissionChecker permissionChecker = null;
74
75 String remoteUser = request.getRemoteUser();
76
77 if (remoteUser != null) {
78 PrincipalThreadLocal.setName(remoteUser);
79
80 long userId = GetterUtil.getLong(remoteUser);
81
82 User user = UserLocalServiceUtil.getUserById(userId);
83
84 permissionChecker = PermissionCheckerFactoryUtil.create(
85 user, true);
86
87 PermissionThreadLocal.setPermissionChecker(permissionChecker);
88 }
89
90
92 Method method = MethodFactory.create(request);
93
94
96 try {
97 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
98 storage, request, response, userAgent, permissionChecker);
99
100 status = method.process(webDavRequest);
101 }
102 catch (WebDAVException wde) {
103 if (_log.isWarnEnabled()) {
104 _log.warn(wde, wde);
105 }
106
107 status = HttpServletResponse.SC_PRECONDITION_FAILED;
108 }
109 }
110 catch (Exception e) {
111 _log.error(e, e);
112 }
113 finally {
114 response.setStatus(status);
115
116 if (_log.isInfoEnabled()) {
117 String xLitmus = GetterUtil.getString(
118 request.getHeader("X-Litmus"));
119
120 if (Validator.isNotNull(xLitmus)) {
121 xLitmus += " ";
122 }
123
124 _log.info(
125 xLitmus + request.getMethod() + " " +
126 request.getRequestURI() + " " + status);
127 }
128 }
129 }
130
131 protected String getRootPath(HttpServletRequest request) {
132 String contextPath = WebDAVUtil.fixPath(request.getContextPath());
133 String ServletPath = WebDAVUtil.fixPath(request.getServletPath());
134
135 return contextPath.concat(ServletPath);
136 }
137
138 protected WebDAVStorage getStorage(HttpServletRequest request)
139 throws WebDAVException {
140
141 String[] pathArray = WebDAVUtil.getPathArray(
142 request.getPathInfo(), true);
143
144 WebDAVStorage storage = null;
145
146 if (pathArray.length == 1) {
147 storage = (WebDAVStorage)InstancePool.get(
148 CompanyWebDAVStorageImpl.class.getName());
149 }
150 else if (pathArray.length == 2) {
151 storage = (WebDAVStorage)InstancePool.get(
152 GroupWebDAVStorageImpl.class.getName());
153 }
154 else if (pathArray.length >= 3) {
155 storage = WebDAVUtil.getStorage(pathArray[2]);
156 }
157
158 if (storage == null) {
159 throw new WebDAVException(
160 "Invalid WebDAV path " + request.getPathInfo());
161 }
162
163 return storage;
164 }
165
166 protected boolean isIgnoredResource(HttpServletRequest request) {
167 String[] pathArray = WebDAVUtil.getPathArray(
168 request.getPathInfo(), true);
169
170 if ((pathArray == null) || (pathArray.length <= 0)) {
171 return true;
172 }
173
174 for (String ignore : PropsValues.WEBDAV_IGNORE) {
175 String[] ignoreArray = ignore.split(StringPool.SLASH);
176
177 if (ignoreArray.length > pathArray.length) {
178 continue;
179 }
180
181 boolean match = true;
182
183 for (int i = 1; i <= ignoreArray.length; i++) {
184 if (!pathArray[pathArray.length - i].equals(
185 ignoreArray[ignoreArray.length - i])) {
186
187 match = false;
188
189 break;
190 }
191 }
192
193 if (match) {
194 if (_log.isDebugEnabled()) {
195 _log.debug(
196 "Skipping over " + request.getMethod() + " " +
197 request.getPathInfo());
198 }
199
200 return true;
201 }
202 }
203
204 return false;
205 }
206
207 private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
208
209 }