From b4d128c12cdcb5b72f92bf49fcd4040436f95c54 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 11 Aug 2013 13:13:40 -0700 Subject: [PATCH] Add support for groups to the connection list XML. --- .../net/basic/crud/connections/List.java | 279 +++++++++++++----- 1 file changed, 204 insertions(+), 75 deletions(-) diff --git a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/crud/connections/List.java b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/crud/connections/List.java index 0f66b9791..2ef6af751 100644 --- a/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/crud/connections/List.java +++ b/guacamole/src/main/java/net/sourceforge/guacamole/net/basic/crud/connections/List.java @@ -19,6 +19,7 @@ package net.sourceforge.guacamole.net.basic.crud.connections; */ import java.io.IOException; +import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.stream.XMLOutputFactory; @@ -28,6 +29,7 @@ import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleSecurityException; import net.sourceforge.guacamole.GuacamoleServerException; import net.sourceforge.guacamole.net.auth.Connection; +import net.sourceforge.guacamole.net.auth.ConnectionGroup; import net.sourceforge.guacamole.net.auth.ConnectionRecord; import net.sourceforge.guacamole.net.auth.Directory; import net.sourceforge.guacamole.net.auth.User; @@ -47,6 +49,13 @@ import net.sourceforge.guacamole.protocol.GuacamoleConfiguration; */ public class List extends AuthenticatingHttpServlet { + /** + * System administration permission. + */ + private static final Permission SYSTEM_PERMISSION = + new SystemPermission(SystemPermission.Type.ADMINISTER); + + /** * Checks whether the given user has permission to perform the given * object operation. Security exceptions are handled appropriately - only @@ -81,6 +90,197 @@ public class List extends AuthenticatingHttpServlet { } + /** + * Writes the XML for the given connection group. + * + * @param self The user whose permissions dictate the availability of the + * data written. + * @param xml The XMLStremWriter to use when writing the data. + * @param group The connection group whose XML representation will be + * written. + * @throws GuacamoleException If an error occurs while reading the + * requested data. + * @throws XMLStreamException If an error occurs while writing the XML. + */ + private void writeConnectionGroup(User self, XMLStreamWriter xml, + ConnectionGroup group) throws GuacamoleException, XMLStreamException { + + // Write group + xml.writeStartElement("group"); + xml.writeAttribute("id", group.getIdentifier()); + xml.writeAttribute("name", group.getName()); + + // Write group type + switch (group.getType()) { + + case ORGANIZATIONAL: + xml.writeAttribute("type", "organizational"); + break; + + case BALANCING: + xml.writeAttribute("type", "balancing"); + break; + + } + + // Write contained groups and connections + writeConnections(self, xml, group.getConnectionDirectory()); + writeConnectionGroups(self, xml, group.getConnectionGroupDirectory()); + + // End of group + xml.writeEndElement(); + + } + + /** + * Writes the XML for the given connection. + * + * @param self The user whose permissions dictate the availability of the + * data written. + * @param xml The XMLStremWriter to use when writing the data. + * @param connection The connection whose XML representation will be + * written. + * @throws GuacamoleException If an error occurs while reading the + * requested data. + * @throws XMLStreamException If an error occurs while writing the XML. + */ + private void writeConnection(User self, XMLStreamWriter xml, + Connection connection) throws GuacamoleException, XMLStreamException { + + // Write connection + xml.writeStartElement("connection"); + xml.writeAttribute("id", connection.getIdentifier()); + xml.writeAttribute("name", connection.getName()); + xml.writeAttribute("protocol", + connection.getConfiguration().getProtocol()); + + // If update permission available, include parameters + if (self.hasPermission(SYSTEM_PERMISSION) || + hasConfigPermission(self, ObjectPermission.Type.UPDATE, + connection.getIdentifier())) { + + // As update permission is present, also list parameters + GuacamoleConfiguration config = connection.getConfiguration(); + for (String name : config.getParameterNames()) { + + String value = connection.getConfiguration().getParameter(name); + xml.writeStartElement("param"); + xml.writeAttribute("name", name); + + if (value != null) + xml.writeCharacters(value); + + xml.writeEndElement(); + } + + } + + // Write history + xml.writeStartElement("history"); + for (ConnectionRecord record : connection.getHistory()) { + xml.writeStartElement("record"); + + // Start date + xml.writeAttribute("start", + Long.toString(record.getStartDate().getTime())); + + // End date + if (record.getEndDate() != null) + xml.writeAttribute("end", + Long.toString(record.getEndDate().getTime())); + + // Whether connection currently active + if (record.isActive()) + xml.writeAttribute("active", "yes"); + + // User involved + xml.writeCharacters(record.getUsername()); + + xml.writeEndElement(); + } + xml.writeEndElement(); + + // End connection + xml.writeEndElement(); + + } + + /** + * Writes the XML for the given directory of connection groups. + * + * @param self The user whose permissions dictate the availability of the + * data written. + * @param xml The XMLStremWriter to use when writing the data. + * @param directory The directory whose XML representation will be + * written. + * @throws GuacamoleException If an error occurs while reading the + * requested data. + * @throws XMLStreamException If an error occurs while writing the XML. + */ + private void writeConnectionGroups(User self, XMLStreamWriter xml, + Directory directory) + throws GuacamoleException, XMLStreamException { + + // If no connections, write nothing + Set identifiers = directory.getIdentifiers(); + if (identifiers.isEmpty()) + return; + + // Begin connections + xml.writeStartElement("groups"); + + // For each entry, write corresponding connection element + for (String identifier : identifiers) { + + // Write each group + ConnectionGroup group = directory.get(identifier); + writeConnectionGroup(self, xml, group); + + } + + // End connections + xml.writeEndElement(); + + } + + /** + * Writes the XML for the given directory of connections. + * + * @param self The user whose permissions dictate the availability of the + * data written. + * @param xml The XMLStremWriter to use when writing the data. + * @param directory The directory whose XML representation will be + * written. + * @throws GuacamoleException If an error occurs while reading the + * requested data. + * @throws XMLStreamException If an error occurs while writing the XML. + */ + private void writeConnections(User self, XMLStreamWriter xml, + Directory directory) + throws GuacamoleException, XMLStreamException { + + // If no connections, write nothing + Set identifiers = directory.getIdentifiers(); + if (identifiers.isEmpty()) + return; + + // Begin connections + xml.writeStartElement("connections"); + + // For each entry, write corresponding connection element + for (String identifier : identifiers) { + + // Write each connection + Connection connection = directory.get(identifier); + writeConnection(self, xml, connection); + + } + + // End connections + xml.writeEndElement(); + + } + @Override protected void authenticatedService( UserContext context, @@ -96,13 +296,8 @@ public class List extends AuthenticatingHttpServlet { // Set encoding response.setCharacterEncoding("UTF-8"); - // Get connection directory - Directory directory = - context.getRootConnectionGroup().getConnectionDirectory(); - - // Sys-admin permission - Permission systemPermission = - new SystemPermission(SystemPermission.Type.ADMINISTER); + // Get root group + ConnectionGroup root = context.getRootConnectionGroup(); // Write actual XML try { @@ -113,75 +308,9 @@ public class List extends AuthenticatingHttpServlet { XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xml = outputFactory.createXMLStreamWriter(response.getWriter()); - // Begin document + // Write content of root group xml.writeStartDocument(); - xml.writeStartElement("connections"); - - // For each entry, write corresponding connection element - for (String identifier : directory.getIdentifiers()) { - - // Get connection - Connection connection = directory.get(identifier); - - // Write connection - xml.writeStartElement("connection"); - xml.writeAttribute("id", identifier); - xml.writeAttribute("name", connection.getName()); - xml.writeAttribute("protocol", - connection.getConfiguration().getProtocol()); - - // If update permission available, include parameters - if (self.hasPermission(systemPermission) || - hasConfigPermission(self, ObjectPermission.Type.UPDATE, - identifier)) { - - // As update permission is present, also list parameters - GuacamoleConfiguration config = connection.getConfiguration(); - for (String name : config.getParameterNames()) { - - String value = connection.getConfiguration().getParameter(name); - xml.writeStartElement("param"); - xml.writeAttribute("name", name); - - if (value != null) - xml.writeCharacters(value); - - xml.writeEndElement(); - } - - } - - // Write history - xml.writeStartElement("history"); - for (ConnectionRecord record : connection.getHistory()) { - xml.writeStartElement("record"); - - // Start date - xml.writeAttribute("start", - Long.toString(record.getStartDate().getTime())); - - // End date - if (record.getEndDate() != null) - xml.writeAttribute("end", - Long.toString(record.getEndDate().getTime())); - - // Whether connection currently active - if (record.isActive()) - xml.writeAttribute("active", "yes"); - - // User involved - xml.writeCharacters(record.getUsername()); - - xml.writeEndElement(); - } - xml.writeEndElement(); - - xml.writeEndElement(); - - } - - // End document - xml.writeEndElement(); + writeConnectionGroup(self, xml, root); xml.writeEndDocument(); }