//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/a4ad0b48-dd95-46b6-8289-721e99c8dc76/nmas_java_client/ldap_login/src/LCMTester.java //Warning: This code has been marked up for HTML

import com.novell.security.nmas.lcm.LCMEnvironment;
import com.novell.security.nmas.client.NMASCallback;
import com.novell.security.nmas.client.InvalidNMASCallbackException;
import com.novell.security.nmas.client.NMASCallbackHandler;
import com.novell.security.nmas.lcm.registry.LCMRegistryException;
import com.novell.security.nmas.lcm.registry.GenLCMRegistry;
import com.novell.security.nmas.lcm.registry.LCMRegistry;
import com.novell.security.nmas.NMASConstants;
import com.novell.security.nmas.ui.GenLcmUI;
import com.novell.security.nmas.ui.NMASTrace;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPJSSESecureSocketFactory;
import javax.swing.*;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.callback.PasswordCallback;
import java.security.Security;
import java.util.Map;
import java.util.HashMap;

public class LCMTester
{
   private String userDN = null;
   private String password = null;
   private String ldapHost = null;
   private String methodName = null;
   private String methodPath = null;
   private String sslPath = null;
   private int ldapPort = 389;
   private LDAPConnection lc = null;
   private boolean ssl = false;

   public LCMTester(String host, int port, String method, String path, String sslpath, String user, String passwd) {
          ldapHost = host;
          ldapPort = port;
     methodName = method;
     methodPath = path;
     sslPath = sslpath;
          userDN = user;
          password = passwd;
     if(sslPath.equals("") == false)
        ssl=true;
         }

    public void connectAndTest(JTextArea results)
    {
        if(ssl)// Only set up the JSSE socket factory if an SSL connecction is requested
        {
           // Dynamically set JSSE as a security provider
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

           // Dynamically set the property that JSSE uses to identify
           // the keystore that holds trusted root certificates
            System.setProperty("javax.net.ssl.trustStore", sslPath);

            lc = new LDAPConnection(new LDAPJSSESecureSocketFactory());

       results.append("Doing an SSL Bind\r\n");
       }
        else
        {
            lc = new LDAPConnection();

       results.append("Doing an Simple Bind\r\n");
        }
    
   String [] mechanisms = {"NMAS_LOGIN"};
        Map props=new HashMap();
        props.put("javax.security.sasl.client.pkgs","com.novell.sasl.client");
        props.put("LoginSequence", methodName);

        GenLcmUI lcmEnv = new GenLcmUI();
        GenLCMRegistry lcmRegistry = new GenLCMRegistry();

      try
        {
           // Add the Easy Password Login method
         lcmRegistry.registerLcm(methodPath);
        //"com.novell.security.nmas.lcm.simplestpwd.SimplestPasswordLCM"
        }
        catch(LCMRegistryException lcmRegE)
        {
            results.append("LCMRegistryException thrown:  " + lcmRegE.toString() + "\r\n");
        }

        MyCallbackHdlr cbh = new MyCallbackHdlr(lcmEnv, lcmRegistry);
//      LDAPConnection lc = new LDAPConnection();
      try
      {
         lc.connect( ldapHost, ldapPort );

         lc.bind( userDN,
                 "dn:" + userDN,
                 mechanisms,
                 props,
                 cbh);

         results.append("bind() complete\r\n");
            results.append("NMAS Return Code = " + cbh.getNmasRetCode() + "\r\n");

            if(lc.isBound())
                results.append("LDAPConnection.bind() succeeded \r\n");
            else
                results.append("LDAPConnection.bind() failed (The Connection is NOT bound)\r\n");

            if(lc.isConnected())
               results.append("The connection is connected\r\n");

            if(lc.isConnectionAlive())
                results.append("The connection is alive\r\n\n");
      }
      catch( LDAPException lde )
        {
            results.append( "Error ==> " + lde.toString() + "\r\n\n");
      }
   }

   public class MyCallbackHdlr extends NMASCallbackHandler
    {
        MyCallbackHdlr(LCMEnvironment env, LCMRegistry registry)
        {
            super(env, registry);
        }

        public void handle(Callback [] callbacks) throws UnsupportedCallbackException
        {
            for(int cbCnt = 0; cbCnt < callbacks.length; cbCnt++)
            {
               // First try to handle any core NMAS callbacks
                if(callbacks[cbCnt] instanceof NMASCallback)
                {
                    try
                    {
                        handleNMASCallback((NMASCallback) callbacks[cbCnt]);
                    }
                    catch(InvalidNMASCallbackException uce)
                    {
                       // This is not an NMAS Callback
                       // So just go on to LCM Callback Handlers
                    }
                }
                else if(callbacks[cbCnt] instanceof PasswordCallback)
                {
                    ((PasswordCallback)callbacks[cbCnt]).setPassword(password.toCharArray());
                }
                else
                {
                    throw new UnsupportedCallbackException(callbacks[cbCnt]);
                }
            }
        }
    }
}