Make data admin log-in more robust and easier to use.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / kernel / NodeHttp.java
index 9d7deefb57200fd29b86d43bce0c4e3ff42138f5..e9219849523eaf0c5bb23cbf21bdd3ad76239ddc 100644 (file)
@@ -44,9 +44,12 @@ public class NodeHttp implements KernelConstants {
        private ServiceTracker<Repository, Repository> repositories;
        private final ServiceTracker<HttpService, HttpService> httpServiceTracker;
 
-       private static String httpRealm = "Argeo";
+       private String httpRealm = "Argeo";
+       private String webDavConfig = HttpUtils.WEBDAV_CONFIG;
+       private final boolean cleanState;
 
-       public NodeHttp() {
+       public NodeHttp(boolean cleanState) {
+               this.cleanState = cleanState;
                httpServiceTracker = new PrepareHttpStc();
                // httpServiceTracker.open();
                KernelUtils.asyncOpen(httpServiceTracker);
@@ -57,7 +60,7 @@ public class NodeHttp implements KernelConstants {
                        repositories.close();
        }
 
-       public static void registerRepositoryServlets(HttpService httpService, String alias, Repository repository) {
+       public void registerRepositoryServlets(HttpService httpService, String alias, Repository repository) {
                if (httpService == null)
                        throw new CmsException("No HTTP service available");
                try {
@@ -65,8 +68,8 @@ public class NodeHttp implements KernelConstants {
                        registerRemotingServlet(httpService, alias, repository);
                        if (NodeConstants.HOME.equals(alias))
                                registerFilesServlet(httpService, alias, repository);
-                       if (log.isDebugEnabled())
-                               log.debug("Registered servlets for repository '" + alias + "'");
+                       if (log.isTraceEnabled())
+                               log.trace("Registered servlets for repository '" + alias + "'");
                } catch (Exception e) {
                        throw new CmsException("Could not register servlets for repository '" + alias + "'", e);
                }
@@ -80,36 +83,36 @@ public class NodeHttp implements KernelConstants {
                        httpService.unregister(remotingPath(alias));
                        if (NodeConstants.HOME.equals(alias))
                                httpService.unregister(filesPath(alias));
-                       if (log.isDebugEnabled())
-                               log.debug("Unregistered servlets for repository '" + alias + "'");
+                       if (log.isTraceEnabled())
+                               log.trace("Unregistered servlets for repository '" + alias + "'");
                } catch (Exception e) {
                        log.error("Could not unregister servlets for repository '" + alias + "'", e);
                }
        }
 
-       static void registerWebdavServlet(HttpService httpService, String alias, Repository repository)
+       void registerWebdavServlet(HttpService httpService, String alias, Repository repository)
                        throws NamespaceException, ServletException {
                // WebdavServlet webdavServlet = new WebdavServlet(repository, new
                // OpenInViewSessionProvider(alias));
                WebdavServlet webdavServlet = new WebdavServlet(repository, new CmsSessionProvider(alias));
                String path = webdavPath(alias);
                Properties ip = new Properties();
-               ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_CONFIG, HttpUtils.WEBDAV_CONFIG);
+               ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_CONFIG, webDavConfig);
                ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, path);
                httpService.registerServlet(path, webdavServlet, ip, new DataHttpContext(httpRealm));
        }
 
-       static void registerFilesServlet(HttpService httpService, String alias, Repository repository)
+       void registerFilesServlet(HttpService httpService, String alias, Repository repository)
                        throws NamespaceException, ServletException {
                WebdavServlet filesServlet = new WebdavServlet(repository, new CmsSessionProvider(alias));
                String path = filesPath(alias);
                Properties ip = new Properties();
-               ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_CONFIG, HttpUtils.WEBDAV_CONFIG);
+               ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_CONFIG, webDavConfig);
                ip.setProperty(WebdavServlet.INIT_PARAM_RESOURCE_PATH_PREFIX, path);
                httpService.registerServlet(path, filesServlet, ip, new PrivateHttpContext(httpRealm, true));
        }
 
