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
54 public class WebDAVServlet extends HttpServlet {
55
56 public void init(ServletConfig config) throws ServletException {
57 super.init(config);
58
59 String storageClass = config.getInitParameter("storage-class");
60
61 _storage = (WebDAVStorage)InstancePool.get(storageClass);
62 }
63
64 public void service(HttpServletRequest req, HttpServletResponse res)
65 throws IOException, ServletException {
66
67 PermissionCheckerImpl permissionChecker = null;
68
69 try {
70
71
74 if (_storage.getRootPath() == null) {
75 _storage.setRootPath(getRootPath(req));
76 }
77
78
80 String remoteUser = req.getRemoteUser();
81
82 if (_log.isDebugEnabled()) {
83 _log.debug("Remote user " + remoteUser);
84 }
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 = PermissionCheckerFactory.create(user, true);
94
95 PermissionThreadLocal.setPermissionChecker(permissionChecker);
96 }
97
98
100 Method method = MethodFactory.create(req);
101
102
104 WebDAVRequest webDavReq = new WebDAVRequest(
105 _storage, req, res, permissionChecker);
106
107 if (_log.isDebugEnabled()) {
108 _log.debug("Path " + webDavReq.getPath());
109 }
110
111 method.process(webDavReq);
112 }
113 catch (Exception e) {
114 _log.error(e, e);
115 }
116 finally {
117 try {
118 PermissionCheckerFactory.recycle(permissionChecker);
119 }
120 catch (Exception e) {
121 }
122 }
123 }
124
125 protected String getRootPath(HttpServletRequest req) {
126 StringMaker sm = new StringMaker();
127
128 sm.append(WebDAVUtil.fixPath(req.getContextPath()));
129 sm.append(WebDAVUtil.fixPath(req.getServletPath()));
130
131 String rootPath = sm.toString();
132
133 if (_log.isDebugEnabled()) {
134 _log.debug("Root path " + rootPath);
135 }
136
137 return rootPath;
138 }
139
140 private static Log _log = LogFactory.getLog(WebDAVServlet.class);
141
142 private WebDAVStorage _storage;
143
144 }