Refactored supporting revised 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 javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpSession;
24 import net.sourceforge.guacamole.GuacamoleException;
25 import net.sourceforge.guacamole.net.InetGuacamoleSocket;
26 import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
27 import net.sourceforge.guacamole.properties.GuacamoleProperties;
28 import net.sourceforge.guacamole.net.GuacamoleSocket;
29 import net.sourceforge.guacamole.servlet.GuacamoleSession;
30 import net.sourceforge.guacamole.net.GuacamoleTunnel;
31 import net.sourceforge.guacamole.net.basic.properties.BasicGuacamoleProperties;
32 import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket;
33 import net.sourceforge.guacamole.servlet.GuacamoleTunnelServlet;
34
35 public class BasicGuacamoleTunnelServlet extends GuacamoleTunnelServlet {
36
37     private AuthenticationProvider authProvider;
38
39     @Override
40     public void init() throws ServletException {
41
42         // Get auth provider instance
43         try {
44             authProvider = GuacamoleProperties.getProperty(BasicGuacamoleProperties.AUTH_PROVIDER);
45         }
46         catch (GuacamoleException e) {
47             throw new ServletException(e);
48         }
49
50     }
51
52     @Override
53     protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
54
55         HttpSession httpSession = request.getSession(true);
56
57         // Retrieve username and password from parms
58         String username = request.getParameter("username");
59         String password = request.getParameter("password");
60
61         // Get authorized config
62         GuacamoleConfiguration config = authProvider.getAuthorizedConfiguration(username, password);
63         if (config == null)
64             throw new GuacamoleException("Invalid login");
65
66         // Configure and connect socket
67         String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
68         int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
69
70         GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
71                 new InetGuacamoleSocket(hostname, port),
72                 config
73         );
74
75         // Associate socket with tunnel
76         GuacamoleTunnel tunnel = new GuacamoleTunnel(socket);
77
78         // Attach tunnel to session
79         GuacamoleSession session = new GuacamoleSession(httpSession);
80         session.attachTunnel(tunnel);
81
82         return tunnel;
83
84     }
85
86 }
87