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