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 String httpRealm
= "Argeo";
48 private String webDavConfig
= HttpUtils
.WEBDAV_CONFIG
;
49 private final boolean cleanState
;
51 public NodeHttp(boolean cleanState
) {
52 this.cleanState
= cleanState
;
53 httpServiceTracker
= new PrepareHttpStc();
54 // httpServiceTracker.open();
55 KernelUtils
.asyncOpen(httpServiceTracker
);
58 public void destroy() {
59 if (repositories
!= null)
63 public void registerRepositoryServlets(HttpService httpService
, String alias
, Repository repository
) {
64 if (httpService
== null)
65 throw new CmsException("No HTTP service available");
67 registerWebdavServlet(httpService
, alias
, repository
);
68 registerRemotingServlet(httpService
, alias
, repository
);
69 if (NodeConstants
.HOME
.equals(alias
))
70 registerFilesServlet(httpService
, alias
, repository
);
71 if (log
.isTraceEnabled())
72 log
.trace("Registered servlets for repository '" + alias
+ "'");
73 } catch (Exception e
) {
74 throw new CmsException("Could not register servlets for repository '" + alias
+ "'", e
);
78 public static void unregisterRepositoryServlets(HttpService httpService
, String alias
) {
79 if (httpService
== null)
82 httpService
.unregister(webdavPath(alias
));
83 httpService
.unregister(remotingPath(alias
));
84 if (NodeConstants
.HOME
.equals(alias
))
85 httpService
.unregister(filesPath(alias
));
86 if (log
.isTraceEnabled())
87 log
.trace("Unregistered servlets for repository '" + alias
+ "'");
88 } catch (Exception e
) {
89 log
.error("Could not unregister servlets for repository '" + alias
+ "'", e
);
93 void registerWebdavServlet(HttpService httpService
, String alias
, Repository repository
)
94 throws NamespaceException
, ServletException
{
95 // WebdavServlet webdavServlet = new WebdavServlet(repository, new
96 // OpenInViewSessionProvider(alias));
97 WebdavServlet webdavServlet
= new WebdavServlet(repository
, new CmsSessionProvider(alias
));
98 String path
= webdavPath(alias
);
99 Properties ip
= new Properties();
100 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_CONFIG
, webDavConfig
);
101 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_PATH_PREFIX
, path
);
102 httpService
.registerServlet(path
, webdavServlet
, ip
, new DataHttpContext(httpRealm
));
105 void registerFilesServlet(HttpService httpService
, String alias
, Repository repository
)
106 throws NamespaceException
, ServletException
{
107 WebdavServlet filesServlet
= new WebdavServlet(repository
, new CmsSessionProvider(alias
));
108 String path
= filesPath(alias
);
109 Properties ip
= new Properties();
110 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_CONFIG
, webDavConfig
);
111 ip
.setProperty(WebdavServlet
.INIT_PARAM_RESOURCE_PATH_PREFIX
, path
);
112 httpService
.registerServlet(path
, filesServlet
, ip
, new PrivateHttpContext(httpRealm
, true));
115 void registerRemotingServlet(HttpService httpService
, String alias
, Repository repository
)
116 throws NamespaceException
, ServletException
{
117 RemotingServlet remotingServlet
= new RemotingServlet(repository
, new CmsSessionProvider(alias
));
118 String path
= remotingPath(alias
);
119 Properties ip
= new Properties();
120 ip
.setProperty(JcrRemotingServlet
.INIT_PARAM_RESOURCE_PATH_PREFIX
, path
);
121 ip
.setProperty(JcrRemotingServlet
.INIT_PARAM_AUTHENTICATE_HEADER
, "Negotiate");
123 // Looks like a bug in Jackrabbit remoting init
126 tmpDir
= Files
.createTempDirectory("remoting_" + alias
);
127 } catch (IOException e
) {
128 throw new CmsException("Cannot create temp directory for remoting servlet", e
);
130 ip
.setProperty(RemotingServlet
.INIT_PARAM_HOME
, tmpDir
.toString());
131 ip
.setProperty(RemotingServlet
.INIT_PARAM_TMP_DIRECTORY
, "remoting_" + alias
);
132 ip
.setProperty(RemotingServlet
.INIT_PARAM_PROTECTED_HANDLERS_CONFIG
, HttpUtils
.DEFAULT_PROTECTED_HANDLERS
);
133 ip
.setProperty(RemotingServlet
.INIT_PARAM_CREATE_ABSOLUTE_URI
, "false");
134 httpService
.registerServlet(path
, remotingServlet
, ip
, new PrivateHttpContext(httpRealm
));
137 static String
webdavPath(String alias
) {
138 return NodeConstants
.PATH_DATA
+ "/" + alias
;
141 static String
remotingPath(String alias
) {
142 return NodeConstants
.PATH_JCR
+ "/" + alias
;
145 static String
filesPath(String alias
) {
146 return NodeConstants
.PATH_FILES
;
149 class RepositoriesStc
extends ServiceTracker
<Repository
, Repository
> {
150 private final HttpService httpService
;
152 private final BundleContext bc
;
154 public RepositoriesStc(BundleContext bc
, HttpService httpService
) {
155 super(bc
, Repository
.class, null);
156 this.httpService
= httpService
;
161 public Repository
addingService(ServiceReference
<Repository
> reference
) {
162 Repository repository
= bc
.getService(reference
);
163 Object jcrRepoAlias
= reference
.getProperty(NodeConstants
.CN
);
164 if (jcrRepoAlias
!= null) {
165 String alias
= jcrRepoAlias
.toString();
166 registerRepositoryServlets(httpService
, alias
, repository
);
172 public void modifiedService(ServiceReference
<Repository
> reference
, Repository service
) {
176 public void removedService(ServiceReference
<Repository
> reference
, Repository service
) {
177 Object jcrRepoAlias
= reference
.getProperty(NodeConstants
.CN
);
178 if (jcrRepoAlias
!= null) {
179 String alias
= jcrRepoAlias
.toString();
180 unregisterRepositoryServlets(httpService
, alias
);
185 private class PrepareHttpStc
extends ServiceTracker
<HttpService
, HttpService
> {
186 public PrepareHttpStc() {
187 super(bc
, HttpService
.class, null);
191 public HttpService
addingService(ServiceReference
<HttpService
> reference
) {
192 long begin
= System
.currentTimeMillis();
193 if (log
.isTraceEnabled())
194 log
.trace("HTTP prepare starts...");
195 HttpService httpService
= addHttpService(reference
);
196 if (log
.isTraceEnabled())
197 log
.trace("HTTP prepare duration: " + (System
.currentTimeMillis() - begin
) + "ms");
202 public void removedService(ServiceReference
<HttpService
> reference
, HttpService service
) {
203 repositories
.close();
207 private HttpService
addHttpService(ServiceReference
<HttpService
> sr
) {
208 HttpService httpService
= bc
.getService(sr
);
209 // TODO find constants
210 Object httpPort
= sr
.getProperty("http.port");
211 Object httpsPort
= sr
.getProperty("https.port");
214 httpService
.registerServlet("/!", new LinkServlet(), null, null);
215 httpService
.registerServlet("/robots.txt", new RobotServlet(), null, null);
216 // httpService.registerServlet("/html", new HtmlServlet(), null, null);
217 } catch (Exception e
) {
218 throw new CmsException("Cannot register filters", e
);
220 // track repositories
221 if (repositories
!= null)
222 throw new CmsException("An http service is already configured");
223 repositories
= new RepositoriesStc(bc
, httpService
);
224 // repositories.open();
226 KernelUtils
.asyncOpen(repositories
);
227 log
.info(httpPortsMsg(httpPort
, httpsPort
));
228 // httpAvailable = true;
231 bc
.registerService(NodeHttp
.class, NodeHttp
.this, null);
235 private String
httpPortsMsg(Object httpPort
, Object httpsPort
) {
236 return "HTTP " + httpPort
+ (httpsPort
!= null ?
" - HTTPS " + httpsPort
: "");
240 private static class WebdavServlet
extends SimpleWebdavServlet
{
241 private static final long serialVersionUID
= -4687354117811443881L;
242 private final Repository repository
;
244 public WebdavServlet(Repository repository
, SessionProvider sessionProvider
) {
245 this.repository
= repository
;
246 setSessionProvider(sessionProvider
);
249 public Repository
getRepository() {
254 protected void service(final HttpServletRequest request
, final HttpServletResponse response
)
255 throws ServletException
, IOException
{
256 WebdavServlet
.super.service(request
, response
);
258 // Subject subject = subjectFromRequest(request);
259 // // TODO make it stronger, with eTags.
260 // // if (CurrentUser.isAnonymous(subject) &&
261 // // request.getMethod().equals("GET")) {
262 // // response.setHeader("Cache-Control", "no-transform, public,
263 // // max-age=300, s-maxage=900");
266 // Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
268 // public Void run() throws Exception {
269 // WebdavServlet.super.service(request, response);
273 // } catch (PrivilegedActionException e) {
274 // throw new CmsException("Cannot process webdav request",
275 // e.getException());
281 private static class RemotingServlet
extends JcrRemotingServlet
{
282 private final Log log
= LogFactory
.getLog(RemotingServlet
.class);
283 private static final long serialVersionUID
= 4605238259548058883L;
284 private final Repository repository
;
285 private final SessionProvider sessionProvider
;
287 public RemotingServlet(Repository repository
, SessionProvider sessionProvider
) {
288 this.repository
= repository
;
289 this.sessionProvider
= sessionProvider
;
293 protected Repository
getRepository() {
298 protected SessionProvider
getSessionProvider() {
299 return sessionProvider
;
303 protected void service(final HttpServletRequest request
, final HttpServletResponse response
)
304 throws ServletException
, IOException
{
305 if (log
.isTraceEnabled())
306 HttpUtils
.logRequest(log
, request
);
307 RemotingServlet
.super.service(request
, response
);