package net.sourceforge.guacamole.net.basic;

/*
 *  Guacamole - Clientless Remote Desktop
 *  Copyright (C) 2010  Michael Jumper
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;

/**
 * Simple HttpServlet which outputs XML containing a list of all authorized
 * configurations for the current user.
 *
 * @author Michael Jumper
 */
public class ConfigurationList extends AuthenticatingHttpServlet {

    @Override
    protected void authenticatedService(
            Map<String, GuacamoleConfiguration> configs,
            HttpServletRequest request, HttpServletResponse response)
    throws IOException {

        // Do not cache
        response.setHeader("Cache-Control", "no-cache");

        // Write XML
        response.setHeader("Content-Type", "text/xml");
        PrintWriter out = response.getWriter();
        out.println("<configs>");

        for (Entry<String, GuacamoleConfiguration> entry : configs.entrySet()) {

            GuacamoleConfiguration config = entry.getValue();

            // Write config
            out.print("<config id=\"");
            out.print(entry.getKey());
            out.print("\" protocol=\"");
            out.print(config.getProtocol());
            out.println("\"/>");

        }

        out.println("</configs>");
    }

}


@@ -840,36 +840,8 @@
 on;%0A
-import java.io.PrintWriter;%0A
 impo
@@ -979,16 +979,143 @@
 sponse;%0A
+import javax.xml.stream.XMLOutputFactory;%0Aimport javax.xml.stream.XMLStreamException;%0Aimport javax.xml.stream.XMLStreamWriter;%0A
 import n
@@ -1761,24 +1761,25 @@
 /xml%22);%0A
+%0A
         
 PrintWri
@@ -1774,26 +1774,162 @@
     
-PrintWriter out = 
+try %7B%0A%0A            XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();%0A            XMLStreamWriter xml = outputFactory.createXMLStreamWriter(
 resp
@@ -1944,17 +1944,19 @@
 Writer()
-;
+);%0A
 %0A       
@@ -1960,35 +1960,197 @@
     
-out.println
+    // Begin document%0A            xml.writeStartDocument();%0A            xml.writeStartElement
 (%22
-%3C
 configs
-%3E
 %22);%0A
-%0A
+            %0A            // For each entry, write corresponding config element%0A    
     
@@ -2223,24 +2223,58 @@
 rySet()) %7B%0A%0A
+                // Get config%0A    
             
@@ -2328,32 +2328,36 @@
 );%0A%0A            
+    
 // Write config%0A
@@ -2372,32 +2372,41 @@
     
-out.pri
+    xml.writeEmptyEleme
 nt(%22
-%3C
 config
- id=%5C%22
 %22);%0A
@@ -2421,76 +2421,125 @@
     
-out.print(entry.getKey());%0A            out.print(%22%5C%22 p
+    xml.writeAttribute(%22id%22, entry.getKey());%0A                xml.writeAttribute(%22protocol%22, config.getP
 rotocol
-=%5C%22%22
+()
 );%0A
+%0A
     
@@ -2550,120 +2550,251 @@
     
-out.print(config.getProtocol());%0A            out.println(%22%5C%22/%3E%22);%0A%0A        %7D%0A%0A        out.println(%22%3C/configs%3E%22);
+%7D%0A%0A            // End document%0A            xml.writeEndElement();%0A            xml.writeEndDocument();%0A%0A        %7D%0A        catch (XMLStreamException e) %7B%0A            throw new IOException(%22Unable to write configuration list XML.%22, e);%0A        %7D%0A
 %0A   

Use StAX to write config XML.