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