public static void test_RefSearch() throws Exception {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);
/* Throw exception when referral is encountered */
env.put(Context.REFERRAL, "throw");
try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
/* specify search controls to search subtree */
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
/* search for all entries with surname of Jensen */
NamingEnumeration results;
while (true) {
try {
results =
ctx.search(Env.MY_SEARCHBASE, Env.MY_FILTER, controls);
/* for each entry print out name + all attrs and values */
while (results != null && results.hasMore()) {
SearchResult si = (SearchResult) results.next();
/* print its name */
System.out.println("name: " + si.getName());
/* print each value */
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println(attrId + ": " +
vals.nextElement()))
;
}
}
System.out.println();
}
break;
}
catch (ReferralException e) {
if (!followReferral(e.getReferralInfo())) {
if (!e.skipReferral()) {
return;
}
}
/* point to the new context */
ctx = (DirContext) e.getReferralContext();
}
}
}
catch (NamingException e) {
System.err.println("RefSearch example failed.");
e.printStackTrace();
}
}
public static void test_Search() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MGR_DN); //Specify host and port to use for directory service
try {
DirContext ctx = new InitialDirContext(env); //get a handle to an Initial DirContext
SearchControls constraints = new SearchControls(); //specify search constraints to search subtree
// constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// search for all entries with surname of Jensen
NamingEnumeration results
//= ctx.search(Env.MY_SEARCHBASE, Env.MY_FILTER, constraints);
= ctx.search("dc=detech,dc=com", "(sn=developer)", constraints);
if (results != null)
System.out.println(results);
//for each entry print out name + all attrs and values
while (results != null && results.hasMoreElements()) {
System.out.println("1");
SearchResult si = (SearchResult) results.next();
System.out.println("2");
// print its name
System.out.println("name: " + si.getName());
System.out.println("name: " + si.toString());
// print each value
for (Enumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println(attrId + ": " +
vals.nextElement()))
;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void test_Create() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MY_SERVICE); //Specify host and port to use for directory service
env.put(Context.SECURITY_AUTHENTICATION, "simple");/* specify authentication information */
//env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=detech,dc=com");
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);
try {
DirContext ctx = new InitialDirContext(env); //get a handle to an Initial DirContext
ctx.createSubcontext("cn=ganjianan,dc=portal,dc=detech,dc=com",Env.getGanjiananAttributes());
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void test_DeleteAttrs() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MY_SERVICE); /* Specify host and port to use for directory service */
env.put(Context.SECURITY_AUTHENTICATION, "simple");/* specify authentication information */
//env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=detech,dc=com");
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);
try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
ctx.destroySubcontext("cn=ganjianan,dc=portal,dc=detech,dc=com");
System.out.println("delete was successful.");
}
catch (NamingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
LdapSun.test_Create();
}
}
public static void test_Modifyrdn() {
/* Values we'll use in creating the entry */
Attribute objClasses = new BasicAttribute("objectclass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("inetOrgPerson");
Attribute cn = new BasicAttribute("cn", "Jacques Smith");
Attribute sn = new BasicAttribute("sn", "Smith");
Attribute givenNames = new BasicAttribute("givenname", "Jacques");
/* Specify the DN we're adding */
String dn = "cn=Jacques Smith, " + Env.MY_MODBASE;
/* the destination DN */
String ndn = "cn=Jacques M Smith, " + Env.MY_MODBASE;
/* the new RDN */
String nrdn = "cn=Jacques M Smith";
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);
try {
/* get a handle to an Initial DirContext */
ctx = new InitialDirContext(env);
Attributes orig = new BasicAttributes();
orig.put(objClasses);
orig.put(cn);
orig.put(sn);
orig.put(givenNames);
}
catch (NameAlreadyBoundException e) {
/* If entry exists already, fine. Ignore this error. */
System.out.println("Entry " + dn +
" already exists, no need to add");
}
catch (NamingException e) {
System.err.println("Modrdn: problem adding entry." + e);
System.exit(1);
}
try {
/* Delete the destination entry, for this example */
ctx.destroySubcontext(ndn);
System.out.println("Deleted entry " + ndn + ".");
}
catch (NameNotFoundException e) {
/* If entry does not exist, fine. Ignore this error. */
System.out.println("Entry " + ndn + " is not in the directory. " +
"No need to delete.");
}
catch (NamingException e) {
System.err.println("Modrdn: problem deleting entry." + e);
System.exit(1);
}
/* Do the modrdn operation */
try {
ctx.rename(dn, ndn);
System.out.println("The modrdn operation was successful. Entry " +
dn + " has been changed to " + ndn + ".");
}
catch (NamingException e) {
System.err.println("Modify operation failed." + e);
}
}
public static void test_Rdentry() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);
try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
public static void test_Getattrs() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
/* Specify host and port to use for directory service */
env.put(Context.PROVIDER_URL, Env.MY_SERVICE);
try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
String[] attrs = new String[4];
attrs[0] = "cn"; /* Get canonical name(s) (full name) */
attrs[1] = "sn"; /* Get surname(s) (last name) */
attrs[2] = "mail"; /* Get email address(es) */
attrs[3] = "telephonenumber"; /* Get telephone number(s) */
Attributes result = ctx.getAttributes(Env.ENTRYDN, attrs);
if (result == null) {
System.out.println(Env.ENTRYDN +
"has none of the specified attributes.");
}
else {
/* print it out */
Attribute attr = result.get("cn");
if (attr != null) {
System.out.println("Full name:");
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println("\t" + vals.nextElement()))
;
}
attr = result.get("sn");
if (attr != null) {
System.out.println("Last name (surname):");
for (NamingEnumeration vals = attr.getAll();
vals.hasMoreElements();
System.out.println("\t" + vals.nextElement()))
;
}
public static void test_Modifyattrs() {
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITCTX);
env.put(Context.PROVIDER_URL, Env.MY_SERVICE); /* Specify host and port to use for directory service */
env.put(Context.SECURITY_AUTHENTICATION, "simple");/* specify authentication information */
//env.put(Context.SECURITY_PRINCIPAL, Env.MGR_DN);
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=detech,dc=com");
env.put(Context.SECURITY_CREDENTIALS, Env.MGR_PW);
try {
/* get a handle to an Initial DirContext */
DirContext ctx = new InitialDirContext(env);
/* construct the list of modifications to make */
ModificationItem[] mods = new ModificationItem[2];
Attribute mod0 = new BasicAttribute("mail", "babs@eng");
// Update mail attribute
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
// Add another value to description attribute
Attribute mod1 = new BasicAttribute("description",
"This entry was modified with the Modattrs program on " +
(new Date()).toString());
mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, mod1);
/* Delete the description attribute altogether */
/*
Attribute mod1 = new BasicAttribute("description");
mods[2] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, mod1);
*/
/* make the change */
ctx.modifyAttributes("cn=ganjianan,dc=portal,dc=detech,dc=com", mods);
System.out.println("modification was successful.");