Working multiple-config login stub.
authorMichael Jumper <zhangmaike@users.sourceforge.net>
Sat, 13 Aug 2011 04:40:07 +0000 (21:40 -0700)
committerMichael Jumper <zhangmaike@users.sourceforge.net>
Sat, 13 Aug 2011 04:40:07 +0000 (21:40 -0700)
pom.xml
src/main/java/net/sourceforge/guacamole/net/basic/BasicGuacamoleTunnelServlet.java
src/main/java/net/sourceforge/guacamole/net/basic/BasicLogin.java
src/main/java/net/sourceforge/guacamole/net/basic/ConfigurationList.java [new file with mode: 0644]
src/main/webapp/WEB-INF/web.xml
src/main/webapp/client.xhtml
src/main/webapp/index.xhtml
src/main/webapp/styles/login.css

diff --git a/pom.xml b/pom.xml
index 5b465e3..17f2cc0 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
     <groupId>net.sourceforge.guacamole</groupId>
     <artifactId>guacamole-default-webapp</artifactId>
     <packaging>war</packaging>
-    <version>0.4.0</version>
+    <version>0.5.0</version>
     <name>guacamole-default-webapp</name>
     <url>http://guacamole.sourceforge.net/</url>
 
index e2d447f..d23f631 100644 (file)
@@ -18,8 +18,10 @@ package net.sourceforge.guacamole.net.basic;
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+import java.util.Map;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import net.sourceforge.guacamole.GuacamoleException;
 import net.sourceforge.guacamole.net.InetGuacamoleSocket;
@@ -59,26 +61,26 @@ public class BasicGuacamoleTunnelServlet extends GuacamoleTunnelServlet {
 
         HttpSession httpSession = request.getSession(true);
 
-        // Retrieve username and password from parms
-        String username = request.getParameter("username");
-        String password = request.getParameter("password");
+        // Get ID of connection
+        String id = request.getParameter("id");
+        
+        // Get authorized configs
+        Map<String, GuacamoleConfiguration> configs =
+                (Map<String, GuacamoleConfiguration>)
+                httpSession.getAttribute("GUAC_AUTH_CONFIGS");
+
+        // If no configs in session, not authorized
+        if (configs == null)
+            throw new GuacamoleException("No authorized configurations.");
 
         // Get authorized config
-        GuacamoleConfiguration config;
-        try {
-            config = authProvider.getAuthorizedConfiguration(username, password);
-        }
-        catch (GuacamoleException e) {
-            logger.error("Error retrieving authorized configuration for user {}.", username);
-            throw e;
-        }
-        
+        GuacamoleConfiguration config = configs.get(id);
         if (config == null) {
-            logger.warn("Failed login from {} for user \"{}\".", request.getRemoteAddr(), username);
-            throw new GuacamoleException("Invalid login");
+            logger.error("Error retrieving authorized configuration id={}.", id);
+            throw new GuacamoleException("Unknown configuration ID.");
         }
-
-        logger.info("Successful login from {} for user \"{}\".", request.getRemoteAddr(), username);
+        
+        logger.info("Successful connection from {} to \"{}\".", request.getRemoteAddr(), id);
 
         // Configure and connect socket
         String hostname = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
index 1702421..0884f46 100644 (file)
@@ -19,6 +19,8 @@ package net.sourceforge.guacamole.net.basic;
  */
 
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -80,7 +82,11 @@ public class BasicLogin extends HttpServlet {
 
         logger.info("Successful login from {} for user \"{}\".", request.getRemoteAddr(), username);
 
-        httpSession.setAttribute("GUAC_AUTH_CONFIGS", new Integer(0));
+        // Build map of authorized configs
+        Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
+        configs.put("TEST-UID", config);
+        
+        httpSession.setAttribute("GUAC_AUTH_CONFIGS", configs);
 
     }
 
diff --git a/src/main/java/net/sourceforge/guacamole/net/basic/ConfigurationList.java b/src/main/java/net/sourceforge/guacamole/net/basic/ConfigurationList.java
new file mode 100644 (file)
index 0000000..b3dae6e
--- /dev/null
@@ -0,0 +1,77 @@
+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.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ConfigurationList extends HttpServlet {
+
+    private Logger logger = LoggerFactory.getLogger(ConfigurationList.class);
+   
+    @Override
+    protected void service(HttpServletRequest request, HttpServletResponse response)
+    throws IOException {
+
+        HttpSession httpSession = request.getSession(true);
+
+        // Get authorized configs
+        Map<String, GuacamoleConfiguration> configs =
+                (Map<String, GuacamoleConfiguration>)
+                httpSession.getAttribute("GUAC_AUTH_CONFIGS");
+
+        // If no configs in session, not authorized
+        if (configs == null) {
+            response.sendError(HttpServletResponse.SC_FORBIDDEN);
+            return;
+        }
+
+        // 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>");
+    }
+
+}
+
index 3983287..2b64e11 100644 (file)
         <url-pattern>/login</url-pattern>
     </servlet-mapping>
 
+    <!-- Configuration List Servlet -->
+    <servlet>
+        <description>Configuration list servlet.</description>
+        <servlet-name>Configs</servlet-name>
+        <servlet-class>net.sourceforge.guacamole.net.basic.ConfigurationList</servlet-class>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>Configs</servlet-name>
+        <url-pattern>/configs</url-pattern>
+    </servlet-mapping>
+
     <!-- Guacamole Tunnel Servlet -->
     <servlet>
         <description>Tunnel servlet.</description>
index 97bdb64..58529bd 100644 (file)
                 new Guacamole.HTTPTunnel("tunnel")
             );
 
