1
22
23 package com.liferay.portal.webdav;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.InstancePool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.security.auth.PrincipalThreadLocal;
30 import com.liferay.portal.security.permission.PermissionChecker;
31 import com.liferay.portal.security.permission.PermissionCheckerFactory;
32 import com.liferay.portal.security.permission.PermissionThreadLocal;
33 import com.liferay.portal.service.UserLocalServiceUtil;
34 import com.liferay.portal.util.PropsValues;
35 import com.liferay.portal.webdav.methods.Method;
36 import com.liferay.portal.webdav.methods.MethodFactory;
37
38 import javax.servlet.http.HttpServlet;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45
52 public class WebDAVServlet extends HttpServlet {
53
54 public void service(
55 HttpServletRequest request, HttpServletResponse response) {
56
57 PermissionChecker permissionChecker = null;
58
59 int status = HttpServletResponse.SC_NOT_IMPLEMENTED;
60
61 try {
62 if (isIgnoredResource(request)) {
63 status = HttpServletResponse.SC_NOT_FOUND;
64
65 return;
66 }
67
68 WebDAVStorage storage = getStorage(request);
69
70
73 if (storage.getRootPath() == null) {
74 storage.setRootPath(getRootPath(request));
75 }
76
77
79 String remoteUser = request.getRemoteUser();
80
81 if (remoteUser != null) {
82 PrincipalThreadLocal.setName(remoteUser);
83
84 long userId = GetterUtil.getLong(remoteUser);
85
86 User user = UserLocalServiceUtil.getUserById(userId);
87
88 permissionChecker = PermissionCheckerFactory.create(user, true);
89
90 PermissionThreadLocal.setPermissionChecker(permissionChecker);
91 }
92
93
95 Method method = MethodFactory.create(request);
96
97
99 WebDAVRequest webDavRequest = new WebDAVRequest(
100 storage, request, response, permissionChecker);
101
102 if (_log.isInfoEnabled()) {
103 _log.info(
104 "Remote user " + remoteUser + " " + request.getMethod() +
105 " " + request.getRequestURI());
106 }
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.isDebugEnabled()) {
118 _log.debug("Returning 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 String rootPath = sb.toString();
137
138 if (_log.isDebugEnabled()) {
139 _log.debug("Root path " + rootPath);
140 }
141
142 return rootPath;
143 }
144
145 protected WebDAVStorage getStorage(HttpServletRequest request)
146 throws WebDAVException {
147
148 String[] pathArray = WebDAVUtil.getPathArray(
149 request.getPathInfo(), true);
150
151 String storageClass = null;
152
153 if (pathArray.length == 1) {
154 storageClass = CompanyWebDAVStorageImpl.class.getName();
155 }
156 else if (pathArray.length == 2) {
157 storageClass = GroupWebDAVStorageImpl.class.getName();
158 }
159 else if (pathArray.length >= 3) {
160 storageClass = WebDAVUtil.getStorageClass(pathArray[2]);
161 }
162
163 if (Validator.isNull(storageClass)) {
164 throw new WebDAVException(
165 "Invalid WebDAV path " + request.getPathInfo());
166 }
167
168 return (WebDAVStorage)InstancePool.get(storageClass);
169 }
170
171 protected boolean isIgnoredResource(HttpServletRequest request) {
172 String[] pathArray = WebDAVUtil.getPathArray(
173 request.getPathInfo(), true);
174 String resourceName = pathArray[pathArray.length - 1];
175
176 for (String ignore : PropsValues.WEBDAV_IGNORE) {
177 if (ignore.equals(resourceName)) {
178 if (_log.isDebugEnabled()) {
179 _log.debug("Skipping over resource " + resourceName);
180 }
181
182 return true;
183 }
184 }
185
186 return false;
187 }
188
189 private static Log _log = LogFactory.getLog(WebDAVServlet.class);
190
191 }