package lava.net.psyc.packages; import java.util.Hashtable; import lava.net.common.UNL; import lava.net.common.Value; import lava.net.psyc.PSYCDeliveryException; import lava.net.psyc.PSYCMessageCenter; import lava.net.psyc.PSYCPackage; /** * **/ public class Authentication extends Skeleton { /** * **/ public final static String PackageName = "Authentication"; /** * **/ public final static String passwordTag = "_password"; /** * **/ protected final static String[] passwordAliases = {"password"}; /** * **/ public final static String queryPasswordTag = "_query_password"; /** * **/ protected final static String[] queryPasswordAliases = {"query_password"}; /** * **/ public final static String setPasswordTag = "_set_password"; /** * **/ protected final static String[] setPasswordAliases = {"set_password"}; /** * **/ public final static String errorInvalidPasswordTag = // "_error_invalid_password"; /** * **/ protected final static String[] errorInvalidPasswordAliases = // {"error_invalid_password"}; class DummyListener implements AuthenticationListener { /** * **/ public String authenticationGetPassword(UNL server, String body) // throws AuthenticationWontGivePasswordException { return null; } /** * **/ public void authenticationInvalidPassword(UNL server, String body) { } } /** * **/ private AuthenticationListener listener = new DummyListener(); /** * **/ private Hashtable vars = new Hashtable(); /** * **/ private Hashtable passwordSent = new Hashtable(); /** * **/ public Authentication(AuthenticationListener listener) { super(); setPackageName(PackageName); addPackageMethod(queryPasswordTag,queryPasswordAliases,true); addPackageMethod(setPasswordTag,setPasswordAliases,false); addPackageMethod(errorInvalidPasswordTag,errorInvalidPasswordAliases,// true); addPackageVariable(passwordTag,passwordAliases,false); if(listener != null) this.listener = listener; } /** * **/ public boolean understands() { return !(listener instanceof DummyListener); } /** * **/ public void received(UNL source, String method, String body) { // We'll ever get the method name we've registered, // even if a submethod were called if(errorInvalidPasswordTag.equals(method)) { if(havePasswordSent(source)) { removePasswordSent(source); listener.authenticationInvalidPassword(source,body); } } else if(queryPasswordTag.equals(method)) { try { vars.clear(); vars.put(passwordTag,new Value(// listener.authenticationGetPassword(source,body))); try { center.sendChecked(source,// AuthenticationServer.PackageName,setPasswordTag,null,// vars); addPasswordSent(source); } catch(PSYCDeliveryException e) { } } catch(AuthenticationWontGivePasswordException e) { // TODO: send some error if we don't want send an password } } } /** * **/ public boolean havePasswordSent(UNL server) { return passwordSent.get(server) != null; } /** * **/ public void addPasswordSent(UNL server) { passwordSent.put(server,server); } /** * **/ public void removePasswordSent(UNL server) { passwordSent.remove(server); } /** * **/ public void sendPassword(UNL server, String password) // throws PSYCDeliveryException { vars.clear(); vars.put(passwordTag,new Value(password)); center.sendChecked(server,AuthenticationServer.PackageName,// setPasswordTag,null,vars); addPasswordSent(server); } }