diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/DecoratingDirectory.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/DecoratingDirectory.java new file mode 100644 index 000000000..23ea0871e --- /dev/null +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/DecoratingDirectory.java @@ -0,0 +1,133 @@ +/* + * 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.util.ArrayList; +import java.util.Collection; +import org.apache.guacamole.GuacamoleException; + +/** + * Directory implementation which simplifies decorating the objects within an + * underlying Directory. The decorate() and undecorate() functions must be + * implemented to define how each object is decorated, and how that decoration + * may be removed. + * + * @param + * The type of objects stored within this Directory. + */ +public abstract class DecoratingDirectory + extends DelegatingDirectory { + + /** + * Creates a new DecoratingDirectory which decorates the objects within + * the given directory. + * + * @param directory + * The Directory whose objects are being decorated. + */ + public DecoratingDirectory(Directory directory) { + super(directory); + } + + /** + * Given an object retrieved from a Directory which originates from a + * different AuthenticationProvider, returns an identical type of object + * optionally wrapped with additional information, functionality, etc. If + * this directory chooses to decorate the object provided, it is up to the + * implementation of that decorated object to properly pass through + * operations as appropriate, as well as provide for an eventual + * undecorate() operation. All objects retrieved from this + * DecoratingDirectory will first be passed through this function. + * + * @param object + * An object from a Directory which originates from a different + * AuthenticationProvider. + * + * @return + * An object which may have been decorated by this + * DecoratingDirectory. If the object was not decorated, the original, + * unmodified object may be returned instead. + * + * @throws GuacamoleException + * If the provided object cannot be decorated due to an error. + */ + protected abstract ObjectType decorate(ObjectType object) + throws GuacamoleException; + + /** + * Given an object originally returned from a call to this + * DecoratingDirectory's decorate() function, reverses the decoration + * operation, returning the original object. This function is effectively + * the exact inverse of the decorate() function. The return value of + * undecorate(decorate(X)) must be identically X. All objects given to this + * DecoratingDirectory via add() or update() will first be passed through + * this function. + * + * @param object + * An object which was originally returned by a call to this + * DecoratingDirectory's decorate() function. + * + * @return + * The original object which was provided to this DecoratingDirectory's + * decorate() function. + * + * @throws GuacamoleException + * If the provided object cannot be undecorated due to an error. + */ + protected abstract ObjectType undecorate(ObjectType object) + throws GuacamoleException; + + @Override + public ObjectType get(String identifier) throws GuacamoleException { + + // Decorate only if object exists + ObjectType object = super.get(identifier); + if (object == null) + return object; + + return decorate(object); + } + + @Override + public Collection getAll(Collection identifiers) + throws GuacamoleException { + + Collection objects = super.getAll(identifiers); + + // Decorate all retrieved objects, if any + Collection decorated = new ArrayList(objects.size()); + for (ObjectType object : objects) + decorated.add(decorate(object)); + + return decorated; + + } + + @Override + public void add(ObjectType object) throws GuacamoleException { + super.add(undecorate(object)); + } + + @Override + public void update(ObjectType object) throws GuacamoleException { + super.update(undecorate(object)); + } + +} diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/DelegatingDirectory.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/DelegatingDirectory.java new file mode 100644 index 000000000..f1bf27494 --- /dev/null +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/DelegatingDirectory.java @@ -0,0 +1,83 @@ +/* + * 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.util.Collection; +import java.util.Set; +import org.apache.guacamole.GuacamoleException; + +/** + * Directory implementation which simply delegates all function calls to an + * underlying Directory. + * + * @param + * The type of objects stored within this Directory. + */ +public class DelegatingDirectory + implements Directory { + + /** + * The wrapped Directory. + */ + private final Directory directory; + + /** + * Wraps the given Directory such that all function calls against this + * DelegatingDirectory will be delegated to it. + * + * @param directory + * The directory to wrap. + */ + public DelegatingDirectory(Directory directory) { + this.directory = directory; + } + + @Override + public ObjectType get(String identifier) throws GuacamoleException { + return directory.get(identifier); + } + + @Override + public Collection getAll(Collection identifiers) + throws GuacamoleException { + return directory.getAll(identifiers); + } + + @Override + public Set getIdentifiers() throws GuacamoleException { + return directory.getIdentifiers(); + } + + @Override + public void add(ObjectType object) throws GuacamoleException { + directory.add(object); + } + + @Override + public void update(ObjectType object) throws GuacamoleException { + directory.update(object); + } + + @Override + public void remove(String identifier) throws GuacamoleException { + directory.remove(identifier); + } + +}