X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.lib.jetty%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjetty%2FJettyHttpServer.java;h=027ef9f3dcba0b54dc1a1371e126f9f45e566642;hb=2ddf8219c721df80456d99848cc25ce69238297d;hp=ea9705720f82cccff5993b32f592f50986b88aa9;hpb=81d9084e2c9fd9d33ca1d864171d28f9564647d8;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/JettyHttpServer.java b/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/JettyHttpServer.java index ea9705720..027ef9f3d 100644 --- a/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/JettyHttpServer.java +++ b/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/JettyHttpServer.java @@ -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; @@ -31,6 +32,7 @@ import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsServer; +/** An {@link HttpServer} implementation based on Jetty. */ public class JettyHttpServer extends HttpsServer { private final static CmsLog log = CmsLog.getLog(JettyHttpServer.class); @@ -41,7 +43,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 +56,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(); @@ -91,15 +96,9 @@ public class JettyHttpServer extends HttpsServer { // httpContext.addServlet(holder, "/*"); if (rootContextHandler != null) configureRootContextHandler(rootContextHandler); -// server.setHandler(rootContextHandler); - 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); server.setHandler(contextHandlerCollection); @@ -108,11 +107,23 @@ 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()); + } else 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()); started = true; } catch (Exception e) { + stop(); throw new IllegalStateException("Cannot start Jetty HTTPS server", e); } } @@ -126,12 +137,11 @@ public class JettyHttpServer extends HttpsServer { public void stop() { try { - // serverConnector.close(); server.stop(); // TODO delete temp dir started = false; } catch (Exception e) { - e.printStackTrace(); + log.error("Cannot stop Jetty HTTP server", e); } } @@ -182,7 +192,10 @@ public class JettyHttpServer extends HttpsServer { @Override public InetSocketAddress getAddress() { - return address; + InetSocketAddress res = httpAddress != null ? httpAddress : httpsAddress; + if (res == null) + throw new IllegalStateException("Neither an HTTP nor and HTTPS address is available"); + return res; } @Override @@ -195,25 +208,27 @@ public class JettyHttpServer extends HttpsServer { return httpsConfigurator; } - - protected void configureConnectors() { HttpConfiguration httpConfiguration = new HttpConfiguration(); String httpPortStr = getDeployProperty(CmsDeployProperty.HTTP_PORT); String httpsPortStr = getDeployProperty(CmsDeployProperty.HTTPS_PORT); + if (httpPortStr != null && httpsPortStr != null) + throw new IllegalArgumentException("Either an HTTP or an HTTPS should be configured, not both"); + if (httpPortStr == null && httpsPortStr == null) + throw new IllegalArgumentException("Neither an HTTP or HTTPS port was configured"); /// TODO make it more generic String httpHost = getDeployProperty(CmsDeployProperty.HOST); -// String httpsHost = getFrameworkProp( -// JettyConfig.JETTY_PROPERTY_PREFIX + CmsHttpConstants.HTTPS_HOST); // 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; - // props.put(JettyHttpConstants.HTTPS_ENABLED, httpsEnabled); + if (httpsEnabled) { int httpsPort = Integer.parseInt(httpsPortStr); httpConfiguration.setSecureScheme("https"); @@ -226,6 +241,7 @@ public class JettyHttpServer extends HttpsServer { httpConnector.setPort(httpPort); httpConnector.setHost(httpHost); httpConnector.setIdleTimeout(DEFAULT_IDLE_TIMEOUT); + } if (httpsEnabled) { @@ -268,7 +284,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 +314,10 @@ public class JettyHttpServer extends HttpsServer { } - + public void setCmsState(CmsState cmsState) { + this.cmsState = cmsState; + } + public boolean isStarted() { return started; }