2

How to check whether the UID are present already. If it's present increment one value for the new user with 3 letters of first name and last name. If UId are not present Assign the value of UID ...and store in eDirectory..

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

    Hashtable env = new Hashtable();
    String adminName = "cn=admin,o=novell";
    String adminPassword = "Happiest1";
    String ldapURL = "ldaps://10.18.26.192:636";

    //Access the keystore, this is where the Root CA public key cert was installed
    //Could also do this via the command line option java -Djavax.net.ssl.trustStore....
    //No need to specifiy the keystore password for read operations
    String keystore = "C:\\Users\\durga.tiruvakkad\\Desktop\\cert.keystore";
    System.setProperty("javax.net.ssl.trustStore",keystore);

    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

    //set security credentials
    env.put(Context.SECURITY_AUTHENTICATION,"simple");
    env.put(Context.SECURITY_PRINCIPAL,adminName);
    env.put(Context.SECURITY_CREDENTIALS,adminPassword);

    //specify use of ssl
    env.put(Context.SECURITY_PROTOCOL,"ssl");

    //connect to my domain controller
    env.put(Context.PROVIDER_URL,ldapURL);
    try {

        // Create the initial directory context
        DirContext ctx = new InitialLdapContext(env,null);

        //Create the search controls        
        SearchControls searchCtls = new SearchControls();

        //Specify the attributes to return
        String returnedAtts[]={"sn","givenName","UID"};
        searchCtls.setReturningAttributes(returnedAtts);

        //Specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        //specify the LDAP search filter
        String searchFilter = "(&(objectClass=user)(UID=*))";

        //Specify the Base for the search
        String searchBase = "ou=FWCMS,o=novell";

        //initialize counter to total the results
        int totalResults = 0;


        // Search for objects using the filter
        NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);

        //Loop through the search results
        while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult)answer.next();

            totalResults++;

            System.out.println(">>>" + sr.getName());

            // Print out some of the attributes, catch the exception if the attributes have no values
            Attributes attrs = sr.getAttributes();
            if (attrs != null) {
                try {
                System.out.println("   surname: " + attrs.get("sn").get());
                System.out.println("   firstname: " + attrs.get("givenName").get());
                System.out.println("   UID: " + attrs.get("UID").get());
                                    } 
                catch (NullPointerException e)  {
                System.out.println("Errors listing attributes: " + e);
                }
            }

        }

        System.out.println("Total results: " + totalResults);
        ctx.close();

    } 
    catch (NamingException e) {
        System.err.println("Problem searching directory: " + e);
    }
    }
}

}

if the name oof the user same so it must check in search and if the user is present it must increment its value

vimuth
  • 5,064
  • 33
  • 79
  • 116
DURGA
  • 63
  • 1
  • 1
  • 10

2 Answers2

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

    Hashtable env = new Hashtable();
    String adminName = "cn=admin,o=novell";
    String adminPassword = "Happiest1";
    String ldapURL = "ldaps://10.18.26.192:636";

    // Access the keystore, this is where the Root CA public key cert was
    // installed
    // Could also do this via the command line option java
    // -Djavax.net.ssl.trustStore....
    // No need to specifiy the keystore password for read operations
    String keystore = "C:\\Users\\durga.tiruvakkad\\Desktop\\cert.keystore";
    System.setProperty("javax.net.ssl.trustStore", keystore);

    env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");

    // set security credentials
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, adminName);
    env.put(Context.SECURITY_CREDENTIALS, adminPassword);

    // specify use of ssl
    env.put(Context.SECURITY_PROTOCOL, "ssl");

    // connect to my domain controller
    env.put(Context.PROVIDER_URL, ldapURL);
    try {

        // Create the initial directory context
        DirContext ctx = new InitialLdapContext(env, null);

        Attribute attr = ctx.getAttributes("sn").get("UID");
        String uid = (String) attr.get();

        // Create the search controls
        SearchControls searchCtls = new SearchControls();

        // Specify the attributes to return
        String returnedAtts[] = { "sn", "givenName", "UID" };
        searchCtls.setReturningAttributes(returnedAtts);

        // Specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        // specify the LDAP search filter
        String searchFilter = "(&(objectClass=user)(UID=*))";

        // Specify the Base for the search
        String searchBase = "ou=FWCMS,o=novell";

        // initialize counter to total the results
        int totalResults = 0;

        // Search for objects using the filter
        NamingEnumeration answer = ctx.search(searchBase, searchFilter,
                searchCtls);

        // Loop through the search results
        while (answer.hasMoreElements()) {
            SearchResult sr = (SearchResult) answer.next();

            totalResults++;

            System.out.println(">>>" + sr.getName());

            // Print out some of the attributes, catch the exception if the
            // attributes have no values
            Attributes attrs = sr.getAttributes();
            if (attrs != null) {
                try {
                    int count = 0;
                    System.out.println("   surname: "
                            + attrs.get("sn").get());

                    String lastName = (String) attrs.get("sn").get();

                    System.out.println("   firstname: "
                            + attrs.get("givenName").get());

                    String firstName = (String) attrs.get("givenName")
                            .get();

                    if (attrs.get("UID").toString().contains("GIVENUID")) {
                        count++;
                        attrs.get("UID").get().toString()
                                .concat(firstName.substring(0, 3))
                                .concat(lastName.substring(0, 3))
                                .concat(String.valueOf(count));
                    }
                    System.out.println("   UID: " + attrs.get("UID").get());

                } catch (NullPointerException e) {
                    System.out.println("Errors listing attributes: " + e);
                }
            }

        }

        System.out.println("Total results: " + totalResults);
        ctx.close();

    } catch (NamingException e) {
        System.err.println("Problem searching directory: " + e);
    }
}

}

