GUAC-1161: Generalize HTTPException to APIException.

This commit is contained in:
Michael Jumper
2015-04-20 14:51:29 -07:00
parent 74883ae121
commit 1010552c69
4 changed files with 24 additions and 24 deletions

View File

@@ -28,29 +28,29 @@ import javax.ws.rs.core.Response;
import org.glyptodon.guacamole.form.Parameter; import org.glyptodon.guacamole.form.Parameter;
/** /**
* An exception that will result in the given HTTP Status and error being * An exception that will result in the given error error information being
* returned from the API layer. All error messages have the same format which * returned from the API layer. All error messages have the same format which
* is defined by APIError. * is defined by APIError.
* *
* @author James Muehlner * @author James Muehlner
* @author Michael Jumper * @author Michael Jumper
*/ */
public class HTTPException extends WebApplicationException { public class APIException extends WebApplicationException {
/** /**
* Construct a new HTTPException with the given error. All information * Construct a new APIException with the given error. All information
* associated with this new exception will be extracted from the given * associated with this new exception will be extracted from the given
* APIError. * APIError.
* *
* @param error * @param error
* The error that occurred. * The error that occurred.
*/ */
public HTTPException(APIError error) { public APIException(APIError error) {
super(Response.status(error.getType().getStatus()).entity(error).build()); super(Response.status(error.getType().getStatus()).entity(error).build());
} }
/** /**
* Creates a new HTTPException with the given type and message. The * Creates a new APIException with the given type and message. The
* corresponding APIError will be created from the provided information. * corresponding APIError will be created from the provided information.
* *
* @param type * @param type
@@ -59,14 +59,14 @@ public class HTTPException extends WebApplicationException {
* @param message * @param message
* A human-readable message describing the error. * A human-readable message describing the error.
*/ */
public HTTPException(APIError.Type type, String message) { public APIException(APIError.Type type, String message) {
this(new APIError(type, message)); this(new APIError(type, message));
} }
/** /**
* Creates a new HTTPException with the given type, message, and * Creates a new APIException with the given type, message, and parameter
* parameter information. The corresponding APIError will be created from * information. The corresponding APIError will be created from the
* the provided information. * provided information.
* *
* @param type * @param type
* The type of error that occurred. * The type of error that occurred.
@@ -78,7 +78,7 @@ public class HTTPException extends WebApplicationException {
* All parameters expected in the original request, or now required as * All parameters expected in the original request, or now required as
* a result of the original request. * a result of the original request.
*/ */
public HTTPException(APIError.Type type, String message, Collection<Parameter> expected) { public APIException(APIError.Type type, String message, Collection<Parameter> expected) {
this(new APIError(type, message, expected)); this(new APIError(type, message, expected));
} }

View File

@@ -62,7 +62,7 @@ public class AuthProviderRESTExceptionWrapper implements MethodInterceptor {
if (message == null) if (message == null)
message = "Permission denied."; message = "Permission denied.";
throw new HTTPException( throw new APIException(
APIError.Type.INSUFFICIENT_CREDENTIALS, APIError.Type.INSUFFICIENT_CREDENTIALS,
message, message,
e.getCredentialsInfo().getParameters() e.getCredentialsInfo().getParameters()
@@ -77,7 +77,7 @@ public class AuthProviderRESTExceptionWrapper implements MethodInterceptor {
if (message == null) if (message == null)
message = "Permission denied."; message = "Permission denied.";
throw new HTTPException( throw new APIException(
APIError.Type.INVALID_CREDENTIALS, APIError.Type.INVALID_CREDENTIALS,
message, message,
e.getCredentialsInfo().getParameters() e.getCredentialsInfo().getParameters()
@@ -92,7 +92,7 @@ public class AuthProviderRESTExceptionWrapper implements MethodInterceptor {
if (message == null) if (message == null)
message = "Permission denied."; message = "Permission denied.";
throw new HTTPException( throw new APIException(
APIError.Type.PERMISSION_DENIED, APIError.Type.PERMISSION_DENIED,
message message
); );
@@ -107,7 +107,7 @@ public class AuthProviderRESTExceptionWrapper implements MethodInterceptor {
if (message == null) if (message == null)
message = "Not found."; message = "Not found.";
throw new HTTPException( throw new APIException(
APIError.Type.NOT_FOUND, APIError.Type.NOT_FOUND,
message message
); );
@@ -122,7 +122,7 @@ public class AuthProviderRESTExceptionWrapper implements MethodInterceptor {
if (message == null) if (message == null)
message = "Invalid request."; message = "Invalid request.";
throw new HTTPException( throw new APIException(
APIError.Type.BAD_REQUEST, APIError.Type.BAD_REQUEST,
message message
); );
@@ -138,7 +138,7 @@ public class AuthProviderRESTExceptionWrapper implements MethodInterceptor {
message = "Unexpected server error."; message = "Unexpected server error.";
logger.debug("Unexpected exception in REST endpoint.", e); logger.debug("Unexpected exception in REST endpoint.", e);
throw new HTTPException( throw new APIException(
APIError.Type.INTERNAL_ERROR, APIError.Type.INTERNAL_ERROR,
message message
); );

View File

@@ -46,7 +46,7 @@ import org.glyptodon.guacamole.net.basic.GuacamoleSession;
import org.glyptodon.guacamole.net.basic.rest.APIError; import org.glyptodon.guacamole.net.basic.rest.APIError;
import org.glyptodon.guacamole.net.basic.rest.APIRequest; import org.glyptodon.guacamole.net.basic.rest.APIRequest;
import org.glyptodon.guacamole.net.basic.rest.AuthProviderRESTExposure; import org.glyptodon.guacamole.net.basic.rest.AuthProviderRESTExposure;
import org.glyptodon.guacamole.net.basic.rest.HTTPException; import org.glyptodon.guacamole.net.basic.rest.APIException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -290,7 +290,7 @@ public class TokenRESTService {
GuacamoleSession session = tokenSessionMap.remove(authToken); GuacamoleSession session = tokenSessionMap.remove(authToken);
if (session == null) if (session == null)
throw new HTTPException(APIError.Type.NOT_FOUND, "No such token."); throw new APIException(APIError.Type.NOT_FOUND, "No such token.");
session.invalidate(); session.invalidate();

View File

@@ -56,7 +56,7 @@ import org.glyptodon.guacamole.net.basic.rest.APIPatch;
import static org.glyptodon.guacamole.net.basic.rest.APIPatch.Operation.add; import static org.glyptodon.guacamole.net.basic.rest.APIPatch.Operation.add;
import static org.glyptodon.guacamole.net.basic.rest.APIPatch.Operation.remove; import static org.glyptodon.guacamole.net.basic.rest.APIPatch.Operation.remove;
import org.glyptodon.guacamole.net.basic.rest.AuthProviderRESTExposure; import org.glyptodon.guacamole.net.basic.rest.AuthProviderRESTExposure;
import org.glyptodon.guacamole.net.basic.rest.HTTPException; import org.glyptodon.guacamole.net.basic.rest.APIException;
import org.glyptodon.guacamole.net.basic.rest.ObjectRetrievalService; import org.glyptodon.guacamole.net.basic.rest.ObjectRetrievalService;
import org.glyptodon.guacamole.net.basic.rest.PATCH; import org.glyptodon.guacamole.net.basic.rest.PATCH;
import org.glyptodon.guacamole.net.basic.rest.auth.AuthenticationService; import org.glyptodon.guacamole.net.basic.rest.auth.AuthenticationService;
@@ -275,12 +275,12 @@ public class UserRESTService {
// Validate data and path are sane // Validate data and path are sane
if (!user.getUsername().equals(username)) if (!user.getUsername().equals(username))
throw new HTTPException(APIError.Type.BAD_REQUEST, throw new APIException(APIError.Type.BAD_REQUEST,
"Username in path does not match username provided JSON data."); "Username in path does not match username provided JSON data.");
// A user may not use this endpoint to modify himself // A user may not use this endpoint to modify himself
if (userContext.self().getIdentifier().equals(user.getUsername())) { if (userContext.self().getIdentifier().equals(user.getUsername())) {
throw new HTTPException(APIError.Type.PERMISSION_DENIED, throw new APIException(APIError.Type.PERMISSION_DENIED,
"Permission denied."); "Permission denied.");
} }
@@ -335,7 +335,7 @@ public class UserRESTService {
// Verify that the old password was correct // Verify that the old password was correct
if (authProvider.getUserContext(credentials) == null) { if (authProvider.getUserContext(credentials) == null) {
throw new HTTPException(APIError.Type.PERMISSION_DENIED, throw new APIException(APIError.Type.PERMISSION_DENIED,
"Permission denied."); "Permission denied.");
} }
@@ -466,7 +466,7 @@ public class UserRESTService {
// Unsupported patch operation // Unsupported patch operation
default: default:
throw new HTTPException(APIError.Type.BAD_REQUEST, throw new APIException(APIError.Type.BAD_REQUEST,
"Unsupported patch operation: \"" + operation + "\""); "Unsupported patch operation: \"" + operation + "\"");
} }
@@ -585,7 +585,7 @@ public class UserRESTService {
// Otherwise, the path is not supported // Otherwise, the path is not supported
else else
throw new HTTPException(APIError.Type.BAD_REQUEST, "Unsupported patch path: \"" + path + "\""); throw new APIException(APIError.Type.BAD_REQUEST, "Unsupported patch path: \"" + path + "\"");
} // end for each patch operation } // end for each patch operation