LDAP has been edited by Christopher Hunt (May 05, 2009).

Change summary:

Enhanced to include more information on how to use the LDAP component. Also re-wrote the examples in support of using a context that uses a prototype scoped object.

(View changes)

Content:

LDAP Component

The ldap: component allows you to perform searches in LDAP servers using filters as the message payload.
This component uses standard JNDI (javax.naming) to access the server.

URI format

ldap:ldapServerBean?options

This component only supports producer, meaning that you can not use routes with this component in the from type.


Options

Name Default Value Description
base ou=system The base DN for searches
scope subtree Search the whole subtree. Value must be one of: "object", "onelevel" or "subtree"

Result

The result is returned in the out body as a ArrayList<javax.naming.directory.SearchResult> list object with the result.

DirContext

The ldapServerBean portion of the URI refers to a DirContext bean in the registry.

Given an ldapServerBean of "ldapserver", a bean may be declared via Spring as:

<bean id="ldapserver" class="javax.naming.directory.InitialDirContext" scope="prototype">
  <constructor-arg>
    <props>
      <prop key="java.naming.factory.initial">com.sun.jndi.ldap.LdapCtxFactory</prop>
      <prop key="java.naming.provider.url">ldap://localhost:10389</prop>
      <prop key="java.naming.security.authentication">none</prop>
    </props>
  </constructor-arg>
</bean>

The above declares a regular Sun based LDAP DirContext that connects anonymously to a locally hosted LDAP server.

DirContext objects are not required to support concurrency by contract. It is therefore important that the directory context is declared as scope="prototype" (in the case when using Spring) or that the context supports concurrency. Prototype scoped objects are instantiated each time they are looked up.

Camel 1.6.1 and Camel 2.0 include a fix to support by concurrency for LDAP producers. ldapServerBean contexts are now looked up each time a request is sent to the LDAP server. In addition the contexts are released as soon as the producer completes.

Samples

Following on from the Spring configuration above, the code sample below sends an ldap request to filter search a group for a member. The common name is then extracted from the response.

ProducerTemplate<Exchange> template = exchange
  .getContext().createProducerTemplate();

Collection<?> results = (Collection<?>) (template
  .sendBody(
    "ldap:ldapserver?base=ou=mygroup,ou=groups,ou=system",
    "(member=uid=huntc,ou=users,ou=system)"));

if (results.size() > 0) {
  // Extract what we need from the device's profile

  Iterator<?> resultIter = results.iterator();
  SearchResult searchResult = (SearchResult) resultIter
      .next();
  Attributes attributes = searchResult
      .getAttributes();
  Attribute deviceCNAttr = attributes.get("cn");
  String deviceCN = (String) deviceCNAttr.get();

  ...

If no specific filter is required i.e. you just need to look an entry up, specify a wildcard filter _expression_. In the case where there is a common name use a filter _expression_ like:

(cn=*)

See Also

Reply via email to