-       static void registerRemotingServlet(HttpService httpService, String alias, Repository repository)
+       void registerRemotingServlet(HttpService httpService, String alias, Repository repository)
                        throws NamespaceException, ServletException {
                RemotingServlet remotingServlet = new RemotingServlet(repository, new CmsSessionProvider(alias));
                String path = remotingPath(alias);
@@ -143,23 +146,7 @@ public class NodeHttp implements KernelConstants {
                return NodeConstants.PATH_FILES;
        }
 
-       // private Subject subjectFromRequest(HttpServletRequest request,
-       // HttpServletResponse response) {
-       // Authorization authorization = (Authorization)
-       // request.getAttribute(HttpContext.AUTHORIZATION);
-       // if (authorization == null)
-       // throw new CmsException("Not authenticated");
-       // try {
-       // LoginContext lc = new LoginContext(NodeConstants.LOGIN_CONTEXT_USER,
-       // new HttpRequestCallbackHandler(request, response));
-       // lc.login();
-       // return lc.getSubject();
-       // } catch (LoginException e) {
-       // throw new CmsException("Cannot login", e);
-       // }
-       // }
-
-       static class RepositoriesStc extends ServiceTracker<Repository, Repository> {
+       class RepositoriesStc extends ServiceTracker<Repository, Repository> {
                private final HttpService httpService;
 
                private final BundleContext bc;
@@ -196,9 +183,6 @@ public class NodeHttp implements KernelConstants {
        }
 
        private class PrepareHttpStc extends ServiceTracker<HttpService, HttpService> {
-               // private DataHttp dataHttp;
-               // private NodeHttp nodeHttp;
-
                public PrepareHttpStc() {
                        super(bc, HttpService.class, null);
                }
@@ -216,13 +200,6 @@ public class NodeHttp implements KernelConstants {
 
                @Override
                public void removedService(ServiceReference<HttpService> reference, HttpService service) {
-                       // if (dataHttp != null)
-                       // dataHttp.destroy();
-                       // dataHttp = null;
-                       // if (nodeHttp != null)
-                       // nodeHttp.destroy();
-                       // nodeHttp = null;
-                       // destroy();
                        repositories.close();
                        repositories = null;
                }
@@ -236,6 +213,7 @@ public class NodeHttp implements KernelConstants {
                        try {
                                httpService.registerServlet("/!", new LinkServlet(), null, null);
                                httpService.registerServlet("/robots.txt", new RobotServlet(), null, null);
+                               // httpService.registerServlet("/html", new HtmlServlet(), null, null);
                        } catch (Exception e) {
                                throw new CmsException("Cannot register filters", e);
                        }
@@ -244,7 +222,8 @@ public class NodeHttp implements KernelConstants {
                                throw new CmsException("An http service is already configured");
                        repositories = new RepositoriesStc(bc, httpService);
                        // repositories.open();
-                       KernelUtils.asyncOpen(repositories);
+                       if (cleanState)
+                               KernelUtils.asyncOpen(repositories);
                        log.info(httpPortsMsg(httpPort, httpsPort));
                        // httpAvailable = true;
                        // checkReadiness();
@@ -323,21 +302,9 @@ public class NodeHttp implements KernelConstants {
                @Override
                protected void service(final HttpServletRequest request, final HttpServletResponse response)
                                throws ServletException, IOException {
-                       // try {
-                       // Subject subject = subjectFromRequest(request, response);
-                       // Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
-                       // @Override
-                       // public Void run() throws Exception {
                        if (log.isTraceEnabled())
                                HttpUtils.logRequest(log, request);
                        RemotingServlet.super.service(request, response);
-                       // return null;
-                       // }
-                       // });
-                       // } catch (PrivilegedActionException e) {
-                       // throw new CmsException("Cannot process JCR remoting request",
-                       // e.getException());
-                       // }
                }
        }