Migrated to new tunnel API.
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / BasicGuacamoleTunnelServlet.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 javax.servlet.ServletException;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpSession;
25 import net.sourceforge.guacamole.GuacamoleException;
26 import net.sourceforge.guacamole.GuacamoleTCPClient;
27 import net.sourceforge.guacamole.net.Configuration;
28 import net.sourceforge.guacamole.net.GuacamoleProperties;
29 import net.sourceforge.guacamole.net.GuacamoleSession;
30 import net.sourceforge.guacamole.net.tunnel.GuacamoleTunnel;
31 import net.sourceforge.guacamole.net.tunnel.GuacamoleTunnelServlet;
32
33 public class BasicGuacamoleTunnelServlet extends GuacamoleTunnelServlet {
34
35     private AuthenticationProvider authProvider;
36
37     @Override
38     public void init() throws ServletException {
39
40         // Get auth provider instance
41         try {
42             String authProviderClassName = GuacamoleProperties.getProperty("auth-provider");
43             Object obj = Class.forName(authProviderClassName).getConstructor().newInstance();
44             if (!(obj instanceof AuthenticationProvider))
45                 throw new ServletException("Specified authentication provider class is not a AuthenticationProvider.");
46
47             authProvider = (AuthenticationProvider) obj;
48         }
49         catch (GuacamoleException e) {
50             throw new ServletException(e);
51         }
52         catch (ClassNotFoundException e) {
53             throw new ServletException("Authentication provider class not found", e);
54         }
55         catch (NoSuchMethodException e) {
56             throw new ServletException("Default constructor for authentication provider not present", e);
57         }
58         catch (SecurityException e) {
59             throw new ServletException("Creation of authentication provider disallowed; check your security settings", e);
60         }
61         catch (InstantiationException e) {
62             throw new ServletException("Unable to instantiate authentication provider", e);
63         }
64         catch (IllegalAccessException e) {
65             throw new ServletException("Unable to access default constructor of authentication provider", e);
66         }
67         catch (InvocationTargetException e) {
68             throw new ServletException("Internal error in constructor of authentication provider", e.getTargetException());
69         }
70
71     }
72
73     @Override
74     protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
75
76         HttpSession httpSession = request.getSession(true);
77
78         // Retrieve username and password from parms
79         String username = request.getParameter("username");
80         String password = request.getParameter("password");
81
82         // Get authorized config
83         Configuration config = authProvider.getAuthorizedConfiguration(username, password);
84         if (config == null)
85             throw new GuacamoleException("Invalid login");
86
87         // Configure and connect client
88         String hostname = GuacamoleProperties.getProperty("guacd-hostname");
89         int port = GuacamoleProperties.getIntProperty("guacd-port", null);
90
91         GuacamoleTCPClient client = new GuacamoleTCPClient(hostname, port);
92         client.connect(config);
93
94         // Associate client with tunnel
95         GuacamoleTunnel tunnel = new GuacamoleTunnel(client);
96
97         // Attach tunnel to session
98         GuacamoleSession session = new GuacamoleSession(httpSession);
99         session.attachTunnel(tunnel);
100
101         return tunnel;
102
103     }
104
105 }
106