mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-07 05:31:22 +00:00
Major refactor of API (new interfaces, semantic changes)
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
|
||||
package net.sourceforge.guacamole.servlet;
|
||||
|
||||
/*
|
||||
* Guacamole - Clientless Remote Desktop
|
||||
* Copyright (C) 2010 Michael Jumper
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import net.sourceforge.guacamole.GuacamoleException;
|
||||
import net.sourceforge.guacamole.net.GuacamoleTunnel;
|
||||
|
||||
public class GuacamoleSession {
|
||||
|
||||
private final HttpSession session;
|
||||
private ConcurrentMap<String, GuacamoleTunnel> tunnels;
|
||||
|
||||
public GuacamoleSession(HttpSession session) throws GuacamoleException {
|
||||
|
||||
if (session == null)
|
||||
throw new GuacamoleException("User has no session.");
|
||||
|
||||
this.session = session;
|
||||
|
||||
synchronized (session) {
|
||||
|
||||
tunnels = (ConcurrentMap<String, GuacamoleTunnel>) session.getAttribute("GUAC_TUNNELS");
|
||||
if (tunnels == null) {
|
||||
tunnels = new ConcurrentHashMap<String, GuacamoleTunnel>();
|
||||
session.setAttribute("GUAC_TUNNELS", tunnels);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void invalidate() {
|
||||
session.invalidate();
|
||||
}
|
||||
|
||||
public void attachTunnel(GuacamoleTunnel tunnel) throws GuacamoleException {
|
||||
tunnels.put(tunnel.getUUID().toString(), tunnel);
|
||||
}
|
||||
|
||||
public void detachTunnel(GuacamoleTunnel tunnel) throws GuacamoleException {
|
||||
tunnels.remove(tunnel.getUUID().toString());
|
||||
}
|
||||
|
||||
public GuacamoleTunnel getTunnel(String tunnelUUID) {
|
||||
return tunnels.get(tunnelUUID);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,220 @@
|
||||
package net.sourceforge.guacamole.servlet;
|
||||
|
||||
/*
|
||||
* Guacamole - Clientless Remote Desktop
|
||||
* Copyright (C) 2010 Michael Jumper
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import net.sourceforge.guacamole.net.GuacamoleTunnel;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import net.sourceforge.guacamole.GuacamoleException;
|
||||
import net.sourceforge.guacamole.io.GuacamoleReader;
|
||||
import net.sourceforge.guacamole.net.GuacamoleSocket;
|
||||
import net.sourceforge.guacamole.io.GuacamoleWriter;
|
||||
|
||||
|
||||
public abstract class GuacamoleTunnelServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
|
||||
service(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
|
||||
service(request, response);
|
||||
}
|
||||
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException {
|
||||
|
||||
try {
|
||||
|
||||
String query = request.getQueryString();
|
||||
if (query == null)
|
||||
throw new GuacamoleException("No query string provided.");
|
||||
|
||||
// If connect operation, call doConnect() and return tunnel UUID
|
||||
// in response.
|
||||
if (query.equals("connect")) {
|
||||
|
||||
GuacamoleTunnel tunnel = doConnect(request);
|
||||
if (tunnel != null) {
|
||||
try {
|
||||
response.getWriter().println(tunnel.getUUID().toString());
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new GuacamoleException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// If read operation, call doRead() with tunnel UUID
|
||||
else if(query.startsWith("read:"))
|
||||
doRead(request, response, query.substring(5));
|
||||
|
||||
// If write operation, call doWrite() with tunnel UUID
|
||||
else if(query.startsWith("write:"))
|
||||
doWrite(request, response, query.substring(6));
|
||||
|
||||
// Otherwise, invalid operation
|
||||
else
|
||||
throw new GuacamoleException("Invalid tunnel operation: " + query);
|
||||
}
|
||||
|
||||
// Catch any thrown guacamole exception and attempt to pass within the
|
||||
// HTTP response.
|
||||
catch (GuacamoleException e) {
|
||||
|
||||
try {
|
||||
|
||||
// If response not committed, send error code along with
|
||||
// message.
|
||||
if (!response.isCommitted()) {
|
||||
response.setHeader("X-Guacamole-Error-Message", e.getMessage());
|
||||
response.sendError(
|
||||
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
|
||||
e.getMessage()
|
||||
);
|
||||
}
|
||||
|
||||
// If unable to send error code, rethrow as servlet exception
|
||||
else
|
||||
throw new ServletException(e);
|
||||
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
|
||||
// If unable to send error at all due to I/O problems,
|
||||
// rethrow as servlet exception
|
||||
throw new ServletException(ioe);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected abstract GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException;
|
||||
|
||||
protected void doRead(HttpServletRequest request, HttpServletResponse response, String tunnelUUID) throws GuacamoleException {
|
||||
|
||||
HttpSession httpSession = request.getSession(false);
|
||||
GuacamoleSession session = new GuacamoleSession(httpSession);
|
||||
|
||||
GuacamoleTunnel tunnel = session.getTunnel(tunnelUUID);
|
||||
if (tunnel == null)
|
||||
throw new GuacamoleException("No such tunnel.");
|
||||
|
||||
// Obtain exclusive read access
|
||||
GuacamoleReader reader = tunnel.acquireReader();
|
||||
|
||||
try {
|
||||
|
||||
// Note that although we are sending text, Webkit browsers will
|
||||
// buffer 1024 bytes before starting a normal stream if we use
|
||||
// anything but application/octet-stream.
|
||||
response.setContentType("application/octet-stream");
|
||||
|
||||
Writer out = response.getWriter();
|
||||
|
||||
// For all messages, until another stream is ready (we send at least one message)
|
||||
char[] message;
|
||||
while ((message = reader.read()) != null) {
|
||||
|
||||
// Get message output bytes
|
||||
out.write(message, 0, message.length);
|
||||
out.flush();
|
||||
response.flushBuffer();
|
||||
|
||||
// No more messages another stream can take over
|
||||
if (tunnel.hasQueuedReaderThreads())
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if (message == null) {
|
||||
session.detachTunnel(tunnel);
|
||||
throw new GuacamoleException("Disconnected.");
|
||||
}
|
||||
|
||||
// End-of-instructions marker
|
||||
out.write(';');
|
||||
out.flush();
|
||||
response.flushBuffer();
|
||||
|
||||
}
|
||||
catch (UnsupportedEncodingException e) {
|
||||
throw new GuacamoleException("UTF-8 not supported by Java.", e);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new GuacamoleException("I/O error writing to servlet output stream.", e);
|
||||
}
|
||||
finally {
|
||||
tunnel.releaseReader();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void doWrite(HttpServletRequest request, HttpServletResponse response, String tunnelUUID) throws GuacamoleException {
|
||||
|
||||
HttpSession httpSession = request.getSession(false);
|
||||
GuacamoleSession session = new GuacamoleSession(httpSession);
|
||||
|
||||
GuacamoleTunnel tunnel = session.getTunnel(tunnelUUID);
|
||||
if (tunnel == null)
|
||||
throw new GuacamoleException("No such tunnel.");
|
||||
|
||||
// We still need to set the content type to avoid the default of
|
||||
// text/html, as such a content type would cause some browsers to
|
||||
// attempt to parse the result, even though the JavaScript client
|
||||
// does not explicitly request such parsing.
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setContentLength(0);
|
||||
|
||||
// Send data
|
||||
try {
|
||||
|
||||
GuacamoleWriter writer = tunnel.acquireWriter();
|
||||
|
||||
Reader input = request.getReader();
|
||||
char[] buffer = new char[8192];
|
||||
|
||||
int length;
|
||||
while ((length = input.read(buffer, 0, buffer.length)) != -1)
|
||||
writer.write(buffer, 0, length);
|
||||
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new GuacamoleException("I/O Error sending data to server: " + e.getMessage(), e);
|
||||
}
|
||||
finally {
|
||||
tunnel.releaseWriter();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user