From 2414c9a2457ae57cf3df30f1d40535d1895525fb Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 12 Sep 2017 13:56:58 -0700 Subject: [PATCH] GUACAMOLE-394: Separate core of ConnectionRecordSet into ModeledActivityRecordSet. --- .../jdbc/base/ModeledActivityRecordSet.java | 132 ++++++++++++++++++ .../jdbc/connection/ConnectionRecordSet.java | 67 ++------- 2 files changed, 141 insertions(+), 58 deletions(-) create mode 100644 extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledActivityRecordSet.java diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledActivityRecordSet.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledActivityRecordSet.java new file mode 100644 index 000000000..d2590184b --- /dev/null +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledActivityRecordSet.java @@ -0,0 +1,132 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.guacamole.auth.jdbc.base; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.guacamole.GuacamoleException; +import org.apache.guacamole.net.auth.ActivityRecord; +import org.apache.guacamole.net.auth.ActivityRecordSet; +import org.apache.guacamole.net.auth.ActivityRecordSet.SortableProperty; +import org.apache.guacamole.net.auth.AuthenticatedUser; + +/** + * A JDBC implementation of ActivityRecordSet. Calls to asCollection() will + * query history records using an implementation-specific mechanism. Which + * records are returned will be determined by the values passed in earlier. + * + * @param + * The type of ActivityRecord contained within this set. + */ +public abstract class ModeledActivityRecordSet + extends RestrictedObject implements ActivityRecordSet { + + /** + * The set of strings that each must occur somewhere within the returned + * records, whether within the associated username, an associated date, or + * other related data. If non-empty, any record not matching each of the + * strings within the collection will be excluded from the results. + */ + private final Set requiredContents = + new HashSet(); + + /** + * The maximum number of history records that should be returned by a call + * to asCollection(). + */ + private int limit = Integer.MAX_VALUE; + + /** + * A list of predicates to apply while sorting the resulting records, + * describing the properties involved and the sort order for those + * properties. + */ + private final List sortPredicates = + new ArrayList(); + + /** + * Retrieves the history records matching the given criteria. Retrieves up + * to limit history records matching the given terms and sorted + * by the given predicates. Only history records associated with data that + * the given user can read are returned. + * + * @param user + * The user retrieving the history. + * + * @param requiredContents + * The search terms that must be contained somewhere within each of the + * returned records. + * + * @param sortPredicates + * A list of predicates to sort the returned records by, in order of + * priority. + * + * @param limit + * The maximum number of records that should be returned. + * + * @return + * A collection of all history records matching the given criteria. + * + * @throws GuacamoleException + * If permission to read the history records is denied. + */ + protected abstract Collection retrieveHistory( + AuthenticatedUser user, + Set requiredContents, + List sortPredicates, + int limit) throws GuacamoleException; + + @Override + public Collection asCollection() + throws GuacamoleException { + return retrieveHistory(getCurrentUser(), requiredContents, + sortPredicates, limit); + } + + @Override + public ModeledActivityRecordSet contains(String value) + throws GuacamoleException { + requiredContents.add(new ActivityRecordSearchTerm(value)); + return this; + } + + @Override + public ModeledActivityRecordSet limit(int limit) throws GuacamoleException { + this.limit = Math.min(this.limit, limit); + return this; + } + + @Override + public ModeledActivityRecordSet sort(SortableProperty property, boolean desc) + throws GuacamoleException { + + sortPredicates.add(new ActivityRecordSortPredicate( + property, + desc + )); + + return this; + + } + +} diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordSet.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordSet.java index df2a0a9ea..f4574f4e0 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordSet.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/connection/ConnectionRecordSet.java @@ -20,17 +20,14 @@ package org.apache.guacamole.auth.jdbc.connection; import com.google.inject.Inject; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.auth.jdbc.base.ActivityRecordSearchTerm; import org.apache.guacamole.auth.jdbc.base.ActivityRecordSortPredicate; -import org.apache.guacamole.auth.jdbc.base.RestrictedObject; -import org.apache.guacamole.net.auth.ActivityRecordSet; -import org.apache.guacamole.net.auth.ActivityRecordSet.SortableProperty; +import org.apache.guacamole.auth.jdbc.base.ModeledActivityRecordSet; +import org.apache.guacamole.net.auth.AuthenticatedUser; import org.apache.guacamole.net.auth.ConnectionRecord; /** @@ -38,8 +35,7 @@ import org.apache.guacamole.net.auth.ConnectionRecord; * asCollection() will query connection history records from the database. Which * records are returned will be determined by the values passed in earlier. */ -public class ConnectionRecordSet extends RestrictedObject - implements ActivityRecordSet { +public class ConnectionRecordSet extends ModeledActivityRecordSet { /** * Service for managing connection objects. @@ -47,60 +43,15 @@ public class ConnectionRecordSet extends RestrictedObject @Inject private ConnectionService connectionService; - /** - * The set of strings that each must occur somewhere within the returned - * connection records, whether within the associated username, the name of - * the associated connection, or any associated date. If non-empty, any - * connection record not matching each of the strings within the collection - * will be excluded from the results. - */ - private final Set requiredContents = - new HashSet(); - - /** - * The maximum number of connection history records that should be returned - * by a call to asCollection(). - */ - private int limit = Integer.MAX_VALUE; - - /** - * A list of predicates to apply while sorting the resulting connection - * records, describing the properties involved and the sort order for those - * properties. - */ - private final List connectionRecordSortPredicates = - new ArrayList(); - @Override - public Collection asCollection() + protected Collection retrieveHistory( + AuthenticatedUser user, Set requiredContents, + List sortPredicates, int limit) throws GuacamoleException { + + // Retrieve history from database return connectionService.retrieveHistory(getCurrentUser(), - requiredContents, connectionRecordSortPredicates, limit); - } - - @Override - public ConnectionRecordSet contains(String value) - throws GuacamoleException { - requiredContents.add(new ActivityRecordSearchTerm(value)); - return this; - } - - @Override - public ConnectionRecordSet limit(int limit) throws GuacamoleException { - this.limit = Math.min(this.limit, limit); - return this; - } - - @Override - public ConnectionRecordSet sort(SortableProperty property, boolean desc) - throws GuacamoleException { - - connectionRecordSortPredicates.add(new ActivityRecordSortPredicate( - property, - desc - )); - - return this; + requiredContents, sortPredicates, limit); }