Make ACR content providers more configurable
[lgpl/argeo-commons.git] / org.argeo.cms.lib.jetty / src / org / argeo / cms / jetty / JettyHttpServer.java
index ea9705720f82cccff5993b32f592f50986b88aa9..80cff8b012bba5e5f55b56dd9a9fcd2e5924eac3 100644 (file)
@@ -10,6 +10,7 @@ import java.util.concurrent.ThreadPoolExecutor;
 import javax.servlet.ServletException;
 
 import org.argeo.api.cms.CmsLog;
+import org.argeo.api.cms.CmsState;
 import org.argeo.cms.CmsDeployProperty;
 import org.argeo.util.http.HttpServerUtils;
 import org.eclipse.jetty.http.UriCompliance;
@@ -41,7 +42,8 @@ public class JettyHttpServer extends HttpsServer {
        protected ServerConnector httpConnector;
        protected ServerConnector httpsConnector;
 
-       private InetSocketAddress address;
+       private InetSocketAddress httpAddress;
+       private InetSocketAddress httpsAddress;
 
        private ThreadPoolExecutor executor;
 
@@ -53,6 +55,8 @@ public class JettyHttpServer extends HttpsServer {
 
        private boolean started;
 
+       private CmsState cmsState;
+
        @Override
        public void bind(InetSocketAddress addr, int backlog) throws IOException {
                throw new UnsupportedOperationException();
@@ -93,13 +97,13 @@ public class JettyHttpServer extends HttpsServer {
                                configureRootContextHandler(rootContextHandler);
 //                     server.setHandler(rootContextHandler);
 
-                       ContextHandlerCollection contextHandlers = new ContextHandlerCollection();
+//                     ContextHandlerCollection contextHandlers = new ContextHandlerCollection();
                        if (rootContextHandler != null && !contexts.containsKey("/"))
-                               contextHandlers.addHandler(rootContextHandler);
-                       for (String contextPath : contexts.keySet()) {
-                               JettyHttpContext ctx = contexts.get(contextPath);
-                               contextHandlers.addHandler(ctx.getContextHandler());
-                       }
+                               contextHandlerCollection.addHandler(rootContextHandler);
+//                     for (String contextPath : contexts.keySet()) {
+//                             JettyHttpContext ctx = contexts.get(contextPath);
+//                             contextHandlers.addHandler(ctx.getContextHandler());
+//                     }
 
                        server.setHandler(contextHandlerCollection);
 
@@ -108,6 +112,17 @@ public class JettyHttpServer extends HttpsServer {
                        server.start();
                        //
 
+                       // Addresses
+                       String httpHost = getDeployProperty(CmsDeployProperty.HOST);
+                       String fallBackHostname = cmsState != null ? cmsState.getHostname() : "::1";
+                       if (httpConnector != null)
+                               httpAddress = new InetSocketAddress(httpHost != null ? httpHost : fallBackHostname,
+                                               httpConnector.getLocalPort());
+                       if (httpsConnector != null)
+                               httpsAddress = new InetSocketAddress(httpHost != null ? httpHost : fallBackHostname,
+                                               httpsConnector.getLocalPort());
+
+                       // Clean up
                        Runtime.getRuntime().addShutdownHook(new Thread(() -> stop(), "Jetty shutdown"));
 
                        log.info(httpPortsMsg());
@@ -182,7 +197,7 @@ public class JettyHttpServer extends HttpsServer {
 
        @Override
        public InetSocketAddress getAddress() {
-               return address;
+               return httpAddress;
        }
 
        @Override
@@ -195,8 +210,6 @@ public class JettyHttpServer extends HttpsServer {
                return httpsConfigurator;
        }
 
-       
-       
        protected void configureConnectors() {
                HttpConfiguration httpConfiguration = new HttpConfiguration();
 
@@ -210,6 +223,9 @@ public class JettyHttpServer extends HttpsServer {
 
                // try {
                if (httpPortStr != null || httpsPortStr != null) {
+                       // TODO deal with hostname resolving taking too much time
+//                     String fallBackHostname = InetAddress.getLocalHost().getHostName();
+
                        boolean httpEnabled = httpPortStr != null;
                        // props.put(JettyHttpConstants.HTTP_ENABLED, httpEnabled);
                        boolean httpsEnabled = httpsPortStr != null;
@@ -226,6 +242,7 @@ public class JettyHttpServer extends HttpsServer {
                                httpConnector.setPort(httpPort);
                                httpConnector.setHost(httpHost);
                                httpConnector.setIdleTimeout(DEFAULT_IDLE_TIMEOUT);
+
                        }
 
                        if (httpsEnabled) {
@@ -268,7 +285,8 @@ public class JettyHttpServer extends HttpsServer {
        }
 
        protected String getDeployProperty(CmsDeployProperty deployProperty) {
-               return System.getProperty(deployProperty.getProperty());
+               return cmsState != null ? cmsState.getDeployProperty(deployProperty.getProperty())
+                               : System.getProperty(deployProperty.getProperty());
        }
 
        private String httpPortsMsg() {
@@ -297,7 +315,10 @@ public class JettyHttpServer extends HttpsServer {
 
        }
 
-       
+       public void setCmsState(CmsState cmsState) {
+               this.cmsState = cmsState;
+       }
+
        public boolean isStarted() {
                return started;
        }