Author: davsclaus
Date: Mon Nov 15 08:27:35 2010
New Revision: 1035175
URL: http://svn.apache.org/viewvc?rev=1035175&view=rev
Log:
CAMEL-3329: Added paging support for ldap. Thanks to Georg Henzler for patch.
Modified:
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
camel/trunk/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java
Modified:
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java?rev=1035175&r1=1035174&r2=1035175&view=diff
==============================================================================
---
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
(original)
+++
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapEndpoint.java
Mon Nov 15 08:27:35 2010
@@ -17,7 +17,6 @@
package org.apache.camel.component.ldap;
import java.net.URISyntaxException;
-
import javax.naming.directory.SearchControls;
import org.apache.camel.Consumer;
@@ -28,11 +27,9 @@ import org.apache.camel.impl.DefaultEndp
/**
* Represents an endpoint that synchronously invokes an LDAP server when a
producer sends a message to it.
- *
- * @version
*/
public class LdapEndpoint extends DefaultEndpoint {
- public static final String SYSTEM_DN = "ou=system";
+ public static final String SYSTEM_DN = "ou=system";
public static final String OBJECT_SCOPE = "object";
public static final String ONELEVEL_SCOPE = "onelevel";
public static final String SUBTREE_SCOPE = "subtree";
@@ -40,6 +37,8 @@ public class LdapEndpoint extends Defaul
private String remaining;
private String base = SYSTEM_DN;
private String scope = SUBTREE_SCOPE;
+ private Integer pageSize;
+ private String returnedAttributes;
protected LdapEndpoint(String endpointUri, String remaining, LdapComponent
component) throws URISyntaxException {
super(endpointUri, component);
@@ -56,7 +55,7 @@ public class LdapEndpoint extends Defaul
}
public Producer createProducer() throws Exception {
- return new LdapProducer(this, remaining, base,
toSearchControlScope(scope));
+ return new LdapProducer(this, remaining, base,
toSearchControlScope(scope), pageSize, returnedAttributes);
}
public boolean isSingleton() {
@@ -79,6 +78,22 @@ public class LdapEndpoint extends Defaul
this.scope = scope;
}
+ public int getPageSize() {
+ return pageSize;
+ }
+
+ public void setPageSize(int pageSize) {
+ this.pageSize = pageSize;
+ }
+
+ public String getReturnedAttributes() {
+ return returnedAttributes;
+ }
+
+ public void setReturnedAttributes(String returnedAttributes) {
+ this.returnedAttributes = returnedAttributes;
+ }
+
private int toSearchControlScope(String scope) {
if (scope.equalsIgnoreCase(OBJECT_SCOPE)) {
return SearchControls.OBJECT_SCOPE;
Modified:
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java?rev=1035175&r1=1035174&r2=1035175&view=diff
==============================================================================
---
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
(original)
+++
camel/trunk/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
Mon Nov 15 08:27:35 2010
@@ -17,11 +17,17 @@
package org.apache.camel.component.ldap;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.List;
import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
+import javax.naming.ldap.Control;
+import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.PagedResultsControl;
+import javax.naming.ldap.PagedResultsResponseControl;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultProducer;
@@ -31,45 +37,116 @@ import org.apache.camel.impl.DefaultProd
*/
public class LdapProducer extends DefaultProducer {
private String remaining;
- private SearchControls controls;
+ private SearchControls searchControls;
private String searchBase;
+ private Integer pageSize;
- public LdapProducer(LdapEndpoint endpoint, String remaining, String base,
int scope) throws Exception {
+ public LdapProducer(LdapEndpoint endpoint, String remaining, String base,
int scope, Integer pageSize, String returnedAttributes) throws Exception {
super(endpoint);
this.remaining = remaining;
this.searchBase = base;
- this.controls = new SearchControls();
- this.controls.setSearchScope(scope);
+ this.pageSize = pageSize;
+
+ this.searchControls = new SearchControls();
+ this.searchControls.setSearchScope(scope);
+ if (returnedAttributes != null) {
+ String returnedAtts[] = returnedAttributes.split(",");
+ if (log.isDebugEnabled()) {
+ log.debug("Setting returning Attributes to searchControls: " +
Arrays.toString(returnedAtts));
+ }
+ searchControls.setReturningAttributes(returnedAtts);
+ }
}
+
public void process(Exchange exchange) throws Exception {
String filter = exchange.getIn().getBody(String.class);
- // Obtain our ldap context. We do this by looking up the context in
our registry.
+ DirContext dirContext = getDirContext();
+
+ try {
+ // could throw NamingException
+ List<SearchResult> data;
+ if (pageSize == null) {
+ data = simpleSearch(dirContext, filter);
+ } else {
+ if (!(dirContext instanceof LdapContext)) {
+ throw new IllegalArgumentException("When using attribute
'pageSize' for a ldap endpoint, you must provide a LdapContext (subclass of
DirContext)");
+ }
+ data = pagedSearch((LdapContext) dirContext, filter);
+ }
+ exchange.getOut().setBody(data);
+ } finally {
+ dirContext.close();
+ }
+ }
+
+ protected DirContext getDirContext() {
+ // Obtain our ldap context. We do this by looking up the context in
our registry.
// Note though that a new context is expected each time. Therefore if
spring is
// being used then use prototype="scope". If you do not then you might
experience
// concurrency issues as InitialContext is not required to support
concurrency.
// On the other hand if you have a DirContext that is able to support
concurrency
// then using the default singleton scope is entirely sufficient. Most
DirContext
// classes will require prototype scope though.
- DirContext ldapContext =
(DirContext)getEndpoint().getCamelContext().getRegistry().lookup(remaining);
- try {
- // could throw NamingException
- List<SearchResult> data = new ArrayList<SearchResult>();
- NamingEnumeration<SearchResult> namingEnumeration =
ldapContext.search(searchBase, filter,
-
getControls());
+ DirContext dirContext = (DirContext)
getEndpoint().getCamelContext().getRegistry().lookup(remaining);
+ return dirContext;
+ }
- while (namingEnumeration.hasMore()) {
- data.add(namingEnumeration.next());
+ private List<SearchResult> simpleSearch(DirContext ldapContext, String
searchFilter) throws NamingException {
+ List<SearchResult> data = new ArrayList<SearchResult>();
+ NamingEnumeration<SearchResult> namingEnumeration =
ldapContext.search(searchBase, searchFilter,
+ searchControls);
+ while (namingEnumeration != null && namingEnumeration.hasMore()) {
+ data.add(namingEnumeration.next());
+ }
+ return data;
+ }
+
+ private List<SearchResult> pagedSearch(LdapContext ldapContext, String
searchFilter) throws Exception {
+ List<SearchResult> data = new ArrayList<SearchResult>();
+
+ if (log.isTraceEnabled()) {
+ log.trace("Using paged ldap search, pageSize=" + pageSize);
+ }
+
+ Control[] requestControls = new Control[]{new
PagedResultsControl(pageSize, Control.CRITICAL)};
+ ldapContext.setRequestControls(requestControls);
+ do {
+ List<SearchResult> pageResult = simpleSearch(ldapContext,
searchFilter);
+ data.addAll(pageResult);
+ if (log.isTraceEnabled()) {
+ log.trace("Page returned " + pageResult.size() + " entries");
}
- exchange.getOut().setBody(data);
- } finally {
- ldapContext.close();
+ } while (prepareNextPage(ldapContext));
+
+ if (log.isDebugEnabled()) {
+ log.debug("Found a total of " + data.size() + " entries for ldap
filter " + searchFilter);
}
+
+ return data;
}
- protected SearchControls getControls() {
- return controls;
+ private boolean prepareNextPage(LdapContext ldapContext) throws Exception {
+ Control[] responseControls = ldapContext.getResponseControls();
+
+ byte[] cookie = null;
+ if (responseControls != null) {
+ for (int i = 0; i < responseControls.length; i++) {
+ if (responseControls[i] instanceof
PagedResultsResponseControl) {
+ PagedResultsResponseControl prrc =
(PagedResultsResponseControl) responseControls[i];
+ cookie = prrc.getCookie();
+ }
+ }
+ }
+
+ if (cookie == null) {
+ return false;
+ } else {
+ ldapContext.setRequestControls(new Control[]{new
PagedResultsControl(pageSize, cookie, Control.CRITICAL)});
+ return true;
+ }
}
+
}
Modified:
camel/trunk/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java?rev=1035175&r1=1035174&r2=1035175&view=diff
==============================================================================
---
camel/trunk/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java
(original)
+++
camel/trunk/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java
Mon Nov 15 08:27:35 2010
@@ -17,6 +17,7 @@
package org.apache.camel.component.ldap;
import java.util.Collection;
+import javax.naming.directory.Attributes;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;
@@ -33,7 +34,9 @@ import org.apache.directory.server.core.
import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
import org.apache.directory.server.core.integ.FrameworkRunner;
import org.apache.directory.server.ldap.LdapServer;
+import org.junit.After;
import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -41,17 +44,16 @@ import static org.apache.directory.serve
@RunWith(FrameworkRunner.class)
@CreateLdapServer(transports = {...@createtransport(protocol = "LDAP")})
+...@applyldiffiles("org/apache/camel/component/ldap/LdapRouteTest.ldif")
public class LdapRouteTest extends AbstractLdapTestUnit {
public static LdapServer ldapServer;
-
private CamelContext camel;
private ProducerTemplate template;
private int port;
- @ApplyLdifFiles("org/apache/camel/component/ldap/LdapRouteTest.ldif")
- @Test
- public void testLdapRoute() throws Exception {
+ @Before
+ public void setup() throws Exception {
// you can assign port number in the @CreateTransport annotation
port = ldapServer.getPort();
@@ -60,9 +62,17 @@ public class LdapRouteTest extends Abstr
SimpleRegistry reg = new SimpleRegistry();
reg.put("localhost:" + port, ctx);
camel = new DefaultCamelContext(reg);
-
template = camel.createProducerTemplate();
- camel.addRoutes(createRouteBuilder());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ camel.stop();
+ }
+
+ @Test
+ public void testLdapRouteStandard() throws Exception {
+ camel.addRoutes(createRouteBuilder("ldap:localhost:" + port +
"?base=ou=system"));
camel.start();
// START SNIPPET: invoke
@@ -73,20 +83,68 @@ public class LdapRouteTest extends Abstr
// now we send the exchange to the endpoint, and receives the response
from Camel
Exchange out = template.send(endpoint, exchange);
+ Collection<SearchResult> searchResults =
defaultLdapModuleOutAssertions(out);
+
+ Assert.assertFalse(contains("uid=test1,ou=test,ou=system",
searchResults));
+ Assert.assertTrue(contains("uid=test2,ou=test,ou=system",
searchResults));
+ Assert.assertTrue(contains("uid=testNoOU,ou=test,ou=system",
searchResults));
+ Assert.assertTrue(contains("uid=tcruise,ou=actors,ou=system",
searchResults));
+ // START SNIPPET: invoke
+ }
+
+ @Test
+ public void testLdapRouteWithPaging() throws Exception {
+ camel.addRoutes(createRouteBuilder("ldap:localhost:" + port +
"?base=ou=system&pageSize=5"));
+ camel.start();
+
+ Endpoint endpoint = camel.getEndpoint("direct:start");
+ Exchange exchange = endpoint.createExchange();
+ // then we set the LDAP filter on the in body
+ exchange.getIn().setBody("(objectClass=*)");
+
+ // now we send the exchange to the endpoint, and receives the response
from Camel
+ Exchange out = template.send(endpoint, exchange);
+
+ Collection<SearchResult> searchResults =
defaultLdapModuleOutAssertions(out);
+ Assert.assertEquals(16, searchResults.size());
+ }
+
+ @Test
+ public void testLdapRouteReturnedAttributes() throws Exception {
+ // LDAP servers behave differently when it comes to what attributes
are returned
+ // by default (Apache Directory server returns all attributes by
default)
+ // Using the returnedAttributes parameter this behavior can be
controlled
+
+ camel.addRoutes(createRouteBuilder("ldap:localhost:" + port +
"?base=ou=system&returnedAttributes=uid,cn"));
+ camel.start();
+
+ Endpoint endpoint = camel.getEndpoint("direct:start");
+ Exchange exchange = endpoint.createExchange();
+ // then we set the LDAP filter on the in body
+ exchange.getIn().setBody("(uid=tcruise)");
+ // now we send the exchange to the endpoint, and receives the response
from Camel
+ Exchange out = template.send(endpoint, exchange);
+ Collection<SearchResult> searchResults =
defaultLdapModuleOutAssertions(out);
+
+ Assert.assertEquals(1, searchResults.size());
+ Assert.assertTrue(contains("uid=tcruise,ou=actors,ou=system",
searchResults));
+ Attributes theOneResultAtts =
searchResults.iterator().next().getAttributes();
+ Assert.assertEquals("tcruise", theOneResultAtts.get("uid").get());
+ Assert.assertEquals("Tom Cruise", theOneResultAtts.get("cn").get());
+
+ // make sure this att is NOT returned anymore
+ Assert.assertNull(theOneResultAtts.get("sn"));
+ }
+
+ @SuppressWarnings("unchecked")
+ private Collection<SearchResult> defaultLdapModuleOutAssertions(Exchange
out) {
// assertions of the response
Assert.assertNotNull(out);
Assert.assertNotNull(out.getOut());
Collection<SearchResult> data = out.getOut().getBody(Collection.class);
Assert.assertNotNull("out body could not be converted to a Collection
- was: " + out.getOut().getBody(), data);
-
- Assert.assertFalse(contains("uid=test1,ou=test,ou=system", data));
- Assert.assertTrue(contains("uid=test2,ou=test,ou=system", data));
- Assert.assertTrue(contains("uid=testNoOU,ou=test,ou=system", data));
- Assert.assertTrue(contains("uid=tcruise,ou=actors,ou=system", data));
- // START SNIPPET: invoke
-
- camel.stop();
+ return data;
}
protected boolean contains(String dn, Collection<SearchResult> results) {
@@ -99,11 +157,11 @@ public class LdapRouteTest extends Abstr
return false;
}
- protected RouteBuilder createRouteBuilder() throws Exception {
+ protected RouteBuilder createRouteBuilder(final String ldapEndpointUrl)
throws Exception {
return new RouteBuilder() {
// START SNIPPET: route
public void configure() throws Exception {
- from("direct:start").to("ldap:localhost:" + port +
"?base=ou=system");
+ from("direct:start").to(ldapEndpointUrl);
}
// END SNIPPET: route
};