directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robert Hou (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (DIRAPI-189) Support DirSync control
Date Thu, 17 Apr 2014 10:21:15 GMT

    [ https://issues.apache.org/jira/browse/DIRAPI-189?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13972820#comment-13972820
] 

Robert Hou commented on DIRAPI-189:
-----------------------------------

Only set AdDirSyncImpl control in the SearchRequest is right.
Looks like Apache API has some issues to handle request control and response control when
they have same OID. Even though I don't know how should API know which side it is on, as far
as I know JNDI has the correct implementation for this from the test. Please refer to following
codes:

import java.io.*;
import java.util.Hashtable;

import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;

import com.sun.jndi.ldap.ctl.*;

public class ModChanges4SpecifiedFields {
	public static void main(String[] args) {
		Hashtable env = new Hashtable();
		String adminName = "cn=robert,cn=roles,dc=tibco,dc=com";
		String adminPassword = "robert";
		String ldapURL = "ldap://192.168.80.223:50001";
		env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
		// set security credentials, note using simple cleartext authentication
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL, adminName);
		env.put(Context.SECURITY_CREDENTIALS, adminPassword);

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

		try {
			// Create the initial directory context
			LdapContext ctx = new InitialLdapContext(env, null);
			// Create the search controls
			SearchControls searchCtls = new SearchControls();
			// Specify the attributes to return, null initially return all
			// values
			// and in subsequent calls, null returns all modified attributes
			String returnedAtts[] = {};
			searchCtls.setReturningAttributes(returnedAtts);
			// Specify the search scope
			searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
			// specify the LDAP search filter
			String searchFilter = "(&(objectClass=user))";
			// Specify the Base for the search
			String searchBase = "DC=tibco,DC=com";
			// initialize counter to total the results
			int totalResults = 0;
			// Specify the DirSync and DirSyncResponse controls
			byte[] cookie = null;
			Control[] rspCtls;
			Control[] ctls = new Control[] { new DirSyncControl() };
			ctx.setRequestControls(ctls);
			// 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());
				// First time around, no need to print any attributes
			}

			System.out.println("Current results: " + totalResults);

			// save the response controls
			if ((rspCtls = ctx.getResponseControls()) != null) {
				System.out.println("Response Controls: " + rspCtls.length);
				for (int i = 0; i < rspCtls.length; i++) {
					if (rspCtls[i] instanceof DirSyncResponseControl1) {
						DirSyncResponseControl1 rspCtl = (DirSyncResponseControl1) rspCtls[i];
						cookie = rspCtl.getCookie();
						System.out.println("Flag: " + rspCtl.getFlag());
						System.out.println("Length: "+ rspCtl.getMaxReturnLength());
						System.out.println("Cookie: " + cookie.toString());

					}

				}

			}
			// Now start a loop, prompt the user to continue/quit and continue
			// searching for modified objects

			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

			while (true) {
				try {
					String input;
					while (true) {
						System.out.println("Press x to exit, return to continue");
						input = in.readLine();
						if (input.startsWith("x") || input.startsWith("X")) {
							ctx.close();
							System.exit(0);
						}
						else {
							break;
						}

					}

				}

				catch (IOException e) {
					System.err.println();
				}
				// now lets do the search again using the cookie
				totalResults = 0;
				// use the saved cookie in the DirSync control
				ctx.setRequestControls(new Control[] { new DirSyncControl1(1,Integer.MAX_VALUE, cookie,
true) });
				// execute the search again
				String returnedAtts1[] = {"description"};
				searchCtls.setReturningAttributes(returnedAtts1);
				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 the modified attributes
					// instanceType and objectGUID are always returned
					Attributes attrs = sr.getAttributes();
					if (attrs != null) {
						try {
							for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
								Attribute attr = (Attribute) ae.next();
								System.out.println("Attribute: " + attr.getID());
								for (NamingEnumeration e = attr.getAll(); e.hasMore(); 
										System.out.println(" "+ e.next().toString()));
							}

						}
						catch (NullPointerException e) {
							System.err.println();
						}
					}

				}
				System.out.println("Recently Modified: " + totalResults);
				// Save the response control again
				if ((rspCtls = ctx.getResponseControls()) != null) {
					System.out.println("Response Controls: " + rspCtls.length);
					for (int i = 0; i < rspCtls.length; i++) {
						if (rspCtls[i] instanceof DirSyncResponseControl1) {
							DirSyncResponseControl1 rspCtl = (DirSyncResponseControl1) rspCtls[i];
							cookie = rspCtl.getCookie();
							System.out.println("Flag: " + rspCtl.getFlag());
							System.out.println("Length: "+ rspCtl.getMaxReturnLength());
							System.out.println("Cookie: " + cookie.toString());
						}

					}

				}

			}

		}

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

	}

}


> Support DirSync control
> -----------------------
>
>                 Key: DIRAPI-189
>                 URL: https://issues.apache.org/jira/browse/DIRAPI-189
>             Project: Directory Client API
>          Issue Type: Wish
>    Affects Versions: 1.0.0-M21
>            Reporter: Robert Hou
>             Fix For: 1.0.0-M22
>
>
> I originally wanted to impletent DirSync Control using Apache API. After I looked into
existing Controls under  org.apache.directory.api.ldap.model.message.controls. I think it's
difficult to me. I can't understand the decoding and encoding of controls specification of
Apache. So I want Apache team give implementation for this control. Details for this control,
please refer to http://msdn.microsoft.com/en-us/library/ms677626(v=vs.85).aspx.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message