mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 21:27:40 +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 com.google.inject.Inject;
|
||||||
import javax.ws.rs.WebApplicationException;
|
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.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.
|
* A service for performing authentication checks in REST endpoints.
|
||||||
@@ -54,9 +54,7 @@ public class AuthenticationService {
|
|||||||
|
|
||||||
// Authentication failed.
|
// Authentication failed.
|
||||||
if(userContext == null)
|
if(userContext == null)
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
|
||||||
Response.status(Response.Status.UNAUTHORIZED)
|
|
||||||
.entity(new APIError("Permission Denied.")).build());
|
|
||||||
|
|
||||||
return userContext;
|
return userContext;
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
package org.glyptodon.guacamole.net.basic.rest.auth;
|
package org.glyptodon.guacamole.net.basic.rest.auth;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
@@ -10,11 +8,13 @@ import javax.ws.rs.QueryParam;
|
|||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.Response.Status;
|
||||||
import org.glyptodon.guacamole.GuacamoleException;
|
import org.glyptodon.guacamole.GuacamoleException;
|
||||||
import org.glyptodon.guacamole.net.auth.AuthenticationProvider;
|
import org.glyptodon.guacamole.net.auth.AuthenticationProvider;
|
||||||
import org.glyptodon.guacamole.net.auth.Credentials;
|
import org.glyptodon.guacamole.net.auth.Credentials;
|
||||||
import org.glyptodon.guacamole.net.auth.UserContext;
|
import org.glyptodon.guacamole.net.auth.UserContext;
|
||||||
import org.glyptodon.guacamole.net.basic.rest.APIError;
|
import org.glyptodon.guacamole.net.basic.rest.APIError;
|
||||||
|
import org.glyptodon.guacamole.net.basic.rest.HTTPException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -94,15 +94,13 @@ public class LoginRESTService {
|
|||||||
userContext = authProvider.getUserContext(credentials);
|
userContext = authProvider.getUserContext(credentials);
|
||||||
} catch(GuacamoleException e) {
|
} catch(GuacamoleException e) {
|
||||||
logger.error("Exception caught while authenticating user.", e);
|
logger.error("Exception caught while authenticating user.", e);
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.INTERNAL_SERVER_ERROR,
|
||||||
Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).build());
|
"Unexpected server error.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// authentication failed.
|
// authentication failed.
|
||||||
if(userContext == null)
|
if(userContext == null)
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
|
||||||
Response.status(Response.Status.UNAUTHORIZED)
|
|
||||||
.entity(new APIError("Permission Denied.")).build());
|
|
||||||
|
|
||||||
String authToken = authTokenGenerator.getToken();
|
String authToken = authTokenGenerator.getToken();
|
||||||
|
|
||||||
|
@@ -29,6 +29,7 @@ import javax.ws.rs.QueryParam;
|
|||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.Response.Status;
|
||||||
import org.glyptodon.guacamole.GuacamoleException;
|
import org.glyptodon.guacamole.GuacamoleException;
|
||||||
import org.glyptodon.guacamole.GuacamoleSecurityException;
|
import org.glyptodon.guacamole.GuacamoleSecurityException;
|
||||||
import org.glyptodon.guacamole.net.auth.Connection;
|
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.Directory;
|
||||||
import org.glyptodon.guacamole.net.auth.UserContext;
|
import org.glyptodon.guacamole.net.auth.UserContext;
|
||||||
import org.glyptodon.guacamole.net.basic.rest.APIError;
|
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;
|
import org.glyptodon.guacamole.net.basic.rest.auth.AuthenticationService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -85,10 +87,8 @@ public class ConnectionRESTService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(parentConnectionGroup == null)
|
if(parentConnectionGroup == null)
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.BAD_REQUEST,
|
||||||
Response.status(Response.Status.BAD_REQUEST)
|
"No ConnectionGroup found with the provided parentID.");
|
||||||
.entity(new APIError("No ConnectionGroup found with the provided parentID."))
|
|
||||||
.build());
|
|
||||||
|
|
||||||
Directory<String, Connection> connectionDirectory =
|
Directory<String, Connection> connectionDirectory =
|
||||||
parentConnectionGroup.getConnectionDirectory();
|
parentConnectionGroup.getConnectionDirectory();
|
||||||
@@ -103,13 +103,10 @@ public class ConnectionRESTService {
|
|||||||
|
|
||||||
return connectionService.convertConnectionList(connections);
|
return connectionService.convertConnectionList(connections);
|
||||||
} catch(GuacamoleSecurityException e) {
|
} catch(GuacamoleSecurityException e) {
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
|
||||||
Response.status(Response.Status.UNAUTHORIZED)
|
|
||||||
.entity(new APIError("Permission Denied.")).build());
|
|
||||||
} catch(GuacamoleException e) {
|
} catch(GuacamoleException e) {
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.INTERNAL_SERVER_ERROR,
|
||||||
Response.status(Response.Status.INTERNAL_SERVER_ERROR)
|
"Unexpected server error.");
|
||||||
.entity(e).build());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +124,6 @@ public class ConnectionRESTService {
|
|||||||
UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken);
|
UserContext userContext = authenticationService.getUserContextFromAuthToken(authToken);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// Get the connection directory
|
// Get the connection directory
|
||||||
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
|
ConnectionGroup rootGroup = userContext.getRootConnectionGroup();
|
||||||
Directory<String, Connection> connectionDirectory =
|
Directory<String, Connection> connectionDirectory =
|
||||||
@@ -137,20 +133,15 @@ public class ConnectionRESTService {
|
|||||||
Connection connection = connectionDirectory.get(connectionID);
|
Connection connection = connectionDirectory.get(connectionID);
|
||||||
|
|
||||||
if(connection == null)
|
if(connection == null)
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.BAD_REQUEST,
|
||||||
Response.status(Response.Status.BAD_REQUEST)
|
"No Connection found with the provided ID.");
|
||||||
.entity(new APIError("No Connection found with the provided ID."))
|
|
||||||
.build());
|
|
||||||
|
|
||||||
return new APIConnection(connection);
|
return new APIConnection(connection);
|
||||||
} catch(GuacamoleSecurityException e) {
|
} catch(GuacamoleSecurityException e) {
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.UNAUTHORIZED, "Permission Denied.");
|
||||||
Response.status(Response.Status.UNAUTHORIZED)
|
|
||||||
.entity(new APIError("Permission Denied.")).build());
|
|
||||||
} catch(GuacamoleException e) {
|
} catch(GuacamoleException e) {
|
||||||
throw new WebApplicationException(
|
throw new HTTPException(Status.INTERNAL_SERVER_ERROR,
|
||||||
Response.status(Response.Status.INTERNAL_SERVER_ERROR)
|
"Unexpected server error.");
|
||||||
.entity(e).build());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@ package org.glyptodon.guacamole.net.basic.rest.connection;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.glyptodon.guacamole.GuacamoleException;
|
import org.glyptodon.guacamole.GuacamoleException;
|
||||||
|
import org.glyptodon.guacamole.net.auth.Connection;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Guacamole - Clientless Remote Desktop
|
* Guacamole - Clientless Remote Desktop
|
||||||
@@ -30,20 +31,19 @@ import org.glyptodon.guacamole.GuacamoleException;
|
|||||||
public class ConnectionService {
|
public class ConnectionService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a list of org.glyptodon.guacamole.net.auth.APIConnection to
|
* Converts a list of Connection to a list of APIConnection objects for
|
||||||
* APIConnection objects for exposure with the REST endpoints.
|
* exposing with the REST endpoints.
|
||||||
*
|
*
|
||||||
* @param connections The org.glyptodon.guacamole.net.auth.APIConnection to
|
* @param connections The Connection to convert for REST endpoint use.
|
||||||
* convert for REST endpoint use.
|
|
||||||
* @return A List of APIConnection 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
|
* @throws GuacamoleException If an error occurs while converting the
|
||||||
* connections.
|
* connections.
|
||||||
*/
|
*/
|
||||||
public List<APIConnection> convertConnectionList(List<? extends org.glyptodon.guacamole.net.auth.Connection> connections)
|
public List<APIConnection> convertConnectionList(List<? extends Connection> connections)
|
||||||
throws GuacamoleException {
|
throws GuacamoleException {
|
||||||
List<APIConnection> restConnections = new ArrayList<APIConnection>();
|
List<APIConnection> restConnections = new ArrayList<APIConnection>();
|
||||||
|
|
||||||
for(org.glyptodon.guacamole.net.auth.Connection connection : connections) {
|
for(Connection connection : connections) {
|
||||||
restConnections.add(new APIConnection(connection));
|
restConnections.add(new APIConnection(connection));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user