GUACAMOLE-5: Refactor HistoryRESTService as a resource.

This commit is contained in:
Michael Jumper
2016-07-12 13:43:37 -07:00
parent 3f7f6e6d5b
commit 3658b91fe1
3 changed files with 28 additions and 38 deletions

View File

@@ -36,7 +36,6 @@ import org.apache.guacamole.rest.auth.SecureRandomAuthTokenGenerator;
import org.apache.guacamole.rest.auth.TokenSessionMap; import org.apache.guacamole.rest.auth.TokenSessionMap;
import org.apache.guacamole.rest.connection.ConnectionModule; import org.apache.guacamole.rest.connection.ConnectionModule;
import org.apache.guacamole.rest.connectiongroup.ConnectionGroupModule; import org.apache.guacamole.rest.connectiongroup.ConnectionGroupModule;
import org.apache.guacamole.rest.history.HistoryRESTService;
import org.apache.guacamole.rest.language.LanguageRESTService; import org.apache.guacamole.rest.language.LanguageRESTService;
import org.apache.guacamole.rest.patch.PatchRESTService; import org.apache.guacamole.rest.patch.PatchRESTService;
import org.apache.guacamole.rest.schema.SchemaRESTService; import org.apache.guacamole.rest.schema.SchemaRESTService;
@@ -88,7 +87,6 @@ public class RESTServiceModule extends ServletModule {
bind(ObjectRetrievalService.class); bind(ObjectRetrievalService.class);
// Set up the API endpoints // Set up the API endpoints
bind(HistoryRESTService.class);
bind(LanguageRESTService.class); bind(LanguageRESTService.class);
bind(PatchRESTService.class); bind(PatchRESTService.class);
bind(SchemaRESTService.class); bind(SchemaRESTService.class);

View File

@@ -19,13 +19,11 @@
package org.apache.guacamole.rest.history; package org.apache.guacamole.rest.history;
import com.google.inject.Inject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
@@ -33,27 +31,16 @@ import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.auth.ConnectionRecord; import org.apache.guacamole.net.auth.ConnectionRecord;
import org.apache.guacamole.net.auth.ConnectionRecordSet; import org.apache.guacamole.net.auth.ConnectionRecordSet;
import org.apache.guacamole.net.auth.UserContext; import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.GuacamoleSession;
import org.apache.guacamole.rest.ObjectRetrievalService;
import org.apache.guacamole.rest.auth.AuthenticationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* A REST Service for retrieving and managing the history records of Guacamole * A REST resource for retrieving and managing the history records of Guacamole
* objects. * objects.
* *
* @author Michael Jumper * @author Michael Jumper
*/ */
@Path("/data/{dataSource}/history")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
public class HistoryRESTService { public class HistoryResource {
/**
* Logger for this class.
*/
private static final Logger logger = LoggerFactory.getLogger(HistoryRESTService.class);
/** /**
* The maximum number of history records to return in any one response. * The maximum number of history records to return in any one response.
@@ -61,30 +48,25 @@ public class HistoryRESTService {
private static final int MAXIMUM_HISTORY_SIZE = 1000; private static final int MAXIMUM_HISTORY_SIZE = 1000;
/** /**
* A service for authenticating users from auth tokens. * The UserContext whose associated connection history is being exposed.
*/ */
@Inject private final UserContext userContext;
private AuthenticationService authenticationService;
/** /**
* Service for convenient retrieval of objects. * Creates a new HistoryResource which exposes the connection history
* associated with the given UserContext.
*
* @param userContext
* The UserContext whose connection history should be exposed.
*/ */
@Inject public HistoryResource(UserContext userContext) {
private ObjectRetrievalService retrievalService; this.userContext = userContext;
}
/** /**
* Retrieves the usage history for all connections, restricted by optional * Retrieves the usage history for all connections, restricted by optional
* filter parameters. * filter parameters.
* *
* @param authToken
* The authentication token that is used to authenticate the user
* performing the operation.
*
* @param authProviderIdentifier
* The unique identifier of the AuthenticationProvider associated with
* the UserContext containing the connection whose history is to be
* retrieved.
*
* @param requiredContents * @param requiredContents
* The set of strings that each must occur somewhere within the * The set of strings that each must occur somewhere within the
* returned connection records, whether within the associated username, * returned connection records, whether within the associated username,
@@ -105,16 +87,12 @@ public class HistoryRESTService {
* If an error occurs while retrieving the connection history. * If an error occurs while retrieving the connection history.
*/ */
@GET @GET
@Path("/connections") @Path("connections")
public List<APIConnectionRecord> getConnectionHistory(@QueryParam("token") String authToken, public List<APIConnectionRecord> getConnectionHistory(
@PathParam("dataSource") String authProviderIdentifier,
@QueryParam("contains") List<String> requiredContents, @QueryParam("contains") List<String> requiredContents,
@QueryParam("order") List<APIConnectionRecordSortPredicate> sortPredicates) @QueryParam("order") List<APIConnectionRecordSortPredicate> sortPredicates)
throws GuacamoleException { throws GuacamoleException {
GuacamoleSession session = authenticationService.getGuacamoleSession(authToken);
UserContext userContext = retrievalService.retrieveUserContext(session, authProviderIdentifier);
// Retrieve overall connection history // Retrieve overall connection history
ConnectionRecordSet history = userContext.getConnectionHistory(); ConnectionRecordSet history = userContext.getConnectionHistory();

View File

@@ -37,6 +37,7 @@ import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.rest.activeconnection.APIActiveConnection; import org.apache.guacamole.rest.activeconnection.APIActiveConnection;
import org.apache.guacamole.rest.connection.APIConnection; import org.apache.guacamole.rest.connection.APIConnection;
import org.apache.guacamole.rest.connectiongroup.APIConnectionGroup; import org.apache.guacamole.rest.connectiongroup.APIConnectionGroup;
import org.apache.guacamole.rest.history.HistoryResource;
import org.apache.guacamole.rest.user.APIUser; import org.apache.guacamole.rest.user.APIUser;
/** /**
@@ -169,4 +170,17 @@ public class UserContextResource {
userContext.getUserDirectory()); userContext.getUserDirectory());
} }
/**
* Returns a new resource which represents historical data contained
* within the UserContext exposed by this UserContextResource.
*
* @return
* A new resource which represents the historical data contained within
* the UserContext exposed by this UserContextResource.
*/
@Path("history")
public HistoryResource getHistoryResource() {
return new HistoryResource(userContext);
}
} }