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