GUACAMOLE-1224: Add CRUD-type events for Directory modifications.

This commit is contained in:
Michael Jumper
2022-09-20 17:07:17 -07:00
parent 0af17df712
commit 417587259f
27 changed files with 1022 additions and 176 deletions

View File

@@ -34,6 +34,104 @@ import org.apache.guacamole.GuacamoleException;
*/
public interface Directory<ObjectType extends Identifiable> {
/**
* All Directory types that may be found on the {@link UserContext}
* interface.
*/
public enum Type {
/**
* The type of a Directory that contains {@link ActiveConnection}
* objects.
*/
ACTIVE_CONNECTION(ActiveConnection.class),
/**
* The type of a Directory that contains {@link Connection}
* objects.
*/
CONNECTION(Connection.class),
/**
* The type of a Directory that contains {@link ConnectionGroup}
* objects.
*/
CONNECTION_GROUP(ConnectionGroup.class),
/**
* The type of a Directory that contains {@link SharingProfile}
* objects.
*/
SHARING_PROFILE(SharingProfile.class),
/**
* The type of a Directory that contains {@link User} objects.
*/
USER(User.class),
/**
* The type of a Directory that contains {@link UserGroup}
* objects.
*/
USER_GROUP(UserGroup.class);
/**
* The base class of the type of object stored within the type of
* Directory represented by this Directory.Type.
*/
private final Class<? extends Identifiable> objectType;
/**
* Creates a new Directory.Type representing the type of a Directory
* that contains only subclasses of the given class.
*
* @param objectType
* The base class of the type of object stored within the type of
* Directory represented by this Directory.Type.
*/
private Type(Class<? extends Identifiable> objectType) {
this.objectType = objectType;
}
/**
* Returns the base class of the type of object stored within a
* {@link Directory} of this type.
*
* @return
* The base class of the type of object stored within a
* {@link Directory} of this type.
*/
public Class<? extends Identifiable> getObjectType() {
return objectType;
}
/**
* Returns the Directory.Type representing the type of a Directory that
* could contain an object having the given class. The class may be a
* subclass of the overall base class of the objects stored within the
* Directory.
*
* @param objectType
* The class to determine the Directory.Type of.
*
* @return
* The Directory.Type representing the type of a Directory that
* could contain an object having the given class, or null if there
* is no such Directory available via the UserContext interface.
*/
public static Type of(Class<? extends Identifiable> objectType) {
for (Type type : Type.values()) {
if (type.getObjectType().isAssignableFrom(objectType))
return type;
}
return null;
}
}
/**
* Returns the object having the given identifier. Note that changes to
* the object returned will not necessarily affect the object stored within

View File

@@ -0,0 +1,111 @@
/*
* 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.net.event;
import org.apache.guacamole.net.auth.Directory;
import org.apache.guacamole.net.auth.Identifiable;
/**
* Abstract basis for events which involve a modification made to the objects
* within a {@link Directory}.
*
* @param <ObjectType>
* The type of object stored within the {@link Directory}.
*/
public interface DirectoryEvent<ObjectType extends Identifiable>
extends AuthenticationProviderEvent, UserEvent {
/**
* The types of directory operations that may be represented by a
* DirectoryEvent.
*/
public enum Operation {
/**
* An object was added to the {@link Directory}. The object added can
* be accessed with {@link #getObject()}, and its identifier may be
* obtained from {@link #getObjectIdentifier()}.
*/
ADD,
/**
* An object was retrieved from a {@link Directory}. The object
* retrieved can be accessed with {@link #getObject()}, and its
* identifier may be obtained from {@link #getObjectIdentifier()}.
*/
GET,
/**
* An existing object within a {@link Directory} was modified. The
* modified object can be accessed with {@link #getObject()}, and its
* identifier may be obtained from {@link #getObjectIdentifier()}.
*/
UPDATE,
/**
* An existing object within a {@link Directory} was deleted/removed.
* The identifier of the object that was deleted may be obtained from
* {@link #getObjectIdentifier()}. The full object that was deleted
* will be made available via {@link #getObject()} if possible, but
* this is not guaranteed for deletions.
*/
REMOVE
}
/**
* Returns the type of objects stored within the {@link Directory}
* affected by the operation.
*
* @return
* The type of objects stored within the {@link Directory}.
*/
Directory.Type getDirectoryType();
/**
* Returns the operation that was performed/attempted.
*
* @return
* The operation that was performed or attempted.
*/
Operation getOperation();
/**
* Returns the identifier of the object affected by the operation. If the
* object was just created, this will be the identifier of the new object.
*
* @return
* The identifier of the object affected by the operation.
*/
String getObjectIdentifier();
/**
* Returns the object affected by the operation, if available. For
* deletions, there is no guarantee that the affected object will be
* available within this event. If the object is not available, null is
* returned.
*
* @return
* The object affected by the operation performed, or null if that
* object is not available in the context of this event.
*/
ObjectType getObject();
}

View File

@@ -0,0 +1,36 @@
/*
* 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.net.event;
import org.apache.guacamole.net.auth.Directory;
import org.apache.guacamole.net.auth.Identifiable;
/**
* Event that is dispatched whenever a REST API request to create/modify/delete
* an object within a {@link Directory} fails. The specific failure is made
* available via {@link #getFailure()}.
*
* @param <ObjectType>
* The type of object stored within the {@link Directory}.
*/
public interface DirectoryFailureEvent<ObjectType extends Identifiable>
extends DirectoryEvent<ObjectType>, FailureEvent {
}

View File

@@ -0,0 +1,35 @@
/*
* 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.net.event;
import org.apache.guacamole.net.auth.Directory;
import org.apache.guacamole.net.auth.Identifiable;
/**
* Event that is dispatched whenever a REST API request to create/modify/delete
* an object within a {@link Directory} succeeds.
*
* @param <ObjectType>
* The type of object stored within the {@link Directory}.
*/
public interface DirectorySuccessEvent<ObjectType extends Identifiable>
extends DirectoryEvent<ObjectType> {
}

View File

@@ -0,0 +1,124 @@
/*
* 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.net.auth;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
/**
* Test that verifies the functionality provided by the Directory interface.
*/
public class DirectoryTest {
/**
* Returns a Collection of all classes that have associated Directories
* available via the UserContext interface. The classes are retrieved
* using reflection by enumerating the type parameters of the return types
* of all functions that return a Directory.
*
* @return
* A Collection of all classes that have associated Directories
* available via the UserContext interface.
*/
@SuppressWarnings("unchecked") // Verified via calls to isAssignableFrom()
private Collection<Class<? extends Identifiable>> getDirectoryTypes() {
Set<Class<? extends Identifiable>> types = new HashSet<>();
Method[] methods = UserContext.class.getMethods();
for (Method method : methods) {
if (!Directory.class.isAssignableFrom(method.getReturnType()))
continue;
Type retType = method.getGenericReturnType();
Assert.assertTrue("UserContext functions that return directories "
+ "must have proper type parameters for the returned "
+ "directory.", retType instanceof ParameterizedType);
Type[] typeArgs = ((ParameterizedType) retType).getActualTypeArguments();
Assert.assertEquals("UserContext functions that return directories "
+ "must properly declare exactly one type argument for "
+ "those directories.", 1, typeArgs.length);
Class<?> directoryType = (Class<?>) typeArgs[0];
Assert.assertTrue("Directories returned by UserContext functions "
+ "must contain subclasses of Identifiable.",
Identifiable.class.isAssignableFrom(directoryType));
types.add((Class<? extends Identifiable>) directoryType);
}
return Collections.unmodifiableSet(types);
}
/**
* Verifies that Directory.Type covers the types of all directories exposed
* by the UserContext interface.
*/
@Test
public void testTypeCoverage() {
Collection<Class<? extends Identifiable>> types = getDirectoryTypes();
Assert.assertEquals("Directory.Type must provide exactly one value "
+ "for each type of directory provideed by the UserContext "
+ "interface.", types.size(), Directory.Type.values().length);
for (Class<? extends Identifiable> type : types) {
Directory.Type dirType = Directory.Type.of(type);
Assert.assertNotNull("of() must provide mappings for all directory "
+ "types defined on the UserContext interface.", dirType);
Assert.assertEquals("getObjectType() must return the same base "
+ "superclass used by UserContext for all directory "
+ "types defined on the UserContext interface.", type,
dirType.getObjectType());
}
}
/**
* Verifies that each type declared by Directory.Type exposes an
* associated class via getObjectType() which then maps back to the same
* type via Directory.Type.of().
*/
@Test
public void testTypeIdentity() {
for (Directory.Type dirType : Directory.Type.values()) {
Assert.assertEquals("For all defined directory types, "
+ "Directory.Type.of(theType.getObjectType()) must "
+ "correctly map back to theType.", dirType,
Directory.Type.of(dirType.getObjectType()));
}
}
}