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.model.User;
30 import com.liferay.portal.security.auth.PrincipalThreadLocal;
31 import com.liferay.portal.security.permission.PermissionChecker;
32 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
33 import com.liferay.portal.security.permission.PermissionThreadLocal;
34 import com.liferay.portal.service.UserLocalServiceUtil;
35 import com.liferay.portal.util.PropsValues;
36 import com.liferay.portal.webdav.methods.Method;
37 import com.liferay.portal.webdav.methods.MethodFactory;
38
39 import javax.servlet.http.HttpServlet;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43
50 public class WebDAVServlet extends HttpServlet {
51
52 public void service(
53 HttpServletRequest request, HttpServletResponse response) {
54
55 int status = HttpServletResponse.SC_PRECONDITION_FAILED;
56
57 try {
58 if (isIgnoredResource(request)) {
59 status = HttpServletResponse.SC_NOT_FOUND;
60
61 return;
62 }
63
64 WebDAVStorage storage = getStorage(request);
65
66
69 if (storage.getRootPath() == null) {
70 storage.setRootPath(getRootPath(request));
71 }
72
73
75 PermissionChecker permissionChecker = null;
76
77 String remoteUser = request.getRemoteUser();
78
79 if (remoteUser != null) {
80 PrincipalThreadLocal.setName(remoteUser);
81
82 long userId = GetterUtil.getLong(remoteUser);
83
84 User user = UserLocalServiceUtil.getUserById(userId);
85
86 permissionChecker = PermissionCheckerFactoryUtil.create(
87 user, true);
88
89 PermissionThreadLocal.setPermissionChecker(permissionChecker);
90 }
91
92
94 Method method = MethodFactory.create(request);
95
96
98 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
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 }
122
123 protected String getRootPath(HttpServletRequest request) {
124 StringBuilder sb = new StringBuilder();
125
126 sb.append(WebDAVUtil.fixPath(request.getContextPath()));
127 sb.append(WebDAVUtil.fixPath(request.getServletPath()));
128
129 return sb.toString();
130 }
131
132 protected WebDAVStorage getStorage(HttpServletRequest request)
133 throws WebDAVException {
134
135 String[] pathArray = WebDAVUtil.getPathArray(
136 request.getPathInfo(), true);
137
138 WebDAVStorage storage = null;
139
140 if (pathArray.length == 1) {
141 storage = (WebDAVStorage)InstancePool.get(
142 CompanyWebDAVStorageImpl.class.getName());
143 }
144 else if (pathArray.length == 2) {
145 storage = (WebDAVStorage)InstancePool.get(
146 GroupWebDAVStorageImpl.class.getName());
147 }
148 else if (pathArray.length >= 3) {
149 storage = WebDAVUtil.getStorage(pathArray[2]);
150 }
151
152 if (storage == null) {
153 throw new WebDAVException(
154 "Invalid WebDAV path " + request.getPathInfo());
155 }
156
157 return storage;
158 }
159
160 protected boolean isIgnoredResource(HttpServletRequest request) {
161 String[] pathArray = WebDAVUtil.getPathArray(
162 request.getPathInfo(), true);
163
164 if ((pathArray == null) || (pathArray.length <= 0)) {
165 return false;
166 }
167
168 String resourceName = pathArray[pathArray.length - 1];
169
170 for (String ignore : PropsValues.WEBDAV_IGNORE) {
171 if (ignore.equals(resourceName)) {
172 if (_log.isDebugEnabled()) {
173 _log.debug(
174 "Skipping over " + request.getMethod() + " " +
175 request.getPathInfo());
176 }
177
178 return true;
179 }
180 }
181
182 return false;
183 }
184
185 private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
186
187 }