diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/HTTPException.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/HTTPException.java new file mode 100644 index 000000000..11a4fd70d --- /dev/null +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/HTTPException.java @@ -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()); + } + +} diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/AuthenticationService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/AuthenticationService.java index ccf2a518f..c4c93cb11 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/AuthenticationService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/AuthenticationService.java @@ -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; } diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java index 7d79b4f65..720ef88ad 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/auth/LoginRESTService.java @@ -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(); 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 b0f99c35e..c52dc7359 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 @@ -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 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 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."); } } diff --git a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionService.java b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionService.java index 51513b494..710d23d8f 100644 --- a/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionService.java +++ b/guacamole/src/main/java/org/glyptodon/guacamole/net/basic/rest/connection/ConnectionService.java @@ -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 convertConnectionList(List connections) + public List convertConnectionList(List connections) throws GuacamoleException { List restConnections = new ArrayList(); - for(org.glyptodon.guacamole.net.auth.Connection connection : connections) { + for(Connection connection : connections) { restConnections.add(new APIConnection(connection)); }