From ebb75a3494da00839db14fd13301448cd469d093 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Wed, 25 Jan 2012 18:34:56 -0800 Subject: [PATCH] Hackish support for loadable websocket servlets. --- .../net/basic/WebSocketSupportLoader.java | 98 +++++++++++++++++++ guacamole/src/main/webapp/WEB-INF/web.xml | 5 + 2 files changed, 103 insertions(+) create mode 100644 guacamole/src/main/java/net/sourceforge/guacamole/net/basic/WebSocketSupportLoader.java diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/WebSocketSupportLoader.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/WebSocketSupportLoader.java new file mode 100644 index 000000000..b9670947a --- /dev/null +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/WebSocketSupportLoader.java @@ -0,0 +1,98 @@ +package net.sourceforge.guacamole.net.basic; + +/* + * Guacamole - Clientless Remote Desktop + * Copyright (C) 2010 Michael Jumper + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import javax.servlet.Servlet; +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import net.sourceforge.guacamole.GuacamoleException; + +/** + * Simple HttpServlet which outputs XML containing a list of all authorized + * configurations for the current user. + * + * @author Michael Jumper + */ +public class WebSocketSupportLoader implements ServletContextListener { + + @Override + public void contextDestroyed(ServletContextEvent sce) { + } + + @Override + public void contextInitialized(ServletContextEvent sce) { + + try { + + // Attempt to find WebSocket servlet + Class servlet = (Class) GuacamoleClassLoader.getInstance().findClass( + "net.sourceforge.guacamole.net.basic.BasicGuacamoleTunnelServlet" + //"net.sourceforge.guacamole.net.basic.BasicGuacamoleWebSocketTunnelServlet" + ); + + // Dynamically add servlet IF SERVLET 3.0 API AVAILABLE! + try { + + // Get servlet registration class + Class regClass = Class.forName("javax.servlet.ServletRegistration"); + + // Get and invoke addServlet() + Method addServlet = ServletContext.class.getMethod("addServlet", String.class, Class.class); + Object reg = addServlet.invoke(sce.getServletContext(), "WebSocketTunnel", servlet); + + // Get and invoke addMapping() + Method addMapping = regClass.getMethod("addMapping", String[].class); + addMapping.invoke(reg, (Object) new String[]{"/websocket-tunnel"}); + + // If we succesfully load and register the WebSocket tunnel servlet, + // WebSocket is supported. + System.err.println("WebSocket support found!"); + + } + catch (ClassNotFoundException e) { + // Servlet API 3.0 unsupported + System.err.println("Servlet API 3.0 not found."); + } + catch (NoSuchMethodException e) { + // Servlet API 3.0 unsupported + System.err.println("Servlet API 3.0 not found."); + } + catch (IllegalAccessException e) { + } + catch (InvocationTargetException e) { + } + + } + catch (ClassNotFoundException e) { + + // If no such servlet class, WebSocket support not present + System.err.println("WebSocket support not found."); + + } + catch (GuacamoleException e) { + e.printStackTrace(); + } + + } + +} + diff --git a/guacamole/src/main/webapp/WEB-INF/web.xml b/guacamole/src/main/webapp/WEB-INF/web.xml index 041e82ce0..61ae0b21b 100644 --- a/guacamole/src/main/webapp/WEB-INF/web.xml +++ b/guacamole/src/main/webapp/WEB-INF/web.xml @@ -28,6 +28,11 @@ + + + net.sourceforge.guacamole.net.basic.WebSocketSupportLoader + + Login servlet.