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.HttpUtil;
22 import com.liferay.portal.kernel.util.InstancePool;
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 if (storage == null) {
67 if (_log.isDebugEnabled()) {
68 _log.debug("Invalid WebDAV path " + request.getPathInfo());
69 }
70
71 return;
72 }
73
74
77 if (storage.getRootPath() == null) {
78 storage.setRootPath(getRootPath(request));
79 }
80
81 PermissionChecker permissionChecker = null;
82
83 String remoteUser = request.getRemoteUser();
84
85 if (remoteUser != null) {
86 PrincipalThreadLocal.setName(remoteUser);
87
88 long userId = GetterUtil.getLong(remoteUser);
89
90 User user = UserLocalServiceUtil.getUserById(userId);
91
92 permissionChecker = PermissionCheckerFactoryUtil.create(
93 user, true);
94
95 PermissionThreadLocal.setPermissionChecker(permissionChecker);
96 }
97
98
100 Method method = MethodFactory.create(request);
101
102
104 try {
105 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
106 storage, request, response, userAgent, permissionChecker);
107
108 status = method.process(webDavRequest);
109 }
110 catch (WebDAVException wde) {
111 if (_log.isWarnEnabled()) {
112 _log.warn(wde, wde);
113 }
114
115 status = HttpServletResponse.SC_PRECONDITION_FAILED;
116 }
117 }
118 catch (Exception e) {
119 _log.error(e, e);
120 }
121 finally {
122 response.setStatus(status);
123
124 if (_log.isInfoEnabled()) {
125 String xLitmus = GetterUtil.getString(
126 request.getHeader("X-Litmus"));
127
128 if (Validator.isNotNull(xLitmus)) {
129 xLitmus += " ";
130 }
131
132 _log.info(
133 xLitmus + request.getMethod() + " " +
134 request.getRequestURI() + " " + status);
135 }
136 }
137 }
138
139 protected String getRootPath(HttpServletRequest request) {
140 String contextPath = HttpUtil.fixPath(
141 request.getContextPath(), false, true);
142 String ServletPath = HttpUtil.fixPath(
143 request.getServletPath(), false, true);
144
145 return contextPath.concat(ServletPath);
146 }
147
148 protected WebDAVStorage getStorage(HttpServletRequest request) {
149 String[] pathArray = WebDAVUtil.getPathArray(
150 request.getPathInfo(), true);
151
152 WebDAVStorage storage = null;
153
154 if (pathArray.length == 0) {
155 storage = (WebDAVStorage)InstancePool.get(
156 CompanyWebDAVStorageImpl.class.getName());
157 }
158 else if (pathArray.length == 1) {
159 storage = (WebDAVStorage)InstancePool.get(
160 GroupWebDAVStorageImpl.class.getName());
161 }
162 else if (pathArray.length >= 2) {
163 storage = WebDAVUtil.getStorage(pathArray[1]);
164 }
165
166 return storage;
167 }
168
169 protected boolean isIgnoredResource(HttpServletRequest request) {
170 String[] pathArray = WebDAVUtil.getPathArray(
171 request.getPathInfo(), true);
172
173 if ((pathArray == null) || (pathArray.length == 0)) {
174 return false;
175 }
176
177 String resourceName = pathArray[pathArray.length - 1];
178
179 for (String ignore : PropsValues.WEBDAV_IGNORE) {
180 if (ignore.equals(resourceName)) {
181 if (_log.isDebugEnabled()) {
182 _log.debug(
183 "Skipping over " + request.getMethod() + " " +
184 request.getPathInfo());
185 }
186
187 return true;
188 }
189 }
190
191 return false;
192 }
193
194 private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
195
196 }