-            try {
-
-                // Connect client
-                guac.connect(data);
-
-            }
-            catch (e) {
-                // TODO: Handle exception ...
-            }
-
             var menu = document.getElementById("menu");
             var logo = document.getElementById("status-logo");
 
                 guac.sendKeyEvent(0, KEYSYM_CTRL);
             }
 
+
+            try {
+
+                // Get ID
+                var url = window.location.href;
+                var query = url.indexOf("?");
+                var id = url.substring(query+1);
+
+                // Connect client
+                guac.connect("id=" + encodeURIComponent(id));
+
+            }
+            catch (e) {
+                // TODO: Handle exception ...
+            }
+
+
             /* ]]> */ </script>
 
     </body>
index 92fc1ed..5468b7f 100644 (file)
             <table class="connections">
                 <thead>
                     <tr>
-                        <th>Name</th>
-                        <th>Protocol</th>
-                        <th>Description</th>
+                        <th class="protocol">Protocol</th>
+                        <th class="name">Name</th>
                     </tr>
                 </thead>
-                <tbody>
-                    <tr>
-                        <td>zhz@localhost</td>
-                        <td>vnc</td>
-                        <td class="description">Connect to test.guac-dev.org via vnc.</td>
-                    </tr>
-                    <tr>
-                        <td>zhz@localhost</td>
-                        <td>ssh</td>
-                        <td class="description">Connect to test.guac-dev.org via ssh.</td>
-                    </tr>
+                <tbody id="connections-tbody">
                 </tbody>
             </table>
 
         <!-- Init -->
         <script type="text/javascript"> /* <![CDATA[ */
 
+            function Config(protocol, id) {
+                this.protocol = protocol;
+                this.id = id;
+            }
+
+            function getConfigList() {
+
+                // Get config list
+                var xhr = new XMLHttpRequest();
+                xhr.open("GET", "configs", false);
+                xhr.send(null);
+
+                // If fail, throw error
+                if (xhr.status != 200)
+                    throw new Error(xhr.statusText);
+
+                // Otherwise, get list
+                var configs = new Array();
+
+                var configElements = xhr.responseXML.getElementsByTagName("config");
+                for (var i=0; i<configElements.length; i++) {
+                    configs.push(new Config(
+                        configElements[i].getAttribute("protocol"),
+                        configElements[i].getAttribute("id")
+                    ));
+                }
+
+                return configs;
+                
+            }
+
+            function resetUI() {
+
+                var configs;
+                try {
+                    configs = getConfigList();
+                }
+                catch (e) {
+
+                    console.log(e);
+
+                    // Show login UI if unable to get configs
+                    loginUI.style.display = "";
+                    connectionListUI.style.display = "none";
+
+                    return;
+
+                }
+
+                // Remove all rows from connections list
+                var tbody = document.getElementById("connections-tbody");
+                tbody.innerHTML = "";
+                
+                // Add one row per connection
+                for (var i=0; i<configs.length; i++) {
+
+                    // Create row and cells
+                    var tr = document.createElement("tr");
+                    var protocol = document.createElement("td");
+                    var id = document.createElement("td");
+
+                    // Set CSS
+                    protocol.className = "protocol";
+                    id.className = "name";
+
+                    // Create link to client
+                    var clientLink = document.createElement("a");
+                    clientLink.setAttribute("href",
+                        "client.xhtml?" + encodeURIComponent(configs[i].id));
+
+                    // Set cell contents
+                    protocol.textContent   = configs[i].protocol;
+                    clientLink.textContent = configs[i].id;
+                    id.appendChild(clientLink);
+
+                    // Add cells
+                    tr.appendChild(protocol);
+                    tr.appendChild(id);
+
+                    // Add row
+                    tbody.appendChild(tr);
+
+                }
+
+                // If configs could be retrieved, display list
+                loginUI.style.display = "none";
+                connectionListUI.style.display = "";
+
+            }
+
             var loginForm = document.getElementById("login-form");
             var loginUI = document.getElementById("login-ui");
             var connectionListUI = document.getElementById("connection-list-ui");
                     if (xhr.status != 200)
                         throw new Error("Invalid login");
 
-                    // Hide login UI, display connections
-                    loginUI.style.display = "none";
-                    connectionListUI.style.display = "";
+                    resetUI();
 
                 }
                 catch (e) {
 
             }
 
-            loginUI.style.display = "";
+            resetUI();
 
             /* ]]> */ </script>
 
index a64b406..728866a 100644 (file)
@@ -150,6 +150,16 @@ div#connection-list-ui table thead {
     background: #9A8;
 }
 
+div#connection-list-ui table thead th.protocol {
+    width: 1em;
+    padding: 0.5em;
+}
+
+div#connection-list-ui table thead th.name {
+    text-align: left;
+    padding: 0.5em;
+}
+
 div#connection-list-ui table thead tr {
     border-top:    1px solid #676;
     border-bottom: 1px solid gray;
@@ -169,6 +179,10 @@ div#connection-list-ui table td {
     text-align: center;
 }
 
+div#connection-list-ui table td.name {
+    text-align: left;
+}
+
 div#connection-list-ui table tbody tr:nth-child(even)       { background: #CCC; }
 div#connection-list-ui table tbody tr:nth-child(odd)       { background: #EEE; }