There are four ways to use the LDAP Access Service. The four examples below are for a lookup request, but the concepts shown may be applied to all request types. In each of these examples, it is assumed that an instance of LDAPService has been properly configured.
Data for carrying out requests is defined in the toolkit context definition file, and is converted for use with toolkit formatters that are defined in the toolkit formats definition file. To use the LDAP Access Service with externalized data, do the following:
1 Obtain the context that contains the data definitions.
An application can populate a request model itself, and then have data returned by the request mapped into a toolkit context. To use the LDAP Access Service with externalized output data, do the following:
1 Obtain a request model.
2 Populate the request model with data.
3 Obtain a context into which the response data will be mapped.
For applications that do not externalize input or output data, request models must be created and populated, and response data must be explicitly handled. In this case, do the following:
1 Obtain a request model.
2 Populate the request model with data.
3 Initialize the service.
4 Carry out the request.
5 Obtain the response model.
6 Use response data.
The code is as follows:
LDAPLookupRequest request = new LDAPLookupRequest(); request.setName("cn=John Doe,ou=Finance,o=XYZ Corp,c=US"); LDAPService service = (LDAPService)Service.readObject("aLdapService"); service.initialize(); LDAPResponse response = service.performRequest(request);
(At this point the application can read and use the result of the request from the response object.)
The following is a complete example of utilizing the LDAP Access Service using no externalized data:
LDAPService aService = null; LDAPRequest request = null; LDAPResponse = null; /* configure the service */ aService.setAuthenticationCredentials("password"); aService.setAuthenticationMethod("simple"); aService.setAuthenticationPrincipal("cn=root"); aService.setProviderFactory("com.sun.jndi.ldap.LdapCtxFactory"); aService.setProviderUrl("ldap://localhost:389"); /* obtain request model */ request = new LDAPLookupRequest(); /* set input data */ request.setName("cn=Ted Geisel,ou=Finance,o=XYZ Corp,c=US"); /* utilize the service */ aService.initialize(); response = aService.performRequest(request);
The LDAP Access Service encapsulates a subset of the functions provided by JNDI. These low-level functions can be used by applications that require low-level control of LDAP.