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.
ldap:ldapServerUrl?options
This component only supports producer, meaning that you can not use routes with this component in the from
type.
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" |
The result is returned in the out body as a ArrayList<javax.naming.directory.SearchResult>
list object with the result.
In the sample below we fetch the rows from the customer table.
First we register our datasource in the Camel registry as testdb:
JndiRegistry reg = super.createRegistry(); reg.bind("localhost:" + port, getWiredContext()); return reg;
Then we configure a route that routes to the LDAP component so the search will be executed, notice that we refer to the LdapContext that was bound in the previous step:
public void configure() throws Exception { from("direct:start").to("ldap:localhost:" + port + "?base=ou=system"); }
And then we create the endpoint and sends the exchange containing the LDAP filter to execute in the in body. The result is returned in the out body.
Endpoint endpoint = context.getEndpoint("direct:start"); Exchange exchange = endpoint.createExchange(); // then we set the LDAP filter on the in body exchange.getIn().setBody("(!(ou=test1))"); // now we send the exchange to the endpoint, and receives the response from Camel Exchange out = template.send(endpoint, exchange); // assertions of the response assertNotNull(out); assertNotNull(out.getOut()); Collection<SearchResult> data = out.getOut().getBody(Collection.class); assertNotNull("out body could not be converted to a Collection - was: " + out.getOut().getBody(), data); assertFalse(contains("uid=test1,ou=test,ou=system", data)); assertTrue(contains("uid=test2,ou=test,ou=system", data)); assertTrue(contains("uid=testNoOU,ou=test,ou=system", data)); assertTrue(contains("uid=tcruise,ou=actors,ou=system", data)); } @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry reg = super.createRegistry(); reg.bind("localhost:" + port, getWiredContext()); return reg; } @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { from("direct:start").to("ldap:localhost:" + port + "?base=ou=system"); } }; } }
If we want to poll an LDAP server using this component we need to combine this with a polling scheduler such as the Timer or Quartz etc. In this sample we retrieve data every 60th seconds.
from("timer://foo?period=60000").setBody(constant("(o=apache)")). to("ldap:localhost:1024").to("activemq:queue:committers");