Runtime components : Service components : LDAP Access Service : Tasks : Using the LDAP Access Service from an application
  
Using the LDAP Access Service from an application
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.
See also
Externalized input and output data
Externalized output data
No externalized data
Low-level functions
Tasks
Externalized input and output data
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.
2 Obtain the request format.
3 Obtain the response format.
4 Initialize the service.
5 Carry out the request.
The code is as follows:
Context context=(Context) ContextFactory.createContext("lookupContext");
LDAPRequestFormat reqFmt=(LDAPRequestFormat) FormatElement.readObject("lookupReqFmt");
LDAPResponseFormat resFmt=(LDAPResponseFormat) FormatElement.readObject("lookupResFmt");
service.initialize();
service.execute(context, reqFmt, resFmt);
See also
Using the LDAP Access Service from an application
Externalized output data
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.
4 Obtain a format to use in mapping the data.
5 Initialize the service.
6 Carry out the request
The code is as follows:
LDAPLookupRequest request=new LDAPLookupRequest();
request.setName("cn=John Doe,ou=Finance,o=XYZ Corp,c=US");
Context context=(Context) ContextFactory.createContext("lookupContext");
LDAPResponseFormat format=(LDAPResponseFormat) FormatElement.readObject("lookupResFmt");
service.initialize();
service.performRequest(request, context, format);
See also
Using the LDAP Access Service from an application
No externalized data
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);
See also
Using the LDAP Access Service from an application
Low-level functions
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.
See also
Using the LDAP Access Service from an application