diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/APIConnection.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/APIConnection.java index 797f045da..0e78f3fcc 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/APIConnection.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/APIConnection.java @@ -20,8 +20,10 @@ package org.glyptodon.guacamole.net.basic.rest.connection; import java.util.List; import org.glyptodon.guacamole.GuacamoleException; +import org.glyptodon.guacamole.net.GuacamoleSocket; import org.glyptodon.guacamole.net.auth.Connection; import org.glyptodon.guacamole.net.auth.ConnectionRecord; +import org.glyptodon.guacamole.protocol.GuacamoleClientInformation; import org.glyptodon.guacamole.protocol.GuacamoleConfiguration; /** @@ -29,7 +31,7 @@ import org.glyptodon.guacamole.protocol.GuacamoleConfiguration; * * @author James Muehlner */ -public class APIConnection { +public class APIConnection implements Connection { /** * The name of this connection. @@ -70,58 +72,36 @@ public class APIConnection { this.history = connection.getHistory(); } - /** - * Get the name of this connection. - * @return The name of this connection. - */ + @Override public String getName() { return name; } - /** - * Set the name of this connection. - * @param name The name of this connection. - */ + @Override public void setName(String name) { this.name = name; } - - /** - * Get the identifier of this connection. - * @return The identifier of this connection. - */ + @Override public String getIdentifier() { return identifier; } - /** - * Set the identifier of this connection. - * @param identifier The identifier of this connection. - */ + @Override public void setIdentifier(String identifier) { this.identifier = identifier; } - /** - * Get the configuration for this connection. - * @return The configuration for this connection. - */ + @Override public GuacamoleConfiguration getConfiguration() { return configuration; } - /** - * Set the configuration for this connection. - * @param configuration The configuration for this connection. - */ + @Override public void setConfiguration(GuacamoleConfiguration configuration) { this.configuration = configuration; } - /** - * Get the history records for this connection. - * @return The history records for this connection. - */ + @Override public List getHistory() { return history; } @@ -133,5 +113,10 @@ public class APIConnection { public void setHistory(List history) { this.history = history; } + + @Override + public GuacamoleSocket connect(GuacamoleClientInformation info) throws GuacamoleException { + throw new UnsupportedOperationException("Not supported."); + } } diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java index 961cccb13..692bff933 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionRESTService.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.List; import javax.ws.rs.DELETE; import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @@ -126,7 +128,8 @@ public class ConnectionRESTService { */ @GET @Path("/{connectionID}") - public APIConnection getConnection(@QueryParam("token") String authToken, @PathParam("connectionID") String connectionID) { + public APIConnection getConnection(@QueryParam("token") String authToken, + @PathParam("connectionID") String connectionID) { UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken); try { @@ -157,7 +160,7 @@ public class ConnectionRESTService { * * @param authToken The authentication token that is used to authenticate * the user performing the operation. - * @param connectionID The ID of the Connection. + * @param connectionID The ID of the Connection to delete. */ @DELETE @Path("/{connectionID}") @@ -185,5 +188,144 @@ public class ConnectionRESTService { "Unexpected server error. " + e.getMessage()); } } + + /** + * Creates a new connection and returns the identifier of the new connection. + * If a parentID is provided, the connection will be created in the + * connection group with the parentID. Otherwise, the root connection group + * will be used. + * + * @param authToken The authentication token that is used to authenticate + * the user performing the operation. + * @param parentID The ID of the ConnectionGroup the connections + * belong to. If null, the root connection group will be used. + * @param connection The connection to create. + * @return The identifier of the new connection. + */ + @POST + public String createConnection(@QueryParam("token") String authToken, + @QueryParam("parentID") String parentID, APIConnection connection) { + UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken); + + if(connection == null) + throw new HTTPException(Status.BAD_REQUEST, + "A connection is required for this request."); + + try { + // If the parent connection group is passed in, try to find it. + ConnectionGroup parentConnectionGroup; + if(parentID == null) + parentConnectionGroup = userContext.getRootConnectionGroup(); + else { + ConnectionGroup rootGroup = userContext.getRootConnectionGroup(); + Directory connectionGroupDirectory = rootGroup.getConnectionGroupDirectory(); + parentConnectionGroup = connectionGroupDirectory.get(parentID); + } + + if(parentConnectionGroup == null) + throw new HTTPException(Status.BAD_REQUEST, + "No ConnectionGroup found with the provided parentID."); + + Directory connectionDirectory = + parentConnectionGroup.getConnectionDirectory(); + + // Create the connection + connectionDirectory.add(connection); + + // Return the new connection identifier + return connection.getIdentifier(); + } catch(GuacamoleSecurityException e) { + throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied."); + } catch(GuacamoleException e) { + logger.error("Unexpected GuacamoleException caught while listing connections.", e); + throw new HTTPException(Status.INTERNAL_SERVER_ERROR, + "Unexpected server error. " + e.getMessage()); + } + } + + /** + * Updates a connection. + * + * @param authToken The authentication token that is used to authenticate + * the user performing the operation. + * @param connectionID The ID of the Connection to delete. + * @param connection The connection to update. + */ + @POST + @Path("/{connectionID}") + public void updateConnection(@QueryParam("token") String authToken, + @PathParam("connectionID") String connectionID, APIConnection connection) { + UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken); + + if(connection == null) + throw new HTTPException(Status.BAD_REQUEST, + "A connection is required for this request."); + + try { + // Get the connection directory + ConnectionGroup rootGroup = userContext.getRootConnectionGroup(); + Directory connectionDirectory = + rootGroup.getConnectionDirectory(); + + // Make sure the connection is there before trying to update + if(connectionDirectory.get(connectionID) == null) + throw new HTTPException(Status.BAD_REQUEST, + "No Connection with the provided ID."); + + // Update the connection + connectionDirectory.update(connection); + } catch(GuacamoleSecurityException e) { + throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied."); + } catch(GuacamoleException e) { + logger.error("Unexpected GuacamoleException caught while listing connections.", e); + throw new HTTPException(Status.INTERNAL_SERVER_ERROR, + "Unexpected server error. " + e.getMessage()); + } + } + + /** + * Moves an individual connection to a different connection group. + * + * @param authToken The authentication token that is used to authenticate + * the user performing the operation. + * @param connectionID The ID of the Connection to delete. + * @param parentID The ID of the ConnectionGroup the connections + * belong to. If null, the root connection group will be used. + */ + @PUT + @Path("/{connectionID}") + public void moveConnection(@QueryParam("token") String authToken, + @PathParam("connectionID") String connectionID, @QueryParam("parentID") String parentID) { + UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken); + + try { + // Get the connection directory + ConnectionGroup rootGroup = userContext.getRootConnectionGroup(); + Directory connectionDirectory = + rootGroup.getConnectionDirectory(); + + // Find the new parent connection group + Directory connectionGroupDirectory = rootGroup.getConnectionGroupDirectory(); + ConnectionGroup parentConnectionGroup = connectionGroupDirectory.get(parentID); + + if(parentConnectionGroup == null) + throw new HTTPException(Status.BAD_REQUEST, + "No ConnectionGroup found with the provided parentID."); + + // Make sure the connection is there before trying to delete + if(connectionDirectory.get(connectionID) == null) + throw new HTTPException(Status.BAD_REQUEST, + "No Connection found with the provided ID."); + + // Move the connection + connectionDirectory.move(connectionID, connectionDirectory); + } catch(GuacamoleSecurityException e) { + throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied."); + } catch(GuacamoleException e) { + logger.error("Unexpected GuacamoleException caught while deleting a connection.", e); + throw new HTTPException(Status.INTERNAL_SERVER_ERROR, + "Unexpected server error. " + e.getMessage()); + } + } }