//Warning: This code has been marked up for HTML

/**************************************************************************
*  Novell Software Developer Kit
*
*  Copyright (C) 2002-2003 Novell, Inc. All Rights Reserved.
*
*  THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
*  USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
*  ACCOMPANYING THE SOFTWARE DEVELOPER KIT (SDK) THAT CONTAINS THIS WORK.
*  PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
*  ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
*  PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
*  DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
*  PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
*  CUSTOMERS WITH RESPECT TO THIS CODE.
*
* $name:         GetReplicationFilter.java
* $description:  GetReplicationFilter.java is used to find replication filters.
******************************************************************************/
import java.util.Hashtable;
import java.util.Vector;
import java.io.IOException;
import java.util.Enumeration;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import com.sun.jndi.ldap.LdapCtx;
import com.novell.service.ndssdk.jndi.ldap.ext.GetReplicationFilterRequest;
import com.novell.service.ndssdk.jndi.ldap.ext.GetReplicationFilterResponse;
import com.novell.service.ndssdk.jndi.ldap.ext.LDAPExtendedResponse;

public class GetReplicationFilter {
    public static void main(String[] args) {

        if (args.length != 4) {
            usage();
        }

        String hostURL      = args[0];
        String loginDN      = args[1];
        String passWord     = args[2];
        String serverDN     = args[3];
        Vector filter;

        try {
           // Create a Hashtable object.

            Hashtable env = new Hashtable(5, 0.75f);

            env.put(Context.INITIAL_CONTEXT_FACTORY,
                            "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, hostURL);
            env.put(Context.SECURITY_AUTHENTICATION, "simple" );
            env.put(Context.SECURITY_PRINCIPAL, loginDN );
            env.put(Context.SECURITY_CREDENTIALS, passWord );

           // Construct an LdapContext object.

            LdapContext ctx = new InitialLdapContext(env, null);

           // Call extended operation to get replication filter.

            GetReplicationFilterRequest  reqs =
                                new GetReplicationFilterRequest( serverDN );

            GetReplicationFilterResponse resp =
                (GetReplicationFilterResponse)ctx.extendedOperation(reqs);

           // Printout the replication filter.

            filter = resp.getFilters();
            if ( filter.isEmpty() ) {
                System.out.println("\nThe server object '" + serverDN +
                                        "' contains no replication filter.");
            }
            else {
                System.out.println("\nThe server object '" + serverDN +
                                "' contains the following replication filter:");

                Enumeration enum = filter.elements();

                while ( enum.hasMoreElements() )
                    System.out.println( enum.nextElement());
            }

            System.out.println("\nGetReplicationFilter operation succeeded.");
        }
        catch (NamingException e) {
            System.err.println("\nGetReplicationFilter operation failed.");
            e.printStackTrace();
        }
        catch (IOException ioe) {
            System.err.println("\nGetReplicationFilter operation failed.");
            ioe.printStackTrace();
        }
        finally {
            System.exit(0);
        }
    }

    public static void usage() {
        System.out.println("\n Usage  : java GetReplicationFilter <host URL>"
            + " <login dn> <password> <server dn>");
        System.out.println("\n Example: java GetReplicationFilter"
            + "ldap://Acme.com:389 cn=admin,o=Acme secret"
            + "\n          cn=myServer,o=Sales");
        System.exit(1);
    }
}