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.StringMaker;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.security.auth.PrincipalThreadLocal;
30 import com.liferay.portal.security.permission.PermissionCheckerFactory;
31 import com.liferay.portal.security.permission.PermissionCheckerImpl;
32 import com.liferay.portal.security.permission.PermissionThreadLocal;
33 import com.liferay.portal.service.UserLocalServiceUtil;
34 import com.liferay.portal.webdav.methods.Method;
35 import com.liferay.portal.webdav.methods.MethodFactory;
36
37 import java.io.IOException;
38
39 import javax.servlet.ServletConfig;
40 import javax.servlet.ServletException;
41 import javax.servlet.http.HttpServlet;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48
55 public class WebDAVServlet extends HttpServlet {
56
57 public void init(ServletConfig config) throws ServletException {
58 super.init(config);
59
60 String storageClass = config.getInitParameter("storage-class");
61
62 _storage = (WebDAVStorage)InstancePool.get(storageClass);
63 }
64
65 public void service(HttpServletRequest req, HttpServletResponse res)
66 throws IOException, ServletException {
67
68 PermissionCheckerImpl permissionChecker = null;
69
70 int status = HttpServletResponse.SC_NOT_IMPLEMENTED;
71
72 try {
73
74
77 if (_storage.getRootPath() == null) {
78 _storage.setRootPath(getRootPath(req));
79 }
80
81
83 String remoteUser = req.getRemoteUser();
84
85 if (remoteUser != null) {
86 PrincipalThreadLocal.setName(remoteUser);
87
88 long userId = GetterUtil.getLong(remoteUser);
89
90 User user = UserLocalServiceUtil.getUserById(userId);
91
92 permissionChecker = PermissionCheckerFactory.create(user, true);
93
94 PermissionThreadLocal.setPermissionChecker(permissionChecker);
95 }
96
97
99 Method method = MethodFactory.create(req);
100
101
103 WebDAVRequest webDavReq = new WebDAVRequest(
104 _storage, req, res, permissionChecker);
105
106 if (_log.isInfoEnabled()) {
107 _log.info(
108 "Remote user " + remoteUser + " " + req.getMethod() + " " +
109 req.getRequestURI());
110 }
111
112 status = method.process(webDavReq);
113 }
114 catch (Exception e) {
115 _log.error(e, e);
116 }
117 finally {
118 if (status > 0) {
119 res.setStatus(status);
120
121 if (_log.isInfoEnabled()) {
122 _log.info("Returning status code " + status);
123 }
124 }
125
126 try {
127 PermissionCheckerFactory.recycle(permissionChecker);
128 }
129 catch (Exception e) {
130 }
131 }
132 }
133
134 protected String getRootPath(HttpServletRequest req) {
135 StringMaker sm = new StringMaker();
136
137 sm.append(WebDAVUtil.fixPath(req.getContextPath()));
138 sm.append(WebDAVUtil.fixPath(req.getServletPath()));
139
140 String rootPath = sm.toString();
141
142 if (_log.isDebugEnabled()) {
143 _log.debug("Root path " + rootPath);
144 }
145
146 return rootPath;
147 }
148
149 private static Log _log = LogFactory.getLog(WebDAVServlet.class);
150
151 private WebDAVStorage _storage;
152
153 }