b9670947a80aca12b9d2180d4d5d05b613000393
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / WebSocketSupportLoader.java
1 package net.sourceforge.guacamole.net.basic;
2
3 /*
4  *  Guacamole - Clientless Remote Desktop
5  *  Copyright (C) 2010  Michael Jumper
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Affero General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Affero General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Affero General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import javax.servlet.Servlet;
24 import javax.servlet.ServletContext;
25 import javax.servlet.ServletContextEvent;
26 import javax.servlet.ServletContextListener;
27 import net.sourceforge.guacamole.GuacamoleException;
28
29 /**
30  * Simple HttpServlet which outputs XML containing a list of all authorized
31  * configurations for the current user.
32  * 
33  * @author Michael Jumper
34  */
35 public class WebSocketSupportLoader implements ServletContextListener {
36
37     @Override
38     public void contextDestroyed(ServletContextEvent sce) {
39     }
40
41     @Override
42     public void contextInitialized(ServletContextEvent sce) {
43
44         try {
45
46             // Attempt to find WebSocket servlet
47             Class<Servlet> servlet = (Class<Servlet>) GuacamoleClassLoader.getInstance().findClass(
48                 "net.sourceforge.guacamole.net.basic.BasicGuacamoleTunnelServlet"
49                 //"net.sourceforge.guacamole.net.basic.BasicGuacamoleWebSocketTunnelServlet"
50             ); 
51
52             // Dynamically add servlet IF SERVLET 3.0 API AVAILABLE!
53             try {
54
55                 // Get servlet registration class
56                 Class regClass = Class.forName("javax.servlet.ServletRegistration");
57
58                 // Get and invoke addServlet()
59                 Method addServlet = ServletContext.class.getMethod("addServlet", String.class, Class.class);
60                 Object reg = addServlet.invoke(sce.getServletContext(), "WebSocketTunnel", servlet);
61
62                 // Get and invoke addMapping()
63                 Method addMapping = regClass.getMethod("addMapping", String[].class);
64                 addMapping.invoke(reg, (Object) new String[]{"/websocket-tunnel"});
65
66                 // If we succesfully load and register the WebSocket tunnel servlet,
67                 // WebSocket is supported.
68                 System.err.println("WebSocket support found!");
69
70             }
71             catch (ClassNotFoundException e) {
72                 // Servlet API 3.0 unsupported
73                 System.err.println("Servlet API 3.0 not found.");
74             }
75             catch (NoSuchMethodException e) {
76                 // Servlet API 3.0 unsupported
77                 System.err.println("Servlet API 3.0 not found.");
78             }
79             catch (IllegalAccessException e) {
80             }
81             catch (InvocationTargetException e) {
82             }
83
84         }
85         catch (ClassNotFoundException e) {
86             
87             // If no such servlet class, WebSocket support not present
88             System.err.println("WebSocket support not found.");
89
90         }
91         catch (GuacamoleException e) {
92             e.printStackTrace();
93         }
94
95     }
96
97 }
98