Remove trailing whitespace.
[guacamole.git] / src / main / java / net / sourceforge / guacamole / net / basic / BasicFileAuthenticationProvider.java
index d31e2ba..c0e9eaf 100644 (file)
@@ -48,15 +48,13 @@ import org.xml.sax.helpers.XMLReaderFactory;
  * Authenticates users against a static list of username/password pairs.
  * Each username/password may be associated with multiple configurations.
  * This list is stored in an XML file which is reread if modified.
- * 
- * This is modified version of BasicFileAuthenticationProvider written by Michael Jumper.
- * 
- * @author Michal Kotas
+ *
+ * @author Michael Jumper, Michal Kotas
  */
 public class BasicFileAuthenticationProvider implements AuthenticationProvider {
 
     private Logger logger = LoggerFactory.getLogger(BasicFileAuthenticationProvider.class);
-    
+
     private long mappingTime;
     private Map<String, AuthInfo> mapping;
 
@@ -85,7 +83,7 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
             throw new GuacamoleException("Missing \"basic-user-mapping\" parameter required for basic login.");
 
         logger.info("Reading user mapping file: {}", mapFile);
-        
+
         // Parse document
         try {
 
@@ -134,18 +132,11 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
         // If no mapping available, report as such
         if (mapping == null)
             throw new GuacamoleException("User mapping could not be read.");
-        
+
         // Validate and return info for given user and pass
         AuthInfo info = mapping.get(credentials.getUsername());
-        if (info != null && info.validate(credentials.getUsername(), credentials.getPassword())) {
-            
-            //Map<String, GuacamoleConfiguration> configs = new HashMap<String, GuacamoleConfiguration>();
-            //configs.put("DEFAULT", info.getConfiguration());
-            //return configs;
-            
-            Map<String, GuacamoleConfiguration> configs = info.getConfigurations();          
-            return configs;
-        }
+        if (info != null && info.validate(credentials.getUsername(), credentials.getPassword()))
+            return info.getConfigurations();
 
         // Unauthorized
         return null;
@@ -227,15 +218,21 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
         }
 
         public GuacamoleConfiguration getConfiguration(String name) {
-            //return configs;
-            return configs.get(name);
+
+            // Create new configuration if not already in map
+            GuacamoleConfiguration config = configs.get(name);
+            if (config == null) {
+                config = new GuacamoleConfiguration();
+                configs.put(name, config);
+            }
+
+            return config;
+
         }
+
         public Map<String, GuacamoleConfiguration> getConfigurations() {
             return configs;
         }
-        public void addConfiguration(String name) {
-            configs.put(name, new GuacamoleConfiguration());
-        }
 
     }
 
@@ -250,10 +247,19 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
         private enum State {
             ROOT,
             USER_MAPPING,
-            CONNECTION,
+
+            /* Username/password pair */
             AUTH_INFO,
+
+            /* Connection configuration information */
+            CONNECTION,
             PROTOCOL,
             PARAMETER,
+
+            /* Configuration information associated with default connection */
+            DEFAULT_CONNECTION_PROTOCOL,
+            DEFAULT_CONNECTION_PARAMETER,
+
             END;
         }
 
@@ -267,59 +273,77 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
 
             switch (state)  {
 
-            case USER_MAPPING:
+                case USER_MAPPING:
 
-                if (localName.equals("user-mapping")) {
-                    state = State.END;
-                    return;
-                }
+                    if (localName.equals("user-mapping")) {
+                        state = State.END;
+                        return;
+                    }
 
-                break;
+                    break;
 
-            case AUTH_INFO:
+                case AUTH_INFO:
 
-                if (localName.equals("authorize")) {
+                    if (localName.equals("authorize")) {
 
-                    // Finalize mapping for this user
-                    authMapping.put(
-                        current.auth_username,
-                        current
-                    );
+                        // Finalize mapping for this user
+                        authMapping.put(
+                            current.auth_username,
+                            current
+                        );
 
-                    state = State.USER_MAPPING;
-                    return;
-                }
+                        state = State.USER_MAPPING;
+                        return;
+                    }
 
-                break;
-                
-            case CONNECTION:
+                    break;
 
-                if (localName.equals("connection")) {
-                    state = State.AUTH_INFO;
-                    return;
-                }
+                case CONNECTION:
 
-                break;                
+                    if (localName.equals("connection")) {
+                        state = State.AUTH_INFO;
+                        return;
+                    }
 
-            case PROTOCOL:
+                    break;
 
-                if (localName.equals("protocol")) {
-                    state = State.CONNECTION;
-                    return;
-                }
+                case PROTOCOL:
 
-                break;
+                    if (localName.equals("protocol")) {
+                        state = State.CONNECTION;
+                        return;
+                    }
 
-            case PARAMETER:
+                    break;
 
-                if (localName.equals("param")) {
-                    state = State.CONNECTION;
-                    return;
-                }
+                case PARAMETER:
 
-                break;
+                    if (localName.equals("param")) {
+                        state = State.CONNECTION;
+                        return;
+                    }
 
-        }
+                    break;
+
+                case DEFAULT_CONNECTION_PROTOCOL:
+
+                    if (localName.equals("protocol")) {
+                        state = State.AUTH_INFO;
+                        return;
+                    }
+
+                    break;
+
+                case DEFAULT_CONNECTION_PARAMETER:
+
+                    if (localName.equals("param")) {
+                        state = State.AUTH_INFO;
+                        return;
+                    }
+
+                    break;
+
+            }
 
             throw new SAXException("Tag not yet complete: " + localName);
 
@@ -375,17 +399,39 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
 
                         currentConnection = attributes.getValue("name");
                         if (currentConnection == null)
-                            throw new SAXException("Attribute \"name\" required for param tag.");
-                        
-                        current.addConfiguration(currentConnection);
-                        
+                            throw new SAXException("Attribute \"name\" required for connection tag.");
+
                         // Next state
                         state = State.CONNECTION;
                         return;
                     }
 
+                    if (localName.equals("protocol")) {
+
+                        // Associate protocol with default connection
+                        currentConnection = "DEFAULT";
+
+                        // Next state
+                        state = State.DEFAULT_CONNECTION_PROTOCOL;
+                        return;
+                    }
+
+                    if (localName.equals("param")) {
+
+                        // Associate parameter with default connection
+                        currentConnection = "DEFAULT";
+
+                        currentParameter = attributes.getValue("name");
+                        if (currentParameter == null)
+                            throw new SAXException("Attribute \"name\" required for param tag.");
+
+                        // Next state
+                        state = State.DEFAULT_CONNECTION_PARAMETER;
+                        return;
+                    }
+
                     break;
-                    
+
                 case CONNECTION:
 
                     if (localName.equals("protocol")) {
@@ -405,7 +451,7 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
                         return;
                     }
 
-                    break;                   
+                    break;
 
             }
 
@@ -417,19 +463,23 @@ public class BasicFileAuthenticationProvider implements AuthenticationProvider {
         public void characters(char[] ch, int start, int length) throws SAXException {
 
             String str = new String(ch, start, length);
-   
+
             switch (state) {
 
                 case PROTOCOL:
+                case DEFAULT_CONNECTION_PROTOCOL:
+
                     current.getConfiguration(currentConnection)
                         .setProtocol(str);
                     return;
 
                 case PARAMETER:
+                case DEFAULT_CONNECTION_PARAMETER:
+
                     current.getConfiguration(currentConnection)
                             .setParameter(currentParameter, str);
                     return;
-                
+
             }
 
             if (str.trim().length() != 0)