From 2ddf8219c721df80456d99848cc25ce69238297d Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 31 Oct 2022 06:32:07 +0100 Subject: [PATCH] Clean up Jetty HTTP server --- .../org/argeo/cms/jetty/CmsJettyServer.java | 54 +------------------ .../org/argeo/cms/jetty/JettyHttpServer.java | 31 ++++++----- 2 files changed, 16 insertions(+), 69 deletions(-) diff --git a/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/CmsJettyServer.java b/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/CmsJettyServer.java index 3d4a57b9e..90a800f7e 100644 --- a/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/CmsJettyServer.java +++ b/org.argeo.cms.lib.jetty/src/org/argeo/cms/jetty/CmsJettyServer.java @@ -9,25 +9,13 @@ import javax.servlet.ServletException; import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.servlet.ServletContextHandler; +/** A {@link JettyHttpServer} which is compatible with Equinox servlets. */ public class CmsJettyServer extends JettyHttpServer { private static final String CONTEXT_TEMPDIR = "javax.servlet.context.tempdir"; // Equinox compatibility private static final String INTERNAL_CONTEXT_CLASSLOADER = "org.eclipse.equinox.http.jetty.internal.ContextClassLoader"; -// private static final CmsLog log = CmsLog.getLog(CmsJettyServer.class); - -// private Server server; -// private Path tempDir; -// -// private ServerConnector httpConnector; -// private ServerConnector httpsConnector; private Path tempDir; - // WebSocket -// private ServerContainer wsServerContainer; -// private ServerEndpointConfig.Configurator wsEndpointConfigurator; - -// private Authenticator defaultAuthenticator; - protected void addServlets(ServletContextHandler servletContextHandler) throws ServletException { } @@ -59,45 +47,5 @@ public class CmsJettyServer extends JettyHttpServer { @Override protected void configureRootContextHandler(ServletContextHandler servletContextHandler) throws ServletException { addServlets(servletContextHandler); -// enableWebSocket(servletContextHandler); - } - -// @Override -// public synchronized HttpContext createContext(String path) { -// HttpContext httpContext = super.createContext(path); -// httpContext.setAuthenticator(defaultAuthenticator); -// return httpContext; -// } - -// protected void enableWebSocket(ServletContextHandler servletContextHandler) { -// String webSocketEnabled = getDeployProperty(CmsDeployProperty.WEBSOCKET_ENABLED); -// // web socket -// if (webSocketEnabled != null && webSocketEnabled.equals(Boolean.toString(true))) { -//// JavaxWebSocketServletContainerInitializer.configure(servletContextHandler, new Configurator() { -//// -//// @Override -//// public void accept(ServletContext servletContext, ServerContainer serverContainer) -//// throws DeploymentException { -////// wsServerContainer = serverContainer; -//// -//// CmsWebSocketConfigurator wsEndpointConfigurator = new CmsWebSocketConfigurator(); -//// -//// ServerEndpointConfig config = ServerEndpointConfig.Builder -//// .create(TestEndpoint.class, "/ws/test/events/{topic}").configurator(wsEndpointConfigurator) -//// .build(); -//// try { -//// serverContainer.addEndpoint(config); -//// } catch (DeploymentException e) { -//// throw new IllegalStateException("Cannot initalise the WebSocket server runtime.", e); -//// } -//// } -//// }); -// } -// } - -// public void setDefaultAuthenticator(Authenticator defaultAuthenticator) { -// this.defaultAuthenticator = defaultAuthenticator; -// } - } 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 80cff8b01..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 @@ -32,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); @@ -95,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("/")) contextHandlerCollection.addHandler(rootContextHandler); -// for (String contextPath : contexts.keySet()) { -// JettyHttpContext ctx = contexts.get(contextPath); -// contextHandlers.addHandler(ctx.getContextHandler()); -// } server.setHandler(contextHandlerCollection); @@ -115,19 +110,20 @@ public class JettyHttpServer extends HttpsServer { // Addresses String httpHost = getDeployProperty(CmsDeployProperty.HOST); String fallBackHostname = cmsState != null ? cmsState.getHostname() : "::1"; - if (httpConnector != null) + if (httpConnector != null) { httpAddress = new InetSocketAddress(httpHost != null ? httpHost : fallBackHostname, httpConnector.getLocalPort()); - if (httpsConnector != null) + } 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); } } @@ -141,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); } } @@ -197,7 +192,10 @@ public class JettyHttpServer extends HttpsServer { @Override public InetSocketAddress getAddress() { - return httpAddress; + InetSocketAddress res = httpAddress != null ? httpAddress : httpsAddress; + if (res == null) + throw new IllegalStateException("Neither an HTTP nor and HTTPS address is available"); + return res; } @Override @@ -215,11 +213,13 @@ public class JettyHttpServer extends HttpsServer { 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) { @@ -227,9 +227,8 @@ public class JettyHttpServer extends HttpsServer { // 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"); -- 2.30.2