2 package net.sourceforge.guacamole.net;
5 * Guacamole - Clientless Remote Desktop
6 * Copyright (C) 2010 Michael Jumper
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.
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.
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/>.
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;
31 public class GuacamoleSession {
33 private GuacamoleConfiguration config;
34 private final HttpSession session;
35 private SessionClient client;
36 private ReentrantLock instructionStreamLock;
38 private String protocol;
39 private String hostname;
41 private String password;
43 public class SessionClient extends Client implements HttpSessionBindingListener {
45 private Client client;
46 private ReentrantLock authorizedLock;
48 public SessionClient(Client client) {
51 authorizedLock = new ReentrantLock();
52 authorizedLock.lock();
55 public void authorize() {
56 authorizedLock.unlock();
59 public void waitForAuthorization() {
60 if (authorizedLock.isLocked()) {
62 authorizedLock.lock();
63 authorizedLock.unlock();
66 throw new Error("Internal error waiting for authorization", t);
71 public void valueBound(HttpSessionBindingEvent event) {
75 public void valueUnbound(HttpSessionBindingEvent event) {
79 catch (GuacamoleException e) {
84 public void write(char[] data, int off, int len) throws GuacamoleException {
85 client.write(data, off, len);
88 public char[] read() throws GuacamoleException {
92 public void disconnect() throws GuacamoleException {
98 public GuacamoleSession(HttpSession session) throws GuacamoleException {
101 throw new GuacamoleException("User has no session.");
103 this.session = session;
104 synchronized (session) {
106 // Read configuration parameters
107 config = new GuacamoleConfiguration();
109 client = (SessionClient) session.getAttribute("CLIENT");
110 instructionStreamLock = (ReentrantLock) session.getAttribute("INSTRUCTION_STREAM_LOCK");
114 public void connect() throws GuacamoleException {
115 synchronized (session) {
121 client = new SessionClient(
122 new GuacamoleClient (
123 config.getProxyHostname(),
124 config.getProxyPort()
128 session.setAttribute("CLIENT", client);
130 instructionStreamLock = new ReentrantLock();
131 session.setAttribute("INSTRUCTION_STREAM_LOCK", instructionStreamLock);
136 public boolean isConnected() {
137 synchronized (session) {
138 return client != null;
142 public GuacamoleConfiguration getConfiguration() {
146 public SessionClient getClient() {
147 synchronized (session) {
152 public void invalidate() {
153 session.invalidate();
156 public void disconnect() throws GuacamoleException {
157 if (client != null) {
160 session.removeAttribute("CLIENT");
165 public ReentrantLock getInstructionStreamLock() {
166 return instructionStreamLock;
169 public void setConnection(String protocol, String hostname, int port) {
170 this.protocol = protocol;
171 this.hostname = hostname;
175 public String getProtocol() {
179 public String getHostname() {
183 public int getPort() {
187 public String getPassword() {
191 public void setPassword(String password) {
192 this.password = password;
195 public String getConnectMessage() throws GuacamoleException {
197 if (getProtocol() == null)
198 throw new GuacamoleException("Protocol not specified");
200 if (getHostname() == null)
201 throw new GuacamoleException("Hostname not specified");
203 if (getPassword() == null)
204 return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + ";";
206 return "connect:" + getProtocol() + "," + getHostname() + "," + getPort() + "," + getPassword() + ";";