1
14
15 package com.liferay.portal.webdav;
16
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.kernel.servlet.HttpHeaders;
20 import com.liferay.portal.kernel.util.GetterUtil;
21 import com.liferay.portal.kernel.util.InstancePool;
22 import com.liferay.portal.kernel.util.Validator;
23 import com.liferay.portal.model.User;
24 import com.liferay.portal.security.auth.PrincipalThreadLocal;
25 import com.liferay.portal.security.permission.PermissionChecker;
26 import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
27 import com.liferay.portal.security.permission.PermissionThreadLocal;
28 import com.liferay.portal.service.UserLocalServiceUtil;
29 import com.liferay.portal.util.PropsValues;
30 import com.liferay.portal.webdav.methods.Method;
31 import com.liferay.portal.webdav.methods.MethodFactory;
32
33 import javax.servlet.http.HttpServlet;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37
43 public class WebDAVServlet extends HttpServlet {
44
45 public void service(
46 HttpServletRequest request, HttpServletResponse response) {
47
48 int status = HttpServletResponse.SC_PRECONDITION_FAILED;
49
50 String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
51
52 if (_log.isDebugEnabled()) {
53 _log.debug("User agent " + userAgent);
54 }
55
56 try {
57 if (isIgnoredResource(request)) {
58 status = HttpServletResponse.SC_NOT_FOUND;
59
60 return;
61 }
62
63 WebDAVStorage storage = getStorage(request);
64
65
68 if (storage.getRootPath() == null) {
69 storage.setRootPath(getRootPath(request));
70 }
71
72 PermissionChecker permissionChecker = null;
73
74 String remoteUser = request.getRemoteUser();
75
76 if (remoteUser != null) {
77 PrincipalThreadLocal.setName(remoteUser);
78
79 long userId = GetterUtil.getLong(remoteUser);
80
81 User user = UserLocalServiceUtil.getUserById(userId);
82
83 permissionChecker = PermissionCheckerFactoryUtil.create(
84 user, true);
85
86 PermissionThreadLocal.setPermissionChecker(permissionChecker);
87 }
88
89
91 Method method = MethodFactory.create(request);
92
93
95 try {
96 WebDAVRequest webDavRequest = new WebDAVRequestImpl(
97 storage, request, response, userAgent, permissionChecker);
98
99 status = method.process(webDavRequest);
100 }
101 catch (WebDAVException wde) {
102 if (_log.isWarnEnabled()) {
103 _log.warn(wde, wde);
104 }
105
106 status = HttpServletResponse.SC_PRECONDITION_FAILED;
107 }
108 }
109 catch (Exception e) {
110 _log.error(e, e);
111 }
112 finally {
113 response.setStatus(status);
114
115 if (_log.isInfoEnabled()) {
116 String xLitmus = GetterUtil.getString(
117 request.getHeader("X-Litmus"));
118
119 if (Validator.isNotNull(xLitmus)) {
120 xLitmus += " ";
121 }
122
123 _log.info(
124 xLitmus + request.getMethod() + " " +
125 request.getRequestURI() + " " + status);
126 }
127 }
128 }
129
130 protected String getRootPath(HttpServletRequest request) {
131 String contextPath = WebDAVUtil.fixPath(request.getContextPath());
132 String ServletPath = WebDAVUtil.fixPath(request.getServletPath());
133
134 return contextPath.concat(ServletPath);
135 }
136
137 protected WebDAVStorage getStorage(HttpServletRequest request)
138 throws WebDAVException {
139
140 String[] pathArray = WebDAVUtil.getPathArray(
141 request.getPathInfo(), true);
142
143 WebDAVStorage storage = null;
144
145 if (pathArray.length == 1) {
146 storage = (WebDAVStorage)InstancePool.get(
147 CompanyWebDAVStorageImpl.class.getName());
148 }
149 else if (pathArray.length == 2) {
150 storage = (WebDAVStorage)InstancePool.get(
151 GroupWebDAVStorageImpl.class.getName());
152 }
153 else if (pathArray.length >= 3) {
154 storage = WebDAVUtil.getStorage(pathArray[2]);
155 }
156
157 if (storage == null) {
158 throw new WebDAVException(
159 "Invalid WebDAV path " + request.getPathInfo());
160 }
161
162 return storage;
163 }
164
165 protected boolean isIgnoredResource(HttpServletRequest request) {
166 String[] pathArray = WebDAVUtil.getPathArray(
167 request.getPathInfo(), true);
168
169 if ((pathArray == null) || (pathArray.length <= 0)) {
170 return true;
171 }
172
173 String resourceName = pathArray[pathArray.length - 1];
174
175 for (String ignore : PropsValues.WEBDAV_IGNORE) {
176 if (ignore.equals(resourceName)) {
177 if (_log.isDebugEnabled()) {
178 _log.debug(
179 "Skipping over " + request.getMethod() + " " +
180 request.getPathInfo());
181 }
182
183 return true;
184 }
185 }
186
187 return false;
188 }
189
190 private static Log _log = LogFactoryUtil.getLog(WebDAVServlet.class);
191
192 }