Example usage for javax.naming.ldap LdapName LdapName

List of usage examples for javax.naming.ldap LdapName LdapName

Introduction

In this page you can find the example usage for javax.naming.ldap LdapName LdapName.

Prototype

public LdapName(List<Rdn> rdns) 

Source Link

Document

Constructs an LDAP name given its parsed RDN components.

Usage

From source file:Main.java

public static void main(String args[]) {
    try {//from   w  w w  .  jav a 2 s . co m
        LdapName dn = new LdapName("ou=Fruits,o=Food");
        LdapName dn2 = new LdapName("ou=Summer");
        System.out.println(dn.addAll(dn2));
        System.out.println(dn.add(0, "o=Resources"));
        System.out.println(dn.add("cn=WaterMelon"));
        System.out.println(dn.remove(1));
        System.out.println(dn);
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) {
    try {//from   w  w  w  .  j  a v a 2  s.  c o m
        LdapName one = new LdapName("cn=Abc Def, ou=People, o=JNDITutorial");
        LdapName two = new LdapName("cn=Abc Def");
        LdapName three = new LdapName("o=JNDITutorial");
        LdapName four = new LdapName("");

        System.out.println(one.equals(two));
        System.out.println(one.startsWith(three));
        System.out.println(one.endsWith(two));
        System.out.println(one.startsWith(four));
        System.out.println(one.endsWith(four));
        System.out.println(one.endsWith(three));
        System.out.println(one.isEmpty());
        System.out.println(four.isEmpty());
        System.out.println(four.size() == 0);
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:ModifyLdapName.java

public static void main(String args[]) {
    try {//from w ww  .  j ava 2 s .c  om
        LdapName dn = new LdapName("ou=Fruits,o=Food");
        LdapName dn2 = new LdapName("ou=Summer");
        System.out.println(dn.addAll(dn2)); // ou=Summer,ou=Fruits,o=Food
        System.out.println(dn.add(0, "o=Resources")); // ou=Summer,ou=Fruits,o=Food,o=Resources
        System.out.println(dn.add("cn=WaterMelon")); // cn=WaterMelon,ou=Summer,ou=Fruits,o=Food,o=Resources
        System.out.println(dn.remove(1)); // o=Food
        System.out.println(dn); // cn=WaterMelon,ou=Summer,ou=Fruits,o=Resources
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:CompareLdapNames.java

public static void main(String args[]) {
    try {//from ww w  . j a v a  2  s .c o  m
        LdapName one = new LdapName("cn=Vincent Ryan, ou=People, o=JNDITutorial");
        LdapName two = new LdapName("cn=Vincent Ryan");
        LdapName three = new LdapName("o=JNDITutorial");
        LdapName four = new LdapName("");

        System.out.println(one.equals(two)); // false
        System.out.println(one.startsWith(three)); // true
        System.out.println(one.endsWith(two)); // true
        System.out.println(one.startsWith(four)); // true
        System.out.println(one.endsWith(four)); // true
        System.out.println(one.endsWith(three)); // false
        System.out.println(one.isEmpty()); // false
        System.out.println(four.isEmpty()); // true
        System.out.println(four.size() == 0); // true
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:LdapNametoString.java

public static void main(String args[]) {
    String name = "cn=JuicyFruit, ou=Fruits";
    try {/*  ww  w  .  ja v a  2 s  .c  om*/
        LdapName dn = new LdapName(name);
        String str = dn.toString();
        System.out.println(str);
        LdapName dn2 = new LdapName(str);
        System.out.println(dn.equals(dn2));
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:ConstructLdapName.java

public static void main(String args[]) {
    String name = "cn=Mango, ou=Fruits, o=Food";
    try {/*  w w  w .  j av a 2 s  . c o  m*/
        LdapName dn = new LdapName(name);
        System.out.println(dn + " has " + dn.size() + " RDNs: ");
        for (int i = 0; i < dn.size(); i++) {
            System.out.println(dn.get(i));
        }
    } catch (InvalidNameException e) {
        System.out.println("Cannot parse name: " + name);
    }
}

From source file:LdapNameSuffixPrefix.java

public static void main(String args[]) {
    String name = "cn=Mango, ou=Fruits, o=Food";
    try {//from  w ww  .  j a  v a  2 s.  com
        LdapName dn = new LdapName(name);
        Name suffix = dn.getSuffix(1); // 1 <= index < cn.size()
        Name prefix = dn.getPrefix(1); // 0 <= index < 1
        System.out.println(suffix);
        System.out.println(prefix);
    } catch (InvalidNameException e) {
        System.out.println("Cannot parse name: " + name);
    }
}

From source file:EscapingDNs.java

public static void main(String args[]) {
    try {//from   w  w w . j  a v a2  s .co  m
        // DN with ',' (comma) in its attribute value
        String unformatted = "Juicy, Fruit";
        String formatted = Rdn.escapeValue(unformatted);
        LdapName dn = new LdapName("cn=" + formatted);
        System.out.println("dn:" + dn);

        // DN with a '+' (plus) in its attribute value
        unformatted = "true+false";
        formatted = Rdn.escapeValue(unformatted);
        dn = new LdapName("cn=" + formatted);
        System.out.println("dn:" + dn);

        // DN with a binary value as its attribute value
        byte[] bytes = new byte[] { 1, 2, 3, 4 };
        formatted = Rdn.escapeValue(bytes);
        System.out.println("Orig val: " + bytes + " Escaped val: " + formatted);
    } catch (InvalidNameException e) {
        e.printStackTrace();
    }
}

From source file:LookupLdapName.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/");

    try {//w  w  w .  ja  v a 2s . com
        // Create the initial context
        Context ctx = new InitialContext(env);

        // A string representation of an LDAP name
        LdapName dn = new LdapName("ou=People,o=JNDITutorial");

        // Perform the lookup using the dn
        Object obj = ctx.lookup(dn);

        System.out.println(obj);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Lookup failed: " + e);
    }
}

From source file:RetrievingLdapName.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//  w  w w . j  a v a  2  s.  com
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        NamingEnumeration answer = ctx.search("ou=People", null);

        // Print the answer
        while (answer.hasMore()) {
            SearchResult sr = (SearchResult) answer.next();
            String name = sr.getNameInNamespace();
            System.out.println(name);
            LdapName dn = new LdapName(name);

            // do something with the dn
        }

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}