Code
  • 184
  • 1
  • 8
  • i am not so thorough with jndi concept but then too tried to solve your problem :) – Code May 16 '13 at 08:14
  • i m getting this error Problem searching directory: javax.naming.InvalidNameException: sn: [LDAP: error code 34 - Invalid DN Syntax]; remaining name 'sn' – DURGA May 16 '13 at 10:08
  • may be there is some syntax error that you are not getting through, i am newbie to jndi could not tell you much about it – Code May 16 '13 at 10:19
  • ok friend thanks i need one more help that i will say you the concept u can help me in this 1.i have to get fn and LN from xml 2.generate base uid based upon fn and Ln 3.we have to generate the uniqueid from baseid and we have to search in eDirectory whethet UID =BaseID 4.the search result must store in list obj 5.check the unique uiD is exists in list object 6.if exists we have to incriment +1 otherwise use the UId – DURGA May 17 '13 at 05:24
  • i can able to get fn and ln from xml file. and i can generate UID using the fn and ln – DURGA May 17 '13 at 07:21
1
public class searchattribute {
public static void main(String[] args) {

    Hashtable env = new Hashtable();
    String adminName = "cn=admin,o=novell";
    String adminPassword = "Happiest1";
    String ldapURL = "ldaps://10.18.26.192:636";

    // Access the keystore, this is where the Root CA public key cert was
    // installed
    // Could also do this via the command line option java
    // -Djavax.net.ssl.trustStore....
    // No need to specifiy the keystore password for read operations
    String keystore = "C:\\Users\\durga.tiruvakkad\\Desktop\\cert.keystore";
    System.setProperty("javax.net.ssl.trustStore", keystore);

    env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");

    // set security credentials
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, adminName);
    env.put(Context.SECURITY_CREDENTIALS, adminPassword);

    // specify use of ssl
    env.put(Context.SECURITY_PROTOCOL, "ssl");

    // connect to my domain controller
    env.put(Context.PROVIDER_URL, ldapURL);
    try {

        // Create the initial directory context
        DirContext ctx = new InitialLdapContext(env, null);

        Attribute attr = ctx.getAttributes("sn").get("UID");
        String uid = (String) attr.get();

        // Create the search controls
        SearchControls searchCtls = new SearchControls();

        // Specify the attributes to return
        String returnedAtts[] = { "sn", "givenName", "UID" };
        searchCtls.setReturningAttributes(returnedAtts);

        // Specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

        // specify the LDAP search filter
        String searchFilter = "(&(objectClass=user)(UID=*))";

        // Specify the Base for the search
        String searchBase = "ou=FWCMS,o=novell";

        // initialize counter to total the results
        int totalResults = 0;

        // Search for objects using the filter
        NamingEnumeration answer = ctx.search(searchBase, searchFilter,
                searchCtls);

        // Loop through the search results
        while (answer.hasMoreElements()) {
            SearchResult sr = (SearchResult) answer.next();

            totalResults++;

            System.out.println(">>>" + sr.getName());

            List<Set<String>> getUIDs = new ArrayList<Set<String>>();

            Set<String> getString = new HashSet<String>();
            // Print out some of the attributes, catch the exception if the
            // attributes have no values
            Attributes attrs = sr.getAttributes();
            if (attrs != null) {
                try {

                    System.out.println("   surname: "
                            + attrs.get("sn").get());

                    String lastName = (String) attrs.get("sn").get();

                    System.out.println("   firstname: "
                            + attrs.get("givenName").get());

                    String firstName = (String) attrs.get("givenName")
                            .get();

                    if (attrs.get("UID").toString().contains("GIVENUID")) {

                        String uidString = attrs.get("UID").get()
                                .toString()
                                .concat(firstName.substring(0, 3))
                                .concat(lastName.substring(0, 3));
                        getString.add(uidString);  //changes
                        getUIDs.add(getString);    // changes
                    }
                    System.out.println("   UID: " + attrs.get("UID").get());

                } catch (NullPointerException e) {
                    System.out.println("Errors listing attributes: " + e);
                }
            }

        }

        System.out.println("Total results: " + totalResults);
        ctx.close();

    } catch (NamingException e) {
        System.err.println("Problem searching directory: " + e);
    }
}

public static String searchInList(List<Set<String>> list, String uidStr) {  // new function
    int counter = 0;
    if (list != null) {
        if (list.equals(uidStr)) {
            counter++;
            uidStr.concat(String.valueOf(counter));
            return uidStr;
        }
    }
    return uidStr;
}

}

Code
  • 184
  • 1
  • 8
  • you could make use of searchInList function for searching wether list contains the object or not , i have tried to resolve your issue since i am not expert :) – Code May 17 '13 at 08:12