Ticket #362: Added getConnection() with path-params.

This commit is contained in:
James Muehlner
2013-09-16 22:12:45 -07:00
parent a624182d76
commit 131aea1173
3 changed files with 72 additions and 19 deletions

View File

@@ -20,6 +20,7 @@ package org.glyptodon.guacamole.net.basic.rest.connection;
import java.util.List;
import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.net.auth.Connection;
import org.glyptodon.guacamole.net.auth.ConnectionRecord;
import org.glyptodon.guacamole.protocol.GuacamoleConfiguration;
@@ -28,7 +29,7 @@ import org.glyptodon.guacamole.protocol.GuacamoleConfiguration;
*
* @author James Muehlner
*/
public class Connection {
public class APIConnection {
/**
* The name of this connection.
@@ -51,17 +52,17 @@ public class Connection {
private List<? extends ConnectionRecord> history;
/**
* Create an empty Connection.
* Create an empty APIConnection.
*/
public Connection() {}
public APIConnection() {}
/**
* Create a Connection from a org.glyptodon.guacamole.net.auth.Connection.
* @param connection The connection to create this Connection from.
* Create an APIConnection from a Connection record.
* @param connection The connection to create this APIConnection from.
* @throws GuacamoleException If a problem is encountered while
* instantiating this new Connection.
* instantiating this new APIConnection.
*/
public Connection(org.glyptodon.guacamole.net.auth.Connection connection)
public APIConnection(Connection connection)
throws GuacamoleException {
this.name = connection.getName();
this.identifier = connection.getIdentifier();

View File

@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
@@ -30,6 +31,7 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.GuacamoleSecurityException;
import org.glyptodon.guacamole.net.auth.Connection;
import org.glyptodon.guacamole.net.auth.ConnectionGroup;
import org.glyptodon.guacamole.net.auth.Directory;
import org.glyptodon.guacamole.net.auth.UserContext;
@@ -52,13 +54,23 @@ public class ConnectionRESTService {
private AuthenticationService authenticationService;
/**
* A service for managing the REST endpoint Connection objects.
* A service for managing the REST endpoint APIConnection objects.
*/
@Inject
private ConnectionService connectionService;
/**
* Gets a list of connections with the given ConnectionGroup parentID.
* If no parentID is provided, returns the connections from the root group.
*
* @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.
* @return The connection list.
*/
@GET
public List<Connection> getConnections(@QueryParam("token") String authToken, @QueryParam("parentID") String parentID) {
public List<APIConnection> getConnections(@QueryParam("token") String authToken, @QueryParam("parentID") String parentID) {
UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken);
try {
@@ -78,12 +90,11 @@ public class ConnectionRESTService {
.entity(new APIError("No ConnectionGroup found with the provided parentID."))
.build());
Directory<String, org.glyptodon.guacamole.net.auth.Connection> connectionDirectory =
Directory<String, Connection> connectionDirectory =
parentConnectionGroup.getConnectionDirectory();
// Get the list of connection names
List<org.glyptodon.guacamole.net.auth.Connection> connections
= new ArrayList<org.glyptodon.guacamole.net.auth.Connection>();
List<Connection> connections = new ArrayList<Connection>();
Iterable<String> identifiers = connectionDirectory.getIdentifiers();
// Get the connection for each name
@@ -101,5 +112,46 @@ public class ConnectionRESTService {
.entity(e).build());
}
}
/**
* Gets an individual connection.
*
* @param authToken The authentication token that is used to authenticate
* the user performing the operation.
* @param connectionID The ID of the APIConnection..
* @return The connection.
*/
@GET
@Path("/{connectionID}")
public APIConnection getConnection(@QueryParam("token") String authToken, @PathParam("connectionID") String connectionID) {
UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken);
try {
// Get the connection directory
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
Directory<String, Connection> connectionDirectory =
rootGroup.getConnectionDirectory();
// Get the connection
Connection connection = connectionDirectory.get(connectionID);
if(connection == null)
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity(new APIError("No Connection found with the provided ID."))
.build());
return new APIConnection(connection);
} catch(GuacamoleSecurityException e) {
throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
} catch(GuacamoleException e) {
throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(e).build());
}
}
}

View File

@@ -30,21 +30,21 @@ import org.glyptodon.guacamole.GuacamoleException;
public class ConnectionService {
/**
* Converts a list of org.glyptodon.guacamole.net.auth.Connection to
* Connection objects for exposure with the REST endpoints.
* Converts a list of org.glyptodon.guacamole.net.auth.APIConnection to
* APIConnection objects for exposure with the REST endpoints.
*
* @param connections The org.glyptodon.guacamole.net.auth.Connection to
* @param connections The org.glyptodon.guacamole.net.auth.APIConnection to
* convert for REST endpoint use.
* @return A List of Connection objects for use with the REST endpoint.
* @return A List of APIConnection objects for use with the REST endpoint.
* @throws GuacamoleException If an error occurs while converting the
* connections.
*/
public List<Connection> convertConnectionList(List<? extends org.glyptodon.guacamole.net.auth.Connection> connections)
public List<APIConnection> convertConnectionList(List<? extends org.glyptodon.guacamole.net.auth.Connection> connections)
throws GuacamoleException {
List<Connection> restConnections = new ArrayList<Connection>();
List<APIConnection> restConnections = new ArrayList<APIConnection>();
for(org.glyptodon.guacamole.net.auth.Connection connection : connections) {
restConnections.add(new Connection(connection));
restConnections.add(new APIConnection(connection));
}
return restConnections;