diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/Directory.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/Directory.java index e08446340..96f4f7b02 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/Directory.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/Directory.java @@ -22,6 +22,7 @@ package org.glyptodon.guacamole.net.auth; +import java.util.Collection; import java.util.Set; import org.glyptodon.guacamole.GuacamoleException; @@ -57,6 +58,29 @@ public interface Directory { */ ObjectType get(IdentifierType identifier) throws GuacamoleException; + /** + * Returns the objects having the given identifiers. Note that changes to + * any object returned will not necessarily affect the object stored within + * the Directory. To update an object stored within a + * Directory such that future calls to get() will return the updated + * object, you must call update() on the object after modification. + * + * @param identifiers + * The identifiers to use when locating the objects to return. + * + * @return + * The objects having the given identifiers. If any identifiers do not + * correspond to accessible objects, those identifiers will be ignored. + * If no objects correspond to any of the given identifiers, the + * returned collection will be empty. + * + * @throws GuacamoleException + * If an error occurs while retrieving the objects, or if permission + * to retrieve the requested objects is denied. + */ + Collection getAll(Collection identifiers) + throws GuacamoleException; + /** * Returns a Set containing all identifiers for all objects within this * Directory. diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionDirectory.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionDirectory.java index c5fe6594f..e7669b872 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionDirectory.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionDirectory.java @@ -25,11 +25,7 @@ package org.glyptodon.guacamole.net.auth.simple; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; -import org.glyptodon.guacamole.GuacamoleException; -import org.glyptodon.guacamole.GuacamoleSecurityException; import org.glyptodon.guacamole.net.auth.Connection; -import org.glyptodon.guacamole.net.auth.Directory; import org.glyptodon.guacamole.protocol.GuacamoleConfiguration; /** @@ -39,13 +35,12 @@ import org.glyptodon.guacamole.protocol.GuacamoleConfiguration; * * @author Michael Jumper */ -public class SimpleConnectionDirectory - implements Directory { +public class SimpleConnectionDirectory extends SimpleDirectory { /** * The Map of Connections to provide access to. */ - private Map connections = + private final Map connections = new HashMap(); /** @@ -60,56 +55,25 @@ public class SimpleConnectionDirectory // Create connections for each config for (Entry entry : configs.entrySet()) connections.put(entry.getKey(), - new SimpleConnection(entry.getKey(), entry.getKey(), + new SimpleConnection(entry.getKey(), entry.getKey(), entry.getValue())); + // Use the connection map to back the underlying AbstractDirectory + super.setObjects(connections); + } - @Override - public Connection get(String identifier) - throws GuacamoleException { - return connections.get(identifier); - } - - @Override - public Set getIdentifiers() throws GuacamoleException { - return connections.keySet(); - } - - @Override - public void add(Connection connection) - throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void update(Connection connection) - throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void remove(String identifier) throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void move(String identifier, Directory directory) - throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - /** * An internal method for modifying the Connections in this Directory. * Returns the previous connection for the given identifier, if found. - * + * * @param connection The connection to add or update the Directory with. * @return The previous connection for the connection identifier, if found. */ public Connection putConnection(Connection connection) { return connections.put(connection.getIdentifier(), connection); } - + /** * An internal method for removing a Connection from this Directory. * @param identifier The identifier of the Connection to remove. diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionGroupDirectory.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionGroupDirectory.java index 0ebe52414..344cbcb4d 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionGroupDirectory.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleConnectionGroupDirectory.java @@ -25,11 +25,7 @@ package org.glyptodon.guacamole.net.auth.simple; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import java.util.Set; -import org.glyptodon.guacamole.GuacamoleException; -import org.glyptodon.guacamole.GuacamoleSecurityException; import org.glyptodon.guacamole.net.auth.ConnectionGroup; -import org.glyptodon.guacamole.net.auth.Directory; /** * An extremely simple read-only implementation of a Directory of @@ -39,18 +35,18 @@ import org.glyptodon.guacamole.net.auth.Directory; * @author James Muehlner */ public class SimpleConnectionGroupDirectory - implements Directory { + extends SimpleDirectory { /** * The Map of ConnectionGroups to provide access to. */ - private Map connectionGroups = + private final Map connectionGroups = new HashMap(); /** * Creates a new SimpleConnectionGroupDirectory which contains the given * groups. - * + * * @param groups A Collection of all groups that should be present in this * connection group directory. */ @@ -60,46 +56,15 @@ public class SimpleConnectionGroupDirectory for (ConnectionGroup group : groups) connectionGroups.put(group.getIdentifier(), group); - } + // Use the connection group map to back the underlying AbstractDirectory + super.setObjects(connectionGroups); - @Override - public ConnectionGroup get(String identifier) - throws GuacamoleException { - return connectionGroups.get(identifier); - } - - @Override - public Set getIdentifiers() throws GuacamoleException { - return connectionGroups.keySet(); - } - - @Override - public void add(ConnectionGroup connectionGroup) - throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void update(ConnectionGroup connectionGroup) - throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void remove(String identifier) throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void move(String identifier, Directory directory) - throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); } /** * An internal method for modifying the ConnectionGroups in this Directory. * Returns the previous connection group for the given identifier, if found. - * + * * @param connectionGroup The connection group to add or update the * Directory with. * @return The previous connection group for the connection group @@ -108,10 +73,10 @@ public class SimpleConnectionGroupDirectory public ConnectionGroup putConnectionGroup(ConnectionGroup connectionGroup) { return connectionGroups.put(connectionGroup.getIdentifier(), connectionGroup); } - + /** * An internal method for removing a ConnectionGroup from this Directory. - * + * * @param identifier The identifier of the ConnectionGroup to remove. * @return The previous connection group for the given identifier, if found. */ diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleDirectory.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleDirectory.java new file mode 100644 index 000000000..d665a9687 --- /dev/null +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleDirectory.java @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2015 Glyptodon LLC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.glyptodon.guacamole.net.auth.simple; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import org.glyptodon.guacamole.GuacamoleException; +import org.glyptodon.guacamole.GuacamoleSecurityException; +import org.glyptodon.guacamole.net.auth.Directory; + +/** + * An extremely simple read-only implementation of a Directory which provides + * access to a pre-defined Map of arbitrary objects. Any changes to the Map + * will affect the available contents of this SimpleDirectory. + * + * @author Michael Jumper + * @param + * The type of identifier used to identify objects stored within this + * SimpleDirectory. + * + * @param + * The type of objects stored within this SimpleDirectory. + */ +public class SimpleDirectory + implements Directory { + + /** + * The Map of objects to provide access to. + */ + private Map objects = Collections.EMPTY_MAP; + + /** + * Creates a new empty SimpleDirectory which does not provide access to + * any objects. + */ + public SimpleDirectory() { + } + + /** + * Creates a new SimpleDirectory which provides access to the objects + * contained within the given Map. + * + * @param objects + * The Map of objects to provide access to. + */ + public SimpleDirectory(Map objects) { + this.objects = objects; + } + + /** + * Sets the Map which backs this SimpleDirectory. Future function calls + * which retrieve objects from this SimpleDirectory will use the provided + * Map. + * + * @param objects + * The Map of objects to provide access to. + */ + protected void setObjects(Map objects) { + this.objects = objects; + } + + /** + * Returns the Map which currently backs this SimpleDirectory. Changes to + * this Map will affect future function calls that retrieve objects from + * this SimpleDirectory. + * + * @return + * The Map of objects which currently backs this SimpleDirectory. + */ + protected Map getObjects() { + return objects; + } + + @Override + public ObjectType get(IdentifierType identifier) + throws GuacamoleException { + return objects.get(identifier); + } + + @Override + public Collection getAll(Collection identifiers) + throws GuacamoleException { + + // Create collection which has an appropriate initial size + Collection foundObjects = new ArrayList(identifiers.size()); + + // Populate collection with matching objects + for (IdentifierType identifier : identifiers) { + + // Add the object which has the current identifier, if any + ObjectType object = objects.get(identifier); + if (object != null) + foundObjects.add(object); + + } + + return foundObjects; + + } + + @Override + public Set getIdentifiers() throws GuacamoleException { + return objects.keySet(); + } + + @Override + public void add(ObjectType connection) + throws GuacamoleException { + throw new GuacamoleSecurityException("Permission denied."); + } + + @Override + public void update(ObjectType connection) + throws GuacamoleException { + throw new GuacamoleSecurityException("Permission denied."); + } + + @Override + public void remove(IdentifierType identifier) throws GuacamoleException { + throw new GuacamoleSecurityException("Permission denied."); + } + + @Override + public void move(IdentifierType identifier, + Directory directory) + throws GuacamoleException { + throw new GuacamoleSecurityException("Permission denied."); + } + +} diff --git a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleUserDirectory.java b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleUserDirectory.java index 97e6eefb3..de7bd08e8 100644 --- a/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleUserDirectory.java +++ b/guacamole-ext/src/main/java/org/glyptodon/guacamole/net/auth/simple/SimpleUserDirectory.java @@ -23,10 +23,6 @@ package org.glyptodon.guacamole.net.auth.simple; import java.util.Collections; -import java.util.Set; -import org.glyptodon.guacamole.GuacamoleException; -import org.glyptodon.guacamole.GuacamoleSecurityException; -import org.glyptodon.guacamole.net.auth.Directory; import org.glyptodon.guacamole.net.auth.User; /** @@ -35,12 +31,7 @@ import org.glyptodon.guacamole.net.auth.User; * * @author Michael Jumper */ -public class SimpleUserDirectory implements Directory { - - /** - * The only user to be contained within this directory. - */ - private User user; +public class SimpleUserDirectory extends SimpleDirectory { /** * Creates a new SimpleUserDirectory which provides access to the single @@ -49,45 +40,7 @@ public class SimpleUserDirectory implements Directory { * @param user The user to provide access to. */ public SimpleUserDirectory(User user) { - this.user = user; - } - - @Override - public User get(String username) throws GuacamoleException { - - // If username matches, return the user - if (user.getUsername().equals(username)) - return user; - - // Otherwise, not found - return null; - - } - - @Override - public Set getIdentifiers() throws GuacamoleException { - return Collections.singleton(user.getUsername()); - } - - @Override - public void add(User user) throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void update(User user) throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void remove(String username) throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); - } - - @Override - public void move(String identifier, Directory directory) - throws GuacamoleException { - throw new GuacamoleSecurityException("Permission denied."); + super(Collections.singletonMap(user.getUsername(), user)); } }