1 package org
.argeo
.cms
.internal
.kernel
;
3 import java
.io
.IOException
;
4 import java
.nio
.file
.Files
;
5 import java
.nio
.file
.Path
;
6 import java
.util
.Properties
;
8 import javax
.jcr
.Repository
;
9 import javax
.servlet
.ServletException
;
10 import javax
.servlet
.http
.HttpServletRequest
;
11 import javax
.servlet
.http
.HttpServletResponse
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.apache
.jackrabbit
.server
.SessionProvider
;
16 import org
.apache
.jackrabbit
.server
.remoting
.davex
.JcrRemotingServlet
;
17 import org
.apache
.jackrabbit
.webdav
.simple
.SimpleWebdavServlet
;
18 import org
.argeo
.cms
.CmsException
;
19 import org
.argeo
.cms
.internal
.http
.CmsSessionProvider
;
20 import org
.argeo
.cms
.internal
.http
.DataHttpContext
;
21 import org
.argeo
.cms
.internal
.http
.HttpUtils
;
22 import org
.argeo
.cms
.internal
.http
.LinkServlet
;
23 import org
.argeo
.cms
.internal
.http
.PrivateHttpContext
;
24 import org
.argeo
.cms
.internal
.http
.RobotServlet
;
25 import org
.argeo
.node
.NodeConstants
;
26 import org
.osgi
.framework
.BundleContext
;
27 import org
.osgi
.framework
.FrameworkUtil
;
28 import org
.osgi
.framework
.ServiceReference
;
29 import org
.osgi
.service
.http
.HttpService
;
30 import org
.osgi
.service
.http
.NamespaceException
;
31 import org
.osgi
.util
.tracker
.ServiceTracker
;
34 * Intercepts and enriches http access, mainly focusing on security and
37 public class NodeHttp
implements KernelConstants
{
38 private final static Log log
= LogFactory
.getLog(NodeHttp
.class);
40 public final static String DEFAULT_SERVICE
= "HTTP";
42 private final BundleContext bc
= FrameworkUtil
.getBundle(getClass()).getBundleContext();
44 private ServiceTracker
<Repository
, Repository
> repositories
;
45 private final ServiceTracker
<HttpService
, HttpService
> httpServiceTracker
;
47 private static String httpRealm
= "Argeo";
48 private final boolean cleanState
;
50 public NodeHttp(boolean cleanState
) {
51 this.cleanState
= cleanState
;
52 httpServiceTracker
= new PrepareHttpStc();
53 // httpServiceTracker.open();
54 KernelUtils
.asyncOpen(httpServiceTracker
);
57 public void destroy() {
58 if (repositories
!= null)
62 public static void registerRepositoryServlets(HttpService httpService
, String alias
, Repository repository
) {
63 if (httpService
== null)
64 throw new CmsException("No HTTP service available");
66 registerWebdavServlet(httpService
, alias
, repository
);
67 registerRemotingServlet(httpService
, alias
, repository
);
68 if (NodeConstants
.HOME
.equals(alias
))
69 registerFilesServlet(httpService
, alias
, repository
);
70 if (log
.isDebugEnabled())
71 log
.debug("Registered servlets for repository '" + alias
+ "'");
72 } catch (Exception e
) {
73 throw new CmsException("Could not register servlets for repository '" + alias
+ "'", e
);
77 public static void unregisterRepositoryServlets(HttpService httpService
, String alias
) {
78 if (httpService
== null)
81 httpService
.unregister(webdavPath(alias
));
82 httpService
.unregister(remotingPath(alias
));
83 if (NodeConstants
.HOME
.equals(alias
))
84 httpService
.unregister(filesPath(alias
));
85 if (log
.isDebugEnabled())
86 log
.debug("Unregistered servlets for repository '" + alias
+ "'");
87 } catch (Exception e
) {
88 log
.error("Could not unregister servlets for repository '" + alias
+ "'", e
);
92 static void registerWebdavServlet(HttpService httpService
, String alias
, Repository repository
)
93 throws NamespaceException
, ServletException
{
94 // WebdavServlet webdavServlet = new WebdavServlet(repository, new
95 // OpenInViewSessionProvider(alias));
96 WebdavServlet webdavServlet
= new WebdavServlet(repository
, new CmsSessionProvider(alias
));
97 String path
= webdavPath(alias
);
98 Properties ip
= new Properties();
99 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_CONFIG
, HttpUtils
.WEBDAV_CONFIG
);
100 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_PATH_PREFIX
, path
);
101 httpService
.registerServlet(path
, webdavServlet
, ip
, new DataHttpContext(httpRealm
));
104 static void registerFilesServlet(HttpService httpService
, String alias
, Repository repository
)
105 throws NamespaceException
, ServletException
{
106 WebdavServlet filesServlet
= new WebdavServlet(repository
, new CmsSessionProvider(alias
));
107 String path
= filesPath(alias
);
108 Properties ip
= new Properties();
109 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_CONFIG
, HttpUtils
.WEBDAV_CONFIG
);
110 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_PATH_PREFIX
, path
);
111 httpService
.registerServlet(path
, filesServlet
, ip
, new PrivateHttpContext(httpRealm
, true));
114 static void registerRemotingServlet(HttpService httpService
, String alias
, Repository repository
)
115 throws NamespaceException
, ServletException
{
116 RemotingServlet remotingServlet
= new RemotingServlet(repository
, new CmsSessionProvider(alias
));
117 String path
= remotingPath(alias
);
118 Properties ip
= new Properties();
119 ip
.setProperty(JcrRemotingServlet
.INIT_PARAM_RESOURCE_PATH_PREFIX
, path
);
120 ip
.setProperty(JcrRemotingServlet
.INIT_PARAM_AUTHENTICATE_HEADER
, "Negotiate");
122 // Looks like a bug in Jackrabbit remoting init
125 tmpDir
= Files
.createTempDirectory("remoting_" + alias
);
126 } catch (IOException e
) {
127 throw new CmsException("Cannot create temp directory for remoting servlet", e
);
129 ip
.setProperty(RemotingServlet
.INIT_PARAM_HOME
, tmpDir
.toString());
130 ip
.setProperty(RemotingServlet
.INIT_PARAM_TMP_DIRECTORY
, "remoting_" + alias
);
131 ip
.setProperty(RemotingServlet
.INIT_PARAM_PROTECTED_HANDLERS_CONFIG
, HttpUtils
.DEFAULT_PROTECTED_HANDLERS
);
132 ip
.setProperty(RemotingServlet
.INIT_PARAM_CREATE_ABSOLUTE_URI
, "false");
133 httpService
.registerServlet(path
, remotingServlet
, ip
, new PrivateHttpContext(httpRealm
));
136 static String
webdavPath(String alias
) {
137 return NodeConstants
.PATH_DATA
+ "/" + alias
;
140 static String
remotingPath(String alias
) {
141 return NodeConstants
.PATH_JCR
+ "/" + alias
;
144 static String
filesPath(String alias
) {
145 return NodeConstants
.PATH_FILES
;
148 // private Subject subjectFromRequest(HttpServletRequest request,
149 // HttpServletResponse response) {
150 // Authorization authorization = (Authorization)
151 // request.getAttribute(HttpContext.AUTHORIZATION);
152 // if (authorization == null)
153 // throw new CmsException("Not authenticated");
155 // LoginContext lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER,
156 // new HttpRequestCallbackHandler(request, response));
158 // return lc.getSubject();
159 // } catch (LoginException e) {
160 // throw new CmsException("Cannot login", e);
164 static class RepositoriesStc
extends ServiceTracker
<Repository
, Repository
> {
165 private final HttpService httpService
;
167 private final BundleContext bc
;
169 public RepositoriesStc(BundleContext bc
, HttpService httpService
) {
170 super(bc
, Repository
.class, null);
171 this.httpService
= httpService
;
176 public Repository
addingService(ServiceReference
<Repository
> reference
) {
177 Repository repository
= bc
.getService(reference
);
178 Object jcrRepoAlias
= reference
.getProperty(NodeConstants
.CN
);
179 if (jcrRepoAlias
!= null) {
180 String alias
= jcrRepoAlias
.toString();
181 registerRepositoryServlets(httpService
, alias
, repository
);
187 public void modifiedService(ServiceReference
<Repository
> reference
, Repository service
) {
191 public void removedService(ServiceReference
<Repository
> reference
, Repository service
) {
192 Object jcrRepoAlias
= reference
.getProperty(NodeConstants
.CN
);
193 if (jcrRepoAlias
!= null) {
194 String alias
= jcrRepoAlias
.toString();
195 unregisterRepositoryServlets(httpService
, alias
);
200 private class PrepareHttpStc
extends ServiceTracker
<HttpService
, HttpService
> {
201 // private DataHttp dataHttp;
202 // private NodeHttp nodeHttp;
204 public PrepareHttpStc() {
205 super(bc
, HttpService
.class, null);
209 public HttpService
addingService(ServiceReference
<HttpService
> reference
) {
210 long begin
= System
.currentTimeMillis();
211 if (log
.isTraceEnabled())
212 log
.trace("HTTP prepare starts...");
213 HttpService httpService
= addHttpService(reference
);
214 if (log
.isTraceEnabled())
215 log
.trace("HTTP prepare duration: " + (System
.currentTimeMillis() - begin
) + "ms");
220 public void removedService(ServiceReference
<HttpService
> reference
, HttpService service
) {
221 // if (dataHttp != null)
222 // dataHttp.destroy();
224 // if (nodeHttp != null)
225 // nodeHttp.destroy();
228 repositories
.close();
232 private HttpService
addHttpService(ServiceReference
<HttpService
> sr
) {
233 HttpService httpService
= bc
.getService(sr
);
234 // TODO find constants
235 Object httpPort
= sr
.getProperty("http.port");
236 Object httpsPort
= sr
.getProperty("https.port");
239 httpService
.registerServlet("/!", new LinkServlet(), null, null);
240 httpService
.registerServlet("/robots.txt", new RobotServlet(), null, null);
241 } catch (Exception e
) {
242 throw new CmsException("Cannot register filters", e
);
244 // track repositories
245 if (repositories
!= null)
246 throw new CmsException("An http service is already configured");
247 repositories
= new RepositoriesStc(bc
, httpService
);
248 // repositories.open();
250 KernelUtils
.asyncOpen(repositories
);
251 log
.info(httpPortsMsg(httpPort
, httpsPort
));
252 // httpAvailable = true;
255 bc
.registerService(NodeHttp
.class, NodeHttp
.this, null);
259 private String
httpPortsMsg(Object httpPort
, Object httpsPort
) {
260 return "HTTP " + httpPort
+ (httpsPort
!= null ?
" - HTTPS " + httpsPort
: "");
264 private static class WebdavServlet
extends SimpleWebdavServlet
{
265 private static final long serialVersionUID
= -4687354117811443881L;
266 private final Repository repository
;
268 public WebdavServlet(Repository repository
, SessionProvider sessionProvider
) {
269 this.repository
= repository
;
270 setSessionProvider(sessionProvider
);
273 public Repository
getRepository() {
278 protected void service(final HttpServletRequest request
, final HttpServletResponse response
)
279 throws ServletException
, IOException
{
280 WebdavServlet
.super.service(request
, response
);
282 // Subject subject = subjectFromRequest(request);
283 // // TODO make it stronger, with eTags.
284 // // if (CurrentUser.isAnonymous(subject) &&
285 // // request.getMethod().equals("GET")) {
286 // // response.setHeader("Cache-Control", "no-transform, public,
287 // // max-age=300, s-maxage=900");
290 // Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
292 // public Void run() throws Exception {
293 // WebdavServlet.super.service(request, response);
297 // } catch (PrivilegedActionException e) {
298 // throw new CmsException("Cannot process webdav request",
299 // e.getException());
305 private static class RemotingServlet
extends JcrRemotingServlet
{
306 private final Log log
= LogFactory
.getLog(RemotingServlet
.class);
307 private static final long serialVersionUID
= 4605238259548058883L;
308 private final Repository repository
;
309 private final SessionProvider sessionProvider
;
311 public RemotingServlet(Repository repository
, SessionProvider sessionProvider
) {
312 this.repository
= repository
;
313 this.sessionProvider
= sessionProvider
;
317 protected Repository
getRepository() {
322 protected SessionProvider
getSessionProvider() {
323 return sessionProvider
;
327 protected void service(final HttpServletRequest request
, final HttpServletResponse response
)
328 throws ServletException
, IOException
{
330 // Subject subject = subjectFromRequest(request, response);
331 // Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
333 // public Void run() throws Exception {
334 if (log
.isTraceEnabled())
335 HttpUtils
.logRequest(log
, request
);
336 RemotingServlet
.super.service(request
, response
);
340 // } catch (PrivilegedActionException e) {
341 // throw new CmsException("Cannot process JCR remoting request",
342 // e.getException());