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