X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.lib.jetty%2Fsrc%2Forg%2Fargeo%2Fcms%2Fjetty%2FJettyHttpServer.java;h=98975c3c843ad7c152a4c1815f738a812c6ca130;hb=4086635cfaa04c8a184124048794398b0ba96a55;hp=4141cd8ccdb8698690b604f2318ab8dfb7e09031;hpb=54df376a9c2dd458a82eaa09bfbb718fe699dd0d;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 4141cd8cc..98975c3c8 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 @@ -2,18 +2,20 @@ package org.argeo.cms.jetty; import java.io.IOException; import java.net.InetSocketAddress; +import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.TreeMap; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; +import javax.net.ssl.SSLContext; import javax.servlet.ServletException; import javax.websocket.server.ServerContainer; import org.argeo.api.cms.CmsLog; import org.argeo.api.cms.CmsState; import org.argeo.cms.CmsDeployProperty; -import org.argeo.cms.http.HttpServerUtils; +import org.argeo.cms.http.server.HttpServerUtils; import org.eclipse.jetty.http.UriCompliance; import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConnectionFactory; @@ -30,6 +32,7 @@ import org.eclipse.jetty.util.thread.ThreadPool; import com.sun.net.httpserver.HttpContext; import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsServer; @@ -37,7 +40,8 @@ import com.sun.net.httpserver.HttpsServer; public class JettyHttpServer extends HttpsServer { private final static CmsLog log = CmsLog.getLog(JettyHttpServer.class); - private static final int DEFAULT_IDLE_TIMEOUT = 30000; + /** Long timeout since our users may have poor connections. */ + private static final int DEFAULT_IDLE_TIMEOUT = 120 * 1000; private Server server; @@ -67,6 +71,17 @@ public class JettyHttpServer extends HttpsServer { @Override public void start() { + 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 port should be configured, not both"); + if (httpPortStr == null && httpsPortStr == null) { + log.warn("Neither an HTTP or an HTTPS port was configured, not starting Jetty"); + } + + /// TODO make it more generic + String httpHost = getDeployProperty(CmsDeployProperty.HOST); + try { ThreadPool threadPool = null; @@ -79,7 +94,7 @@ public class JettyHttpServer extends HttpsServer { server = new Server(threadPool); - configureConnectors(); + configureConnectors(httpPortStr, httpsPortStr, httpHost); if (httpConnector != null) { httpConnector.open(); @@ -110,7 +125,6 @@ public class JettyHttpServer extends HttpsServer { // // Addresses - String httpHost = getDeployProperty(CmsDeployProperty.HOST); String fallBackHostname = cmsState != null ? cmsState.getHostname() : "::1"; if (httpConnector != null) { httpAddress = new InetSocketAddress(httpHost != null ? httpHost : fallBackHostname, @@ -130,18 +144,7 @@ public class JettyHttpServer extends HttpsServer { } } - 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 port 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); + protected void configureConnectors(String httpPortStr, String httpsPortStr, String httpHost) { // try { if (httpPortStr != null || httpsPortStr != null) { @@ -151,13 +154,15 @@ public class JettyHttpServer extends HttpsServer { boolean httpEnabled = httpPortStr != null; boolean httpsEnabled = httpsPortStr != null; - if (httpsEnabled) { - int httpsPort = Integer.parseInt(httpsPortStr); - httpConfiguration.setSecureScheme("https"); - httpConfiguration.setSecurePort(httpsPort); - } - if (httpEnabled) { + HttpConfiguration httpConfiguration = new HttpConfiguration(); + + if (httpsEnabled) {// not supported anymore to have both http and https, but it may change again + int httpsPort = Integer.parseInt(httpsPortStr); + httpConfiguration.setSecureScheme("https"); + httpConfiguration.setSecurePort(httpsPort); + } + int httpPort = Integer.parseInt(httpPortStr); httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfiguration)); httpConnector.setPort(httpPort); @@ -167,6 +172,16 @@ public class JettyHttpServer extends HttpsServer { } if (httpsEnabled) { + if (httpsConfigurator == null) { + // we make sure that an HttpSConfigurator is set, so that clients can detect + // whether this server is HTTP or HTTPS + try { + httpsConfigurator = new HttpsConfigurator(SSLContext.getDefault()); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("Cannot initalise SSL Context", e); + } + } + SslContextFactory.Server sslContextFactory = new SslContextFactory.Server(); // sslContextFactory.setKeyStore(KeyS) @@ -188,16 +203,17 @@ public class JettyHttpServer extends HttpsServer { sslContextFactory.setNeedClientAuth(true); // HTTPS Configuration - HttpConfiguration https_config = new HttpConfiguration(httpConfiguration); - https_config.addCustomizer(new SecureRequestCustomizer()); - https_config.setUriCompliance(UriCompliance.LEGACY); + HttpConfiguration httpsConfiguration = new HttpConfiguration(); + httpsConfiguration.addCustomizer(new SecureRequestCustomizer()); + httpsConfiguration.setUriCompliance(UriCompliance.LEGACY); // HTTPS connector httpsConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), - new HttpConnectionFactory(https_config)); + new HttpConnectionFactory(httpsConfiguration)); int httpsPort = Integer.parseInt(httpsPortStr); httpsConnector.setPort(httpsPort); httpsConnector.setHost(httpHost); + httpsConnector.setIdleTimeout(DEFAULT_IDLE_TIMEOUT); } } } @@ -255,12 +271,17 @@ public class JettyHttpServer extends HttpsServer { @Override public synchronized void removeContext(String path) throws IllegalArgumentException { + if (!path.endsWith("/")) + path = path + "/"; if (!contexts.containsKey(path)) throw new IllegalArgumentException("Context " + path + " does not exist"); JettyHttpContext httpContext = contexts.remove(path); if (httpContext instanceof ContextHandlerHttpContext contextHandlerHttpContext) { // TODO stop handler first? contextHandlerCollection.removeHandler(contextHandlerHttpContext.getServletContextHandler()); + } else { + // FIXME apparently servlets cannot be removed in Jetty, we should replace the + // handler } }