From 115f647f7a71352e174e650d30f190ef43f1b24f Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 12 Oct 2014 22:17:59 -0700 Subject: [PATCH] GUAC-878: Prefer WebSocket from JSR. Warn if HTTP used instead. --- .../basic/BasicGuacamoleTunnelServlet.java | 4 ++ .../websocket/WebSocketSupportLoader.java | 43 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/BasicGuacamoleTunnelServlet.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/BasicGuacamoleTunnelServlet.java index f37c76c05..8c77e92cd 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/BasicGuacamoleTunnelServlet.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/BasicGuacamoleTunnelServlet.java @@ -44,6 +44,10 @@ public class BasicGuacamoleTunnelServlet extends GuacamoleHTTPTunnelServlet { @Override protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException { + + // Warn of lack of WebSocket + logger.warn("Using HTTP tunnel (not WebSocket). Performance may be sub-optimal."); + return BasicTunnelRequestUtility.createTunnel(new HTTPTunnelRequest(request)); } diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/websocket/WebSocketSupportLoader.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/websocket/WebSocketSupportLoader.java index e233d60e1..c6f2bc3d7 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/websocket/WebSocketSupportLoader.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/websocket/WebSocketSupportLoader.java @@ -60,6 +60,39 @@ public class WebSocketSupportLoader implements ServletContextListener { "org.glyptodon.guacamole.net.basic.websocket.tomcat.BasicGuacamoleWebSocketTunnelServlet" }; + /** + * Checks for JSR 356 support, returning true if such support is found, and + * false otherwise. + * + * @return true if support for JSR 356 is found, false otherwise. + */ + private boolean implementsJSR_356() { + + try { + + // Attempt to find WebSocket servlet + GuacamoleClassLoader.getInstance().findClass("javax.websocket.Endpoint"); + + // JSR 356 found + return true; + + } + + // If no such servlet class, this particular WebSocket support + // is not present + catch (ClassNotFoundException e) {} + catch (NoClassDefFoundError e) {} + + // Log all GuacamoleExceptions + catch (GuacamoleException e) { + logger.error("Unable to load/detect WebSocket support.", e); + } + + // JSR 356 not found + return false; + + } + private boolean loadWebSocketTunnel(ServletContext context, String classname) { try { @@ -129,6 +162,12 @@ public class WebSocketSupportLoader implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { + // Check for JSR 356 support + if (implementsJSR_356()) { + logger.info("JSR-356 WebSocket support present."); + return; + } + // Try to load each WebSocket tunnel in sequence for (String classname : WEBSOCKET_CLASSES) { if (loadWebSocketTunnel(sce.getServletContext(), classname)) { @@ -137,8 +176,8 @@ public class WebSocketSupportLoader implements ServletContextListener { } } - // No legacy WebSocket support found (usually good) - logger.debug("Legacy WebSocket support NOT loaded."); + // Warn of lack of WebSocket + logger.debug("WebSocket support NOT present. Only HTTP will be used."); }