From 79680219cffb37e69c4524ae345d577991c77a99 Mon Sep 17 00:00:00 2001 From: Vasily Loginov Date: Fri, 13 Feb 2015 16:32:36 +0600 Subject: [PATCH] GUAC-1086: Split TunnelRequestService.createTunnel method into three methods solving separate tasks. --- .../net/basic/TunnelRequestService.java | 106 +++++++++++++----- 1 file changed, 75 insertions(+), 31 deletions(-) diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/TunnelRequestService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/TunnelRequestService.java index 25e7c87fe..63665a29b 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/TunnelRequestService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/TunnelRequestService.java @@ -148,33 +148,13 @@ public class TunnelRequestService { } - /** - * Creates a new tunnel using the parameters and credentials present in - * the given request. - * - * @param request The request describing the tunnel to create. - * @return The created tunnel, or null if the tunnel could not be created. - * @throws GuacamoleException If an error occurs while creating the tunnel. + * Reads and returns client information provided by the {@code request} parameter. + * + * @param request The request describing tunnel to create. + * @return GuacamoleClientInformation Object containing information about the client sending the tunnel request. */ - public GuacamoleTunnel createTunnel(TunnelRequest request) - throws GuacamoleException { - - // Get auth token and session - final String authToken = request.getParameter("authToken"); - GuacamoleSession session = authenticationService.getGuacamoleSession(authToken); - - // Get ID of connection - String id = request.getParameter("id"); - TunnelRequest.IdentifierType id_type = TunnelRequest.IdentifierType.getType(id); - - // Do not continue if unable to determine type - if (id_type == null) - throw new GuacamoleClientException("Illegal identifier - unknown type."); - - // Remove prefix - id = id.substring(id_type.PREFIX.length()); - + protected GuacamoleClientInformation getClientInformation(TunnelRequest request) { // Get client information GuacamoleClientInformation info = new GuacamoleClientInformation(); @@ -203,6 +183,32 @@ public class TunnelRequestService { if (video_mimetypes != null) info.getVideoMimetypes().addAll(video_mimetypes); + return info; + } + + /** + * Creates a new socket using client information specified in the {@code info} parameter, + * connection information from {@code request} and credentials from the {@code session} parameter. + * + * @param request The request describing tunnel to create. + * @param session Current guacamole session. + * @param info Guacamole client information. + * @return Socket connected using the provided settings. + * @throws GuacamoleException If an error occurs while creating the socket. + */ + protected GuacamoleSocket createConnectedSocket(TunnelRequest request, GuacamoleSession session, + GuacamoleClientInformation info) throws GuacamoleException { + // Get ID of connection + String id = request.getParameter("id"); + TunnelRequest.IdentifierType id_type = TunnelRequest.IdentifierType.getType(id); + + // Do not continue if unable to determine type + if (id_type == null) + throw new GuacamoleClientException("Illegal identifier - unknown type."); + + // Remove prefix + id = id.substring(id_type.PREFIX.length()); + // Create connected socket from identifier GuacamoleSocket socket; switch (id_type) { @@ -214,7 +220,7 @@ public class TunnelRequestService { // Get connection directory Directory directory = - context.getRootConnectionGroup().getConnectionDirectory(); + context.getRootConnectionGroup().getConnectionDirectory(); // Get authorized connection Connection connection = directory.get(id); @@ -236,7 +242,7 @@ public class TunnelRequestService { // Get connection group directory Directory directory = - context.getRootConnectionGroup().getConnectionGroupDirectory(); + context.getRootConnectionGroup().getConnectionGroupDirectory(); // Get authorized connection group ConnectionGroup group = directory.get(id); @@ -256,9 +262,21 @@ public class TunnelRequestService { throw new GuacamoleClientException("Connection not supported for provided identifier type."); } + return socket; + } - // Associate socket with tunnel - GuacamoleTunnel tunnel = new GuacamoleTunnel(socket) { + + /** + * Creates and returns a tunnel using the specified guacd socket. + * The tunnel is associated with a session identified + * by the {@code authToken} parameter. + * + * @param socket The connected socket. + * @param authToken Current authorization token. + * @return The created tunnel. + */ + protected GuacamoleTunnel createAssociatedTunnel(GuacamoleSocket socket, final String authToken) { + return new GuacamoleTunnel(socket) { @Override public GuacamoleReader acquireReader() { @@ -281,7 +299,7 @@ public class TunnelRequestService { // Pass through by default. return super.acquireReader(); - + } @Override @@ -303,13 +321,39 @@ public class TunnelRequestService { throw new GuacamoleException("Tunnel close canceled by listener."); session.removeTunnel(getUUID().toString()); - + // Close if no exception due to listener super.close(); } }; + } + + + /** + * Creates a new tunnel using the parameters and credentials present in + * the given request. + * + * @param request The request describing the tunnel to create. + * @return The created tunnel, or null if the tunnel could not be created. + * @throws GuacamoleException If an error occurs while creating the tunnel. + */ + public GuacamoleTunnel createTunnel(TunnelRequest request) + throws GuacamoleException { + + // Get auth token and session + final String authToken = request.getParameter("authToken"); + final GuacamoleSession session = authenticationService.getGuacamoleSession(authToken); + + // Get client information + final GuacamoleClientInformation info = getClientInformation(request); + + // Create connected socket from identifier + final GuacamoleSocket socket = createConnectedSocket(request, session, info); + + // Associate socket with tunnel + GuacamoleTunnel tunnel = createAssociatedTunnel(socket, authToken); // Notify listeners about connection if (!notifyConnect(session, tunnel)) {