From 3658b91fe18e386b10d251922610c03075cc55a3 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 12 Jul 2016 13:43:37 -0700 Subject: [PATCH] GUACAMOLE-5: Refactor HistoryRESTService as a resource. --- .../guacamole/rest/RESTServiceModule.java | 2 - ...yRESTService.java => HistoryResource.java} | 50 ++++++------------- .../rest/session/UserContextResource.java | 14 ++++++ 3 files changed, 28 insertions(+), 38 deletions(-) rename guacamole/src/main/java/org/apache/guacamole/rest/history/{HistoryRESTService.java => HistoryResource.java} (71%) diff --git a/guacamole/src/main/java/org/apache/guacamole/rest/RESTServiceModule.java b/guacamole/src/main/java/org/apache/guacamole/rest/RESTServiceModule.java index dbdcaba94..c6de0dc29 100644 --- a/guacamole/src/main/java/org/apache/guacamole/rest/RESTServiceModule.java +++ b/guacamole/src/main/java/org/apache/guacamole/rest/RESTServiceModule.java @@ -36,7 +36,6 @@ import org.apache.guacamole.rest.auth.SecureRandomAuthTokenGenerator; import org.apache.guacamole.rest.auth.TokenSessionMap; import org.apache.guacamole.rest.connection.ConnectionModule; 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.patch.PatchRESTService; import org.apache.guacamole.rest.schema.SchemaRESTService; @@ -88,7 +87,6 @@ public class RESTServiceModule extends ServletModule { bind(ObjectRetrievalService.class); // Set up the API endpoints - bind(HistoryRESTService.class); bind(LanguageRESTService.class); bind(PatchRESTService.class); bind(SchemaRESTService.class); diff --git a/guacamole/src/main/java/org/apache/guacamole/rest/history/HistoryRESTService.java b/guacamole/src/main/java/org/apache/guacamole/rest/history/HistoryResource.java similarity index 71% rename from guacamole/src/main/java/org/apache/guacamole/rest/history/HistoryRESTService.java rename to guacamole/src/main/java/org/apache/guacamole/rest/history/HistoryResource.java index fd3738ed1..d71d44555 100644 --- a/guacamole/src/main/java/org/apache/guacamole/rest/history/HistoryRESTService.java +++ b/guacamole/src/main/java/org/apache/guacamole/rest/history/HistoryResource.java @@ -19,13 +19,11 @@ package org.apache.guacamole.rest.history; -import com.google.inject.Inject; import java.util.ArrayList; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; 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.ConnectionRecordSet; 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. * * @author Michael Jumper */ -@Path("/data/{dataSource}/history") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class HistoryRESTService { - - /** - * Logger for this class. - */ - private static final Logger logger = LoggerFactory.getLogger(HistoryRESTService.class); +public class HistoryResource { /** * 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; /** - * A service for authenticating users from auth tokens. + * The UserContext whose associated connection history is being exposed. */ - @Inject - private AuthenticationService authenticationService; + private final UserContext userContext; /** - * 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 - private ObjectRetrievalService retrievalService; + public HistoryResource(UserContext userContext) { + this.userContext = userContext; + } /** * Retrieves the usage history for all connections, restricted by optional * 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 * The set of strings that each must occur somewhere within the * returned connection records, whether within the associated username, @@ -105,16 +87,12 @@ public class HistoryRESTService { * If an error occurs while retrieving the connection history. */ @GET - @Path("/connections") - public List getConnectionHistory(@QueryParam("token") String authToken, - @PathParam("dataSource") String authProviderIdentifier, + @Path("connections") + public List getConnectionHistory( @QueryParam("contains") List requiredContents, @QueryParam("order") List sortPredicates) throws GuacamoleException { - GuacamoleSession session = authenticationService.getGuacamoleSession(authToken); - UserContext userContext = retrievalService.retrieveUserContext(session, authProviderIdentifier); - // Retrieve overall connection history ConnectionRecordSet history = userContext.getConnectionHistory(); diff --git a/guacamole/src/main/java/org/apache/guacamole/rest/session/UserContextResource.java b/guacamole/src/main/java/org/apache/guacamole/rest/session/UserContextResource.java index c9ff670e2..81344c647 100644 --- a/guacamole/src/main/java/org/apache/guacamole/rest/session/UserContextResource.java +++ b/guacamole/src/main/java/org/apache/guacamole/rest/session/UserContextResource.java @@ -37,6 +37,7 @@ import org.apache.guacamole.net.auth.UserContext; import org.apache.guacamole.rest.activeconnection.APIActiveConnection; import org.apache.guacamole.rest.connection.APIConnection; import org.apache.guacamole.rest.connectiongroup.APIConnectionGroup; +import org.apache.guacamole.rest.history.HistoryResource; import org.apache.guacamole.rest.user.APIUser; /** @@ -169,4 +170,17 @@ public class UserContextResource { 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); + } + }