Add support for groups to the connection list XML.

This commit is contained in:
Michael Jumper
2013-08-11 13:13:40 -07:00
parent 701368a7fe
commit b4d128c12c

View File

@@ -19,6 +19,7 @@ package net.sourceforge.guacamole.net.basic.crud.connections;
*/ */
import java.io.IOException; import java.io.IOException;
import java.util.Set;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLOutputFactory;
@@ -28,6 +29,7 @@ import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.GuacamoleSecurityException; import net.sourceforge.guacamole.GuacamoleSecurityException;
import net.sourceforge.guacamole.GuacamoleServerException; import net.sourceforge.guacamole.GuacamoleServerException;
import net.sourceforge.guacamole.net.auth.Connection; 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.ConnectionRecord;
import net.sourceforge.guacamole.net.auth.Directory; import net.sourceforge.guacamole.net.auth.Directory;
import net.sourceforge.guacamole.net.auth.User; import net.sourceforge.guacamole.net.auth.User;
@@ -47,6 +49,13 @@ import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
*/ */
public class List extends AuthenticatingHttpServlet { 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 * Checks whether the given user has permission to perform the given
* object operation. Security exceptions are handled appropriately - only * object operation. Security exceptions are handled appropriately - only
@@ -81,59 +90,74 @@ public class List extends AuthenticatingHttpServlet {
} }
@Override /**
protected void authenticatedService( * Writes the XML for the given connection group.
UserContext context, *
HttpServletRequest request, HttpServletResponse response) * @param self The user whose permissions dictate the availability of the
throws GuacamoleException { * 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 {
// Do not cache // Write group
response.setHeader("Cache-Control", "no-cache"); xml.writeStartElement("group");
xml.writeAttribute("id", group.getIdentifier());
xml.writeAttribute("name", group.getName());
// Write XML content type // Write group type
response.setHeader("Content-Type", "text/xml"); switch (group.getType()) {
// Set encoding case ORGANIZATIONAL:
response.setCharacterEncoding("UTF-8"); xml.writeAttribute("type", "organizational");
break;
// Get connection directory case BALANCING:
Directory<String, Connection> directory = xml.writeAttribute("type", "balancing");
context.getRootConnectionGroup().getConnectionDirectory(); break;
// Sys-admin permission }
Permission systemPermission =
new SystemPermission(SystemPermission.Type.ADMINISTER);
// Write actual XML // Write contained groups and connections
try { writeConnections(self, xml, group.getConnectionDirectory());
writeConnectionGroups(self, xml, group.getConnectionGroupDirectory());
// Get self // End of group
User self = context.self(); xml.writeEndElement();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); }
XMLStreamWriter xml = outputFactory.createXMLStreamWriter(response.getWriter());
// Begin document /**
xml.writeStartDocument(); * Writes the XML for the given connection.
xml.writeStartElement("connections"); *
* @param self The user whose permissions dictate the availability of the
// For each entry, write corresponding connection element * data written.
for (String identifier : directory.getIdentifiers()) { * @param xml The XMLStremWriter to use when writing the data.
* @param connection The connection whose XML representation will be
// Get connection * written.
Connection connection = directory.get(identifier); * @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 // Write connection
xml.writeStartElement("connection"); xml.writeStartElement("connection");
xml.writeAttribute("id", identifier); xml.writeAttribute("id", connection.getIdentifier());
xml.writeAttribute("name", connection.getName()); xml.writeAttribute("name", connection.getName());
xml.writeAttribute("protocol", xml.writeAttribute("protocol",
connection.getConfiguration().getProtocol()); connection.getConfiguration().getProtocol());
// If update permission available, include parameters // If update permission available, include parameters
if (self.hasPermission(systemPermission) || if (self.hasPermission(SYSTEM_PERMISSION) ||
hasConfigPermission(self, ObjectPermission.Type.UPDATE, hasConfigPermission(self, ObjectPermission.Type.UPDATE,
identifier)) { connection.getIdentifier())) {
// As update permission is present, also list parameters // As update permission is present, also list parameters
GuacamoleConfiguration config = connection.getConfiguration(); GuacamoleConfiguration config = connection.getConfiguration();
@@ -176,12 +200,117 @@ public class List extends AuthenticatingHttpServlet {
} }
xml.writeEndElement(); xml.writeEndElement();
// End connection
xml.writeEndElement(); xml.writeEndElement();
} }
// End document /**
* 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<String, ConnectionGroup> directory)
throws GuacamoleException, XMLStreamException {
// If no connections, write nothing
Set<String> 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(); 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<String, Connection> directory)
throws GuacamoleException, XMLStreamException {
// If no connections, write nothing
Set<String> 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,
HttpServletRequest request, HttpServletResponse response)
throws GuacamoleException {
// Do not cache
response.setHeader("Cache-Control", "no-cache");
// Write XML content type
response.setHeader("Content-Type", "text/xml");
// Set encoding
response.setCharacterEncoding("UTF-8");
// Get root group
ConnectionGroup root = context.getRootConnectionGroup();
// Write actual XML
try {
// Get self
User self = context.self();
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter xml = outputFactory.createXMLStreamWriter(response.getWriter());
// Write content of root group
xml.writeStartDocument();
writeConnectionGroup(self, xml, root);
xml.writeEndDocument(); xml.writeEndDocument();
} }