mirror of
				https://github.com/gyurix1968/guacamole-client.git
				synced 2025-10-31 09:03:21 +00:00 
			
		
		
		
	Ticket #362: Cleaned up a bit and converted error throwing to new HTTPException.
This commit is contained in:
		| @@ -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()); | ||||
|     } | ||||
|      | ||||
| } | ||||
| @@ -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; | ||||
|     } | ||||
|   | ||||
| @@ -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(); | ||||
|          | ||||
|   | ||||
| @@ -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."); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -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)); | ||||
|         } | ||||
|              | ||||
|   | ||||
		Reference in New Issue
	
	Block a user