Ticket #362: All basic CRUD operations for connection.

This commit is contained in:
James Muehlner
2013-09-24 19:58:22 -07:00
parent d111d9a9a6
commit feb03b87c5
2 changed files with 159 additions and 32 deletions

View File

@@ -20,8 +20,10 @@ package org.glyptodon.guacamole.net.basic.rest.connection;
import java.util.List; import java.util.List;
import org.glyptodon.guacamole.GuacamoleException; import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.net.GuacamoleSocket;
import org.glyptodon.guacamole.net.auth.Connection; import org.glyptodon.guacamole.net.auth.Connection;
import org.glyptodon.guacamole.net.auth.ConnectionRecord; import org.glyptodon.guacamole.net.auth.ConnectionRecord;
import org.glyptodon.guacamole.protocol.GuacamoleClientInformation;
import org.glyptodon.guacamole.protocol.GuacamoleConfiguration; import org.glyptodon.guacamole.protocol.GuacamoleConfiguration;
/** /**
@@ -29,7 +31,7 @@ import org.glyptodon.guacamole.protocol.GuacamoleConfiguration;
* *
* @author James Muehlner * @author James Muehlner
*/ */
public class APIConnection { public class APIConnection implements Connection {
/** /**
* The name of this connection. * The name of this connection.
@@ -70,58 +72,36 @@ public class APIConnection {
this.history = connection.getHistory(); this.history = connection.getHistory();
} }
/** @Override
* Get the name of this connection.
* @return The name of this connection.
*/
public String getName() { public String getName() {
return name; return name;
} }
/** @Override
* Set the name of this connection.
* @param name The name of this connection.
*/
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@Override
/**
* Get the identifier of this connection.
* @return The identifier of this connection.
*/
public String getIdentifier() { public String getIdentifier() {
return identifier; return identifier;
} }
/** @Override
* Set the identifier of this connection.
* @param identifier The identifier of this connection.
*/
public void setIdentifier(String identifier) { public void setIdentifier(String identifier) {
this.identifier = identifier; this.identifier = identifier;
} }
/** @Override
* Get the configuration for this connection.
* @return The configuration for this connection.
*/
public GuacamoleConfiguration getConfiguration() { public GuacamoleConfiguration getConfiguration() {
return configuration; return configuration;
} }
/** @Override
* Set the configuration for this connection.
* @param configuration The configuration for this connection.
*/
public void setConfiguration(GuacamoleConfiguration configuration) { public void setConfiguration(GuacamoleConfiguration configuration) {
this.configuration = configuration; this.configuration = configuration;
} }
/** @Override
* Get the history records for this connection.
* @return The history records for this connection.
*/
public List<? extends ConnectionRecord> getHistory() { public List<? extends ConnectionRecord> getHistory() {
return history; return history;
} }
@@ -133,5 +113,10 @@ public class APIConnection {
public void setHistory(List<? extends ConnectionRecord> history) { public void setHistory(List<? extends ConnectionRecord> history) {
this.history = history; this.history = history;
} }
@Override
public GuacamoleSocket connect(GuacamoleClientInformation info) throws GuacamoleException {
throw new UnsupportedOperationException("Not supported.");
}
} }

View File

@@ -23,6 +23,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
@@ -126,7 +128,8 @@ public class ConnectionRESTService {
*/ */
@GET @GET
@Path("/{connectionID}") @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); UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken);
try { try {
@@ -157,7 +160,7 @@ public class ConnectionRESTService {
* *
* @param authToken The authentication token that is used to authenticate * @param authToken The authentication token that is used to authenticate
* the user performing the operation. * the user performing the operation.
* @param connectionID The ID of the Connection. * @param connectionID The ID of the Connection to delete.
*/ */
@DELETE @DELETE
@Path("/{connectionID}") @Path("/{connectionID}")
@@ -185,5 +188,144 @@ public class ConnectionRESTService {
"Unexpected server error. " + e.getMessage()); "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<String, ConnectionGroup> connectionGroupDirectory = rootGroup.getConnectionGroupDirectory();
parentConnectionGroup = connectionGroupDirectory.get(parentID);
}
if(parentConnectionGroup == null)
throw new HTTPException(Status.BAD_REQUEST,
"No ConnectionGroup found with the provided parentID.");
Directory<String, Connection> 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<String, Connection> 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<String, Connection> connectionDirectory =
rootGroup.getConnectionDirectory();
// Find the new parent connection group
Directory<String, ConnectionGroup> 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());
}
}
} }