Removed native components (now in own repositories)
[guacamole.git] / web / guacamole-common / src / main / java / net / sourceforge / guacamole / net / GuacamoleSession.java
1
2 package net.sourceforge.guacamole.net;
3
4 /*
5  *  Guacamole - Clientless Remote Desktop
6  *  Copyright (C) 2010  Michael Jumper
7  *
8  *  This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU Affero General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU Affero General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Affero General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 import java.util.concurrent.locks.ReentrantLock;
23 import javax.servlet.ServletContext;
24 import javax.servlet.http.HttpSession;
25 import javax.servlet.http.HttpSessionBindingEvent;
26 import javax.servlet.http.HttpSessionBindingListener;
27 import net.sourceforge.guacamole.Client;
28 import net.sourceforge.guacamole.GuacamoleClient;
29 import net.sourceforge.guacamole.GuacamoleException;
30
31 public class GuacamoleSession {
32
33     private GuacamoleConfiguration config;
34     private final HttpSession session;
35     private SessionClient client;
36     private ReentrantLock instructionStreamLock;
37
38     private String protocol;
39     private String hostname;
40     private int port;
41     private String password;
42
43     public class SessionClient extends Client implements HttpSessionBindingListener {
44
45         private Client client;
46         private ReentrantLock authorizedLock;
47
48         public SessionClient(Client client) {
49             this.client = client;
50
51             authorizedLock = new ReentrantLock();
52             authorizedLock.lock();
53         }
54
55         public void authorize() {
56             authorizedLock.unlock();
57         }
58
59         public void waitForAuthorization() {
60             if (authorizedLock.isLocked()) {
61                 try {
62                     authorizedLock.lock();
63                     authorizedLock.unlock();
64                 }
65                 catch (Throwable t) {
66                     throw new Error("Internal error waiting for authorization", t);
67                 }
68             }
69         }
70
71         public void valueBound(HttpSessionBindingEvent event) {
72             // Do nothing
73         }
74
75         public void valueUnbound(HttpSessionBindingEvent event) {
76             try {
77                 disconnect();
78             }
79             catch (GuacamoleException e) {
80                 // Ignore
81             }
82         }
83
84         public void write(char[] data, int off, int len) throws GuacamoleException {
85             client.write(data, off, len);
86         }
87
88         public char[] read() throws GuacamoleException {
89             return client.read();
90         }
91
92         public void disconnect() throws GuacamoleException {
93             client.disconnect();
94         }
95
96     }
97
98     public GuacamoleSession(HttpSession session) throws GuacamoleException {
99
100         if (session == null)
101             throw new GuacamoleException("User has no session.");
102
103         this.session = session;
104         synchronized (session) {
105
106             // Read configuration parameters
107             config = new GuacamoleConfiguration();
108
109             client = (SessionClient) session.getAttribute("CLIENT");
110             instructionStreamLock = (ReentrantLock) session.getAttribute("INSTRUCTION_STREAM_LOCK");
111         }
112     }
113
114     public void connect() throws GuacamoleException {
115         synchronized (session) {
116
117             if (client != null)
118                 client.disconnect();
119
120
121             client = new SessionClient(
122                     new GuacamoleClient (
123                         config.getProxyHostname(),
124                         config.getProxyPort()
125                     )
126             );
127
128             session.setAttribute("CLIENT", client);
129
130             instructionStreamLock = new ReentrantLock();
131             session.setAttribute("INSTRUCTION_STREAM_LOCK", instructionStreamLock);
132
133         }
134     }
135
136     public boolean isConnected() {
137         synchronized (session) {
138             return client != null;
139         }
140     }
141
142     public GuacamoleConfiguration getConfiguration() {
143         return config;
144     }
145
146     public SessionClient getClient() {
147         synchronized (session) {
148             return client;
149         }
150     }
151
152     public void invalidate() {
153         session.invalidate();
154     }
155
156     public void disconnect() throws GuacamoleException {
157         if (client != null) {
158             client.disconnect();
159
160             session.removeAttribute("CLIENT");
161             client = null;
162         }
163     }
164
165     public ReentrantLock getInstructionStreamLock() {
166         return instructionStreamLock;
167     }
168
169     public void setConnection(String protocol, String hostname, int port) {
170         this.protocol = protocol;
171         this.hostname = hostname;
172         this.port = port;
173     }
174
175     public String getProtocol() {
176         return protocol;
177     }
178
179     public String getHostname() {
180         return hostname;
181     }
182
183     public int getPort() {
184         return port;
185     }
186
187     public String getPassword() {
188         return password;
189     }
190
191     public void setPassword(String password) {
192         this.password = password;
193     }
194
195     public String getConnectMessage() throws GuacamoleException {
196
197         if (getProtocol() == null)
198             throw new GuacamoleException("Protocol not specified");
199
200         if (getHostname() == null)
201             throw new GuacamoleException("Hostname not specified");
202
203         if (getPassword() == null)
204             return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + ";";
205         else
206             return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + "," + getPassword() + ";";
207     }
208
209 }