Ticket #362: Cleaned up a bit and converted error throwing to new HTTPException.

This commit is contained in:
James Muehlner
2013-09-19 19:17:11 -07:00
parent 131aea1173
commit 614e6395fb
5 changed files with 66 additions and 39 deletions

View File

@@ -0,0 +1,40 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glyptodon.guacamole.net.basic.rest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
/**
* An exception that will result in the given HTTP Status and message or entity
* being returned from the API layer.
*
* @author James Muehlner
*/
public class HTTPException extends WebApplicationException {
/**
* Construct a new HTTPException with the given HTTP status and entity.
*
* @param status The HTTP Status to use for the response.
* @param entity The entity to use as the body of the response.
*/
public HTTPException(Status status, Object entity) {
super(Response.status(status).entity(entity).build());
}
/**
* Construct a new HTTPException with the given HTTP status and message. The
* message will be wrapped in an APIError container.
*
* @param status The HTTP Status to use for the response.
* @param entity The entity to wrap in an APIError as the body of the response.
*/
public HTTPException(Status status, String message) {
super(Response.status(status).entity(new APIError(message)).build());
}
}

View File

@@ -20,9 +20,9 @@ package org.glyptodon.guacamole.net.basic.rest.auth;
import com.google.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glyptodon.guacamole.net.auth.UserContext;
import org.glyptodon.guacamole.net.basic.rest.APIError;
import org.glyptodon.guacamole.net.basic.rest.HTTPException;
/**
* A service for performing authentication checks in REST endpoints.
@@ -54,9 +54,7 @@ public class AuthenticationService {
// Authentication failed.
if(userContext == null)
throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
return userContext;
}

View File

@@ -1,8 +1,6 @@
package org.glyptodon.guacamole.net.basic.rest.auth;
import com.google.inject.Inject;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@@ -10,11 +8,13 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.net.auth.AuthenticationProvider;
import org.glyptodon.guacamole.net.auth.Credentials;
import org.glyptodon.guacamole.net.auth.UserContext;
import org.glyptodon.guacamole.net.basic.rest.APIError;
import org.glyptodon.guacamole.net.basic.rest.HTTPException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -94,15 +94,13 @@ public class LoginRESTService {
userContext = authProvider.getUserContext(credentials);
} catch(GuacamoleException e) {
logger.error("Exception caught while authenticating user.", e);
throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).build());
throw new HTTPException(Status.INTERNAL_SERVER_ERROR,
"Unexpected server error.");
}
// authentication failed.
if(userContext == null)
throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
String authToken = authTokenGenerator.getToken();

View File

@@ -29,6 +29,7 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.GuacamoleSecurityException;
import org.glyptodon.guacamole.net.auth.Connection;
@@ -36,6 +37,7 @@ import org.glyptodon.guacamole.net.auth.ConnectionGroup;
import org.glyptodon.guacamole.net.auth.Directory;
import org.glyptodon.guacamole.net.auth.UserContext;
import org.glyptodon.guacamole.net.basic.rest.APIError;
import org.glyptodon.guacamole.net.basic.rest.HTTPException;
import org.glyptodon.guacamole.net.basic.rest.auth.AuthenticationService;
/**
@@ -85,10 +87,8 @@ public class ConnectionRESTService {
}
if(parentConnectionGroup == null)
throw new WebApplicationException(
Response.status(Response.Status.BAD_REQUEST)
.entity(new APIError("No ConnectionGroup found with the provided parentID."))
.build());
throw new HTTPException(Status.BAD_REQUEST,
"No ConnectionGroup found with the provided parentID.");
Directory<String, Connection> connectionDirectory =
parentConnectionGroup.getConnectionDirectory();
@@ -103,13 +103,10 @@ public class ConnectionRESTService {
return connectionService.convertConnectionList(connections);
} catch(GuacamoleSecurityException e) {
throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
} catch(GuacamoleException e) {
throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(e).build());
throw new HTTPException(Status.INTERNAL_SERVER_ERROR,
"Unexpected server error.");
}
}
@@ -127,7 +124,6 @@ public class ConnectionRESTService {
UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken);
try {
// Get the connection directory
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
Directory<String, Connection> connectionDirectory =
@@ -137,20 +133,15 @@ public class ConnectionRESTService {
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());
throw new HTTPException(Status.BAD_REQUEST,
"No Connection found with the provided ID.");
return new APIConnection(connection);
} catch(GuacamoleSecurityException e) {
throw new WebApplicationException(
Response.status(Response.Status.UNAUTHORIZED)
.entity(new APIError("Permission Denied.")).build());
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
} catch(GuacamoleException e) {
throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(e).build());
throw new HTTPException(Status.INTERNAL_SERVER_ERROR,
"Unexpected server error.");
}
}

View File

@@ -3,6 +3,7 @@ package org.glyptodon.guacamole.net.basic.rest.connection;
import java.util.ArrayList;
import java.util.List;
import org.glyptodon.guacamole.GuacamoleException;
import org.glyptodon.guacamole.net.auth.Connection;
/*
* Guacamole - Clientless Remote Desktop
@@ -30,20 +31,19 @@ import org.glyptodon.guacamole.GuacamoleException;
public class ConnectionService {
/**
* Converts a list of org.glyptodon.guacamole.net.auth.APIConnection to
* APIConnection objects for exposure with the REST endpoints.
* Converts a list of Connection to a list of APIConnection objects for
* exposing with the REST endpoints.
*
* @param connections The org.glyptodon.guacamole.net.auth.APIConnection to
* convert for REST endpoint use.
* @param connections The Connection to convert for REST endpoint use.
* @return A List of APIConnection objects for use with the REST endpoint.
* @throws GuacamoleException If an error occurs while converting the
* connections.
*/
public List<APIConnection> convertConnectionList(List<? extends org.glyptodon.guacamole.net.auth.Connection> connections)
public List<APIConnection> convertConnectionList(List<? extends Connection> connections)
throws GuacamoleException {
List<APIConnection> restConnections = new ArrayList<APIConnection>();
for(org.glyptodon.guacamole.net.auth.Connection connection : connections) {
for(Connection connection : connections) {
restConnections.add(new APIConnection(connection));
}