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_PRECONDITION_FAILED;
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(request.getMethod() + " " + request.getRequestURI());
104 }
105
106 status = method.process(webDavRequest);
107 }
108 catch (Exception e) {
109 _log.error(e, e);
110 }
111 finally {
112 if (status > 0) {
113 response.setStatus(status);
114
115 if (_log.isInfoEnabled()) {
116 _log.info("Status code " + status);
117 }
118 }
119
120 try {
121 PermissionCheckerFactory.recycle(permissionChecker);
122 }
123 catch (Exception e) {
124 }
125 }
126 }
127
128 protected String getRootPath(HttpServletRequest request) {
129 StringBuilder sb = new StringBuilder();
130
131 sb.append(WebDAVUtil.fixPath(request.getContextPath()));
132 sb.append(WebDAVUtil.fixPath(request.getServletPath()));
133
134 return sb.toString();
135 }
136
137 protected WebDAVStorage getStorage(HttpServletRequest request)
138 throws WebDAVException {
139
140 String[] pathArray = WebDAVUtil.getPathArray(
141 request.getPathInfo(), true);
142
143 String storageClass = null;
144
145 if (pathArray.length == 1) {
146 storageClass = CompanyWebDAVStorageImpl.class.getName();
147 }
148 else if (pathArray.length == 2) {
149 storageClass = GroupWebDAVStorageImpl.class.getName();
150 }
151 else if (pathArray.length >= 3) {
152 storageClass = WebDAVUtil.getStorageClass(pathArray[2]);
153 }
154
155 if (Validator.isNull(storageClass)) {
156 throw new WebDAVException(
157 "Invalid WebDAV path " + request.getPathInfo());
158 }
159
160 return (WebDAVStorage)InstancePool.get(storageClass);
161 }
162
163 protected boolean isIgnoredResource(HttpServletRequest request) {
164 String[] pathArray = WebDAVUtil.getPathArray(
165 request.getPathInfo(), true);
166 String resourceName = pathArray[pathArray.length - 1];
167
168 for (String ignore : PropsValues.WEBDAV_IGNORE) {
169 if (ignore.equals(resourceName)) {
170 if (_log.isDebugEnabled()) {
171 _log.debug(
172 "Skipping over " + request.getMethod() + " " +
173 request.getPathInfo());
174 }
175
176 return true;
177 }
178 }
179
180 return false;
181 }
182
183 private static Log _log = LogFactory.getLog(WebDAVServlet.class);
184
185 }