private Vector getAttribute(String entryDN, String attr) {
    Vector v = new Vector();
    LDAPSearchResults searchResults;
    LDAPEntry entry = null;
    try {
      searchResults = connection.search(entryDN, LDAPConnection.SCOPE_BASE, "", null, false);
      entry = searchResults.next();
    } catch (LDAPException e) {
      // e.printStackTrace();
      return v;
    }

    // System.out.println(entry);
    LDAPAttributeSet attributeSet = entry.getAttributeSet();
    // System.out.println(attributeSet);
    Iterator allAttributes = attributeSet.iterator();

    while (allAttributes.hasNext()) {
      LDAPAttribute attribute = (LDAPAttribute) allAttributes.next();
      String attributeName = attribute.getName();
      if (attributeName.equalsIgnoreCase(attr)) {
        Enumeration en = attribute.getStringValues();
        for (; en.hasMoreElements(); ) {
          v.add(en.nextElement());
        }
      }
    }
    return v;
  }
Example #2
0
  public static void main(String[] args) {
    String ldapHost = "192.168.121.130";
    String loginDN = "cn=admin,dc=ucweb,dc=com";
    String password = "******";
    String containerName = "dc=ucweb,dc=com";

    int ldapPort = LDAPConnection.DEFAULT_SSL_PORT;
    int ldapVersion = LDAPConnection.LDAP_V3;

    LDAPJSSESecureSocketFactory ssf =
        new LDAPJSSESecureSocketFactory(TrustManager.createSSLSocketFactory());
    LDAPConnection lc = new LDAPConnection(ssf);

    LDAPAttributeSet attributeSet = new LDAPAttributeSet();

    attributeSet.add(
        new LDAPAttribute("objectclass", new String[] {new String("top"), new String("person")}));
    attributeSet.add(new LDAPAttribute("cn", "17"));
    attributeSet.add(new LDAPAttribute("sn", "17"));
    attributeSet.add(new LDAPAttribute("description", " "));
    //        attributeSet.add(new LDAPAttribute("userPassword", "111111"));
    String dn = "cn=17," + containerName;
    LDAPEntry newEntry = new LDAPEntry(dn, attributeSet);

    try {
      lc.connect(ldapHost, ldapPort);
      lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
      System.out.println("login ldap server successfully.");
      lc.add(newEntry);
      System.out.println("Added object: " + dn + " successfully.");

    } catch (LDAPException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } finally {
      try {
        if (lc.isConnected()) {
          lc.disconnect();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }