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