/* 
 * Copyright (c) 2012, noxan
 * See LICENSE for details.
 */

package com.github.noxan.aves.demo;

import java.io.IOException;

import com.github.noxan.aves.auth.AuthException;
import com.github.noxan.aves.auth.accessor.UsernamePassword;
import com.github.noxan.aves.auth.accessor.UsernamePasswordAccessor;
import com.github.noxan.aves.auth.session.SessionManager;
import com.github.noxan.aves.auth.storage.InMemoryUsernamePasswordStorage;
import com.github.noxan.aves.net.Connection;
import com.github.noxan.aves.server.Server;
import com.github.noxan.aves.server.ServerAdapter;
import com.github.noxan.aves.server.SocketServer;

public class AuthServer extends ServerAdapter {
    public static void main(String[] args) {
        Server server = new SocketServer(new AuthServer());
        try {
            server.start();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    private InMemoryUsernamePasswordStorage userStorage;
    private SessionManager manager;

    public AuthServer() {
        userStorage = new InMemoryUsernamePasswordStorage();
        userStorage.addUser("noxan", "123");
        manager = new SessionManager(userStorage);
    }

    @Override
    public void readData(Connection connection, Object data) {
        String parts[] = ((String)data).trim().split(" ");
        String message = parts[0].toUpperCase();
        String[] args = new String[parts.length - 1];
        System.arraycopy(parts, 1, args, 0, args.length);
        try {
            switch(message) {
            case "LOGIN":
                if(args.length > 1) {
                    UsernamePasswordAccessor accessor = new UsernamePassword(args[0], args[1]);
                    try {
                        if(manager.requestSession(accessor, connection)) {
                            connection.write("Welcome to your session");
                        } else {
                            connection.write("LOGIN ERROR: Session");
                        }
                    } catch(AuthException e) {
                        connection.write("LOGIN ERROR: " + e.getMessage());
                    }
                } else {
                    connection.write("LOGIN ERROR: Invalid parameter(s)");
                }
                break;
            case "LOGOUT":
                connection.write("Not implemented yet!");
                break;
            }
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -1535,32 +1535,36 @@
 ) %7B%0A            
+    
 case %22LOGIN%22:%0A  
@@ -1561,16 +1561,20 @@
 LOGIN%22:%0A
+    
         
@@ -1599,24 +1599,28 @@
 ngth %3E 1) %7B%0A
+    
             
@@ -1715,32 +1715,36 @@
                 
+    
 try %7B%0A          
@@ -1757,16 +1757,20 @@
         
+   
+ 
 if(manag
@@ -1808,24 +1808,28 @@
 nection)) %7B%0A
+    
             
@@ -1905,32 +1905,36 @@
                 
+    
 %7D else %7B%0A       
@@ -1946,32 +1946,36 @@
                 
+ 
+   
 connection.write
@@ -2016,34 +2016,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -2044,32 +2044,34 @@
                 
+  
 %7D catch(AuthExce
@@ -2073,32 +2073,36 @@
 hException e) %7B%0A
+    
                 
@@ -2157,24 +2157,28 @@
 Message());%0A
+    
             
@@ -2195,32 +2195,36 @@
                 
+    
 %7D else %7B%0A       
@@ -2208,32 +2208,36 @@
        %7D else %7B%0A
+    
                 
@@ -2303,34 +2303,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
                 
@@ -2348,32 +2348,36 @@
 ak;%0A            
+   
+ 
 case %22LOGOUT%22:%0A 
@@ -2375,16 +2375,20 @@
 OGOUT%22:%0A
+    
         
@@ -2433,24 +2433,28 @@
 ted yet!%22);%0A
+    
             

fix indention of switch statement in auth server