You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cm...@apache.org on 2012/05/09 22:55:02 UTC

svn commit: r1336392 - in /camel/branches/camel-2.9.x/components/camel-ldap/src: main/java/org/apache/camel/component/ldap/LdapProducer.java test/java/org/apache/camel/component/ldap/LdapRouteTest.java

Author: cmueller
Date: Wed May  9 20:55:02 2012
New Revision: 1336392

URL: http://svn.apache.org/viewvc?rev=1336392&view=rev
Log:
CAMEL-5260: LDAP Component lose headers

Modified:
    camel/branches/camel-2.9.x/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
    camel/branches/camel-2.9.x/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java

Modified: camel/branches/camel-2.9.x/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java?rev=1336392&r1=1336391&r2=1336392&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java (original)
+++ camel/branches/camel-2.9.x/components/camel-ldap/src/main/java/org/apache/camel/component/ldap/LdapProducer.java Wed May  9 20:55:02 2012
@@ -62,7 +62,6 @@ public class LdapProducer extends Defaul
 
     public void process(Exchange exchange) throws Exception {
         String filter = exchange.getIn().getBody(String.class);
-
         DirContext dirContext = getDirContext();
 
         try {
@@ -77,6 +76,8 @@ public class LdapProducer extends Defaul
                 data = pagedSearch((LdapContext) dirContext, filter);
             }
             exchange.getOut().setBody(data);
+            exchange.getOut().setHeaders(exchange.getIn().getHeaders());
+            exchange.getOut().setAttachments(exchange.getIn().getAttachments());
         } finally {
             dirContext.close();
         }
@@ -96,8 +97,7 @@ public class LdapProducer extends Defaul
 
     private List<SearchResult> simpleSearch(DirContext ldapContext, String searchFilter) throws NamingException {
         List<SearchResult> data = new ArrayList<SearchResult>();
-        NamingEnumeration<SearchResult> namingEnumeration = ldapContext.search(searchBase, searchFilter,
-                searchControls);
+        NamingEnumeration<SearchResult> namingEnumeration = ldapContext.search(searchBase, searchFilter, searchControls);
         while (namingEnumeration != null && namingEnumeration.hasMore()) {
             data.add(namingEnumeration.next());
         }

Modified: camel/branches/camel-2.9.x/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java?rev=1336392&r1=1336391&r2=1336392&view=diff
==============================================================================
--- camel/branches/camel-2.9.x/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java (original)
+++ camel/branches/camel-2.9.x/components/camel-ldap/src/test/java/org/apache/camel/component/ldap/LdapRouteTest.java Wed May  9 20:55:02 2012
@@ -17,6 +17,8 @@
 package org.apache.camel.component.ldap;
 
 import java.util.Collection;
+
+import javax.activation.DataHandler;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.SearchResult;
 import javax.naming.ldap.LdapContext;
@@ -24,6 +26,7 @@ import javax.naming.ldap.LdapContext;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultCamelContext;
@@ -35,12 +38,17 @@ import org.apache.directory.server.core.
 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;
 
 import static org.apache.directory.server.integ.ServerIntegrationUtils.getWiredContext;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 
 @RunWith(FrameworkRunner.class)
 @CreateLdapServer(transports = {@CreateTransport(protocol = "LDAP")})
@@ -85,10 +93,10 @@ public class LdapRouteTest extends Abstr
         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));
+        assertFalse(contains("uid=test1,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=test2,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=testNoOU,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=tcruise,ou=actors,ou=system", searchResults));
         // START SNIPPET: invoke
     }
 
@@ -106,7 +114,7 @@ public class LdapRouteTest extends Abstr
         Exchange out = template.send(endpoint, exchange);
 
         Collection<SearchResult> searchResults = defaultLdapModuleOutAssertions(out);
-        Assert.assertEquals(16, searchResults.size());
+        assertEquals(16, searchResults.size());
     }
 
     @Test
@@ -127,23 +135,47 @@ public class LdapRouteTest extends Abstr
         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));
+        assertEquals(1, searchResults.size());
+        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());
+        assertEquals("tcruise", theOneResultAtts.get("uid").get());
+        assertEquals("Tom Cruise", theOneResultAtts.get("cn").get());
 
         // make sure this att is NOT returned anymore 
-        Assert.assertNull(theOneResultAtts.get("sn"));
+        assertNull(theOneResultAtts.get("sn"));
+    }
+    
+    @Test
+    public void testLdapRoutePreserveHeaderAndAttachments() throws Exception {
+        camel.addRoutes(createRouteBuilder("ldap:localhost:" + port + "?base=ou=system"));
+        camel.start();
+
+        final DataHandler dataHandler = new DataHandler("test", "text");
+        Exchange out = template.request("direct:start", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("(!(ou=test1))");
+                exchange.getIn().setHeader("ldapTest", "Camel");
+                exchange.getIn().addAttachment("ldapAttachment", dataHandler);
+            }
+        });
+        
+        Collection<SearchResult> searchResults = defaultLdapModuleOutAssertions(out);
+
+        assertFalse(contains("uid=test1,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=test2,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=testNoOU,ou=test,ou=system", searchResults));
+        assertTrue(contains("uid=tcruise,ou=actors,ou=system", searchResults));
+        assertEquals("Camel", out.getOut().getHeader("ldapTest"));
+        assertSame(dataHandler, out.getOut().getAttachment("ldapAttachment"));
     }
 
     @SuppressWarnings("unchecked")
     private Collection<SearchResult> defaultLdapModuleOutAssertions(Exchange out) {
         // assertions of the response
-        Assert.assertNotNull(out);
-        Assert.assertNotNull(out.getOut());
+        assertNotNull(out);
+        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);
+        assertNotNull("out body could not be converted to a Collection - was: " + out.getOut().getBody(), data);
         return data;
     }