Make WebSocket activation configurable.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 8 Sep 2019 08:18:30 +0000 (10:18 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 8 Sep 2019 08:18:30 +0000 (10:18 +0200)
demo/cms-e4-rap.properties
org.argeo.cms/src/org/argeo/cms/internal/http/HttpConstants.java
org.argeo.cms/src/org/argeo/cms/internal/kernel/DeployConfig.java
org.argeo.cms/src/org/argeo/cms/internal/kernel/InitUtils.java
org.argeo.ext.equinox.jetty/src/org/argeo/equinox/jetty/CmsJettyCustomizer.java [new file with mode: 0644]
org.argeo.ext.equinox.jetty/src/org/argeo/equinox/jetty/WebSocketJettyCustomizer.java [deleted file]

index 50d8e2b8e4f54d2fbaa93c05d000d1b787bf76e6..bbfb30655dc7da4d7ea2e0e6022859c8ad402c11 100644 (file)
@@ -19,6 +19,7 @@ org.argeo.cms.e4.rap
 argeo.node.repo.type=h2
 org.osgi.service.http.port=7070
 #org.osgi.service.http.port.secure=7073
+#org.eclipse.equinox.http.jetty.websocket.enabled=true
 
 # Logging
 log4j.configuration=file:../../log4j.properties
index 5fe57b7c22b85d3bfc0ac1f33e8e56a41537b1e0..51760fcffd3b9988544160e945e675df0f01f439 100644 (file)
@@ -2,20 +2,22 @@ package org.argeo.cms.internal.http;
 
 /** Compatible with Jetty. */
 public interface HttpConstants {
-       public static final String HTTP_ENABLED = "http.enabled";
-       public static final String HTTP_PORT = "http.port";
-       public static final String HTTP_HOST = "http.host";
-       public static final String HTTPS_ENABLED = "https.enabled";
-       public static final String HTTPS_HOST = "https.host";
-       public static final String HTTPS_PORT = "https.port";
-       public static final String SSL_KEYSTORE = "ssl.keystore";
-       public static final String SSL_PASSWORD = "ssl.password";
-       public static final String SSL_KEYPASSWORD = "ssl.keypassword";
-       public static final String SSL_NEEDCLIENTAUTH = "ssl.needclientauth";
-       public static final String SSL_WANTCLIENTAUTH = "ssl.wantclientauth";
-       public static final String SSL_PROTOCOL = "ssl.protocol";
-       public static final String SSL_ALGORITHM = "ssl.algorithm";
-       public static final String SSL_KEYSTORETYPE = "ssl.keystoretype";
-       public static final String JETTY_PROPERTY_PREFIX = "org.eclipse.equinox.http.jetty.";
+       static final String HTTP_ENABLED = "http.enabled";
+       static final String HTTP_PORT = "http.port";
+       static final String HTTP_HOST = "http.host";
+       static final String HTTPS_ENABLED = "https.enabled";
+       static final String HTTPS_HOST = "https.host";
+       static final String HTTPS_PORT = "https.port";
+       static final String SSL_KEYSTORE = "ssl.keystore";
+       static final String SSL_PASSWORD = "ssl.password";
+       static final String SSL_KEYPASSWORD = "ssl.keypassword";
+       static final String SSL_NEEDCLIENTAUTH = "ssl.needclientauth";
+       static final String SSL_WANTCLIENTAUTH = "ssl.wantclientauth";
+       static final String SSL_PROTOCOL = "ssl.protocol";
+       static final String SSL_ALGORITHM = "ssl.algorithm";
+       static final String SSL_KEYSTORETYPE = "ssl.keystoretype";
+       static final String JETTY_PROPERTY_PREFIX = "org.eclipse.equinox.http.jetty.";
+       // Argeo specific
+       static final String WEB_SOCKET_ENABLED = "websocket.enabled";
 
 }
index 3cce7e8218968e8fa5fe754415fc34cf306c55c2..b1e36633a1da9cd704d0e787e4fa8673a40d5c9d 100644 (file)
@@ -123,7 +123,8 @@ class DeployConfig implements ConfigurationListener {
                Dictionary<String, Object> webServerConfig = InitUtils
                                .getHttpServerConfig(getProps(KernelConstants.JETTY_FACTORY_PID, NodeConstants.DEFAULT));
                if (!webServerConfig.isEmpty()) {
-                       webServerConfig.put("customizer.class", "org.argeo.equinox.jetty.WebSocketJettyCustomizer");
+                       // TODO chekc for other customizers
+                       webServerConfig.put("customizer.class", "org.argeo.equinox.jetty.CmsJettyCustomizer");
                        putFactoryDeployConfig(KernelConstants.JETTY_FACTORY_PID, webServerConfig);
                }
                save();
index a4892503d3b0c64e73363bf958105b063e042d2c..045130ecbc63da7501eca8df75416fca06029d86 100644 (file)
@@ -68,6 +68,8 @@ class InitUtils {
                /// TODO make it more generic
                String httpHost = getFrameworkProp(HttpConstants.JETTY_PROPERTY_PREFIX + HttpConstants.HTTP_HOST);
                String httpsHost = getFrameworkProp(HttpConstants.JETTY_PROPERTY_PREFIX + HttpConstants.HTTPS_HOST);
+               String webSocketEnabled = getFrameworkProp(
+                               HttpConstants.JETTY_PROPERTY_PREFIX + HttpConstants.WEB_SOCKET_ENABLED);
 
                final Hashtable<String, Object> props = new Hashtable<String, Object>();
                // try {
@@ -101,6 +103,10 @@ class InitUtils {
                        if (httpsHost != null)
                                props.put(HttpConstants.HTTPS_HOST, httpHost);
 
+                       if (webSocketEnabled != null)
+                               if (webSocketEnabled.equals("true"))
+                                       props.put(HttpConstants.WEB_SOCKET_ENABLED, true);
+
                        props.put(NodeConstants.CN, NodeConstants.DEFAULT);
                }
                return props;
diff --git a/org.argeo.ext.equinox.jetty/src/org/argeo/equinox/jetty/CmsJettyCustomizer.java b/org.argeo.ext.equinox.jetty/src/org/argeo/equinox/jetty/CmsJettyCustomizer.java
new file mode 100644 (file)
index 0000000..60d2371
--- /dev/null
@@ -0,0 +1,51 @@
+package org.argeo.equinox.jetty;
+
+import java.util.Dictionary;
+
+import javax.servlet.ServletException;
+
+import org.eclipse.equinox.http.jetty.JettyCustomizer;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.websocket.jsr356.server.ServerContainer;
+import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+
+/** Customises the Jetty HTTP server. */
+public class CmsJettyCustomizer extends JettyCustomizer {
+       private BundleContext bc = FrameworkUtil.getBundle(CmsJettyCustomizer.class).getBundleContext();
+
+       @Override
+       public Object customizeContext(Object context, Dictionary<String, ?> settings) {
+               // WebSocket
+               Object webSocketEnabled = settings.get("websocket.enabled");
+               if (webSocketEnabled != null && webSocketEnabled.toString().equals("true")) {
+                       ServletContextHandler servletContextHandler = (ServletContextHandler) context;
+                       new WebSocketInit(servletContextHandler).start();
+               }
+               return super.customizeContext(context, settings);
+
+       }
+
+       /** Configure websocket container asynchronously as it may take some time */
+       private class WebSocketInit extends Thread {
+               ServletContextHandler servletContextHandler;
+
+               public WebSocketInit(ServletContextHandler servletContextHandler) {
+                       super("WebSocket Init");
+                       this.servletContextHandler = servletContextHandler;
+               }
+
+               @Override
+               public void run() {
+                       ServerContainer serverContainer;
+                       try {
+                               serverContainer = WebSocketServerContainerInitializer.configureContext(servletContextHandler);
+                       } catch (ServletException e) {
+                               throw new IllegalStateException("Cannot configure web sockets", e);
+                       }
+                       bc.registerService(javax.websocket.server.ServerContainer.class, serverContainer, null);
+               }
+
+       }
+}
diff --git a/org.argeo.ext.equinox.jetty/src/org/argeo/equinox/jetty/WebSocketJettyCustomizer.java b/org.argeo.ext.equinox.jetty/src/org/argeo/equinox/jetty/WebSocketJettyCustomizer.java
deleted file mode 100644 (file)
index f582a43..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-package org.argeo.equinox.jetty;
-
-import java.util.Dictionary;
-
-import javax.servlet.ServletException;
-
-import org.eclipse.equinox.http.jetty.JettyCustomizer;
-import org.eclipse.jetty.servlet.ServletContextHandler;
-import org.eclipse.jetty.websocket.jsr356.server.ServerContainer;
-import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
-
-public class WebSocketJettyCustomizer extends JettyCustomizer {
-       private BundleContext bc = FrameworkUtil.getBundle(WebSocketJettyCustomizer.class).getBundleContext();
-
-       @Override
-       public Object customizeContext(Object context, Dictionary<String, ?> settings) {
-               ServletContextHandler servletContextHandler = (ServletContextHandler) context;
-               new WebSocketInit(servletContextHandler).start();
-               return super.customizeContext(context, settings);
-       }
-
-       /** Configure websocket container asynchronously as it may take some time */
-       private class WebSocketInit extends Thread {
-               ServletContextHandler servletContextHandler;
-
-               public WebSocketInit(ServletContextHandler servletContextHandler) {
-                       super("WebSocket Init");
-                       this.servletContextHandler = servletContextHandler;
-               }
-
-               @Override
-               public void run() {
-                       ServerContainer serverContainer;
-                       try {
-                               serverContainer = WebSocketServerContainerInitializer.configureContext(servletContextHandler);
-//                             serverContainer.addSessionListener(new WebSocketSessionListener() {
-//
-//                                     @Override
-//                                     public void onSessionOpened(WebSocketSession session) {
-//                                             UpgradeRequest upgradeRequest = session.getUpgradeRequest();
-//                                             UpgradeResponse upgradeResponse = session.getUpgradeResponse();
-//                                             List<String> acceptHeader = upgradeResponse.getHeaders("Sec-WebSocket-Accept");
-//                                             if (acceptHeader.contains("no"))
-//                                                     try {
-//                                                             upgradeResponse.sendForbidden("FORBIDDEN");
-//                                                             return;
-//                                                     } catch (IOException e) {
-//                                                             // TODO Auto-generated catch block
-//                                                             e.printStackTrace();
-//                                                     }
-//                                             List<HttpCookie> cookies = upgradeRequest.getCookies();
-//
-//                                             System.out.println("Upgrade request cookies : " + cookies);
-//                                             String httpSessionId = null;
-//                                             if (cookies != null) {
-//                                                     for (HttpCookie cookie : cookies) {
-//                                                             if (cookie.getName().equals("JSESSIONID")) {
-//                                                                     httpSessionId = cookie.getValue();
-//                                                             }
-//                                                     }
-//                                             }
-//
-//                                             if (httpSessionId == null) {
-//                                                     HttpSession httpSession = (HttpSession) upgradeRequest.getSession();
-//                                                     if (httpSession == null) {
-////                                                   session.disconnect();
-////                                                   return;
-//                                                     } else {
-//                                                             httpSessionId = httpSession.getId();
-//                                                             System.out.println("Upgrade request session ID : " + httpSession.getId());
-//                                                     }
-//                                             }
-//
-//                                             if (httpSessionId != null) {
-//                                                     int dotIdx = httpSessionId.lastIndexOf('.');
-//                                                     if (dotIdx > 0) {
-//                                                             httpSessionId = httpSessionId.substring(0, dotIdx);
-//                                                     }
-//                                             }
-//                                     }
-//
-//                                     @Override
-//                                     public void onSessionClosed(WebSocketSession session) {
-//                                     }
-//                             });
-                       } catch (ServletException e) {
-                               throw new IllegalStateException("Cannot configure web sockets", e);
-                       }
-                       bc.registerService(javax.websocket.server.ServerContainer.class, serverContainer, null);
-               }
-
-       }
-}