This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-26199-spring-ldap-map-body in repository https://gitbox.apache.org/repos/asf/camel.git
commit 0e607acfb21f844efc4c9a96693693bf77582b73 Author: Claus Ibsen <[email protected]> AuthorDate: Tue Nov 3 00:08:31 2026 +0100 CAMEL-26199: camel-spring-ldap - a body that is not a Map, or no body, fails with an InvalidPayloadException naming the Map The producer read the body with the non-mandatory getBody(Map.class), so a String body or no body failed with a bare NullPointerException on body.get("dn"). It now uses getMandatoryBody(Map.class), read after the operation check, so the error names the expected Map. A function_driven body without a 'function' entry also failed with a NullPointerException; it now fails with a message naming the missing entry. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/catalog/docs/spring-ldap-component.adoc | 3 +- .../src/main/docs/spring-ldap-component.adoc | 3 +- .../component/springldap/SpringLdapProducer.java | 10 ++++-- .../springldap/SpringLdapProducerTest.java | 41 ++++++++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ldap-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ldap-component.adoc index f312ebf4d1c9..826bea007443 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ldap-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/spring-ldap-component.adoc @@ -53,7 +53,8 @@ include::partial$component-endpoint-headers.adoc[] The component supports producer endpoint only. An attempt to create a consumer endpoint will result in an `UnsupportedOperationException`. + - The body of the message must be a map (an instance of `java.util.Map`). + The body of the message must be a map (an instance of `java.util.Map`); another body, or no body, +fails with an `InvalidPayloadException`. Unless a base DN is specified by in the configuration of your ContextSource, this map must contain at least an entry with the key *`dn`* (not needed for function_driven operation) that specifies the root node for the LDAP operation to be performed. Other diff --git a/components/camel-spring-parent/camel-spring-ldap/src/main/docs/spring-ldap-component.adoc b/components/camel-spring-parent/camel-spring-ldap/src/main/docs/spring-ldap-component.adoc index f312ebf4d1c9..826bea007443 100644 --- a/components/camel-spring-parent/camel-spring-ldap/src/main/docs/spring-ldap-component.adoc +++ b/components/camel-spring-parent/camel-spring-ldap/src/main/docs/spring-ldap-component.adoc @@ -53,7 +53,8 @@ include::partial$component-endpoint-headers.adoc[] The component supports producer endpoint only. An attempt to create a consumer endpoint will result in an `UnsupportedOperationException`. + - The body of the message must be a map (an instance of `java.util.Map`). + The body of the message must be a map (an instance of `java.util.Map`); another body, or no body, +fails with an `InvalidPayloadException`. Unless a base DN is specified by in the configuration of your ContextSource, this map must contain at least an entry with the key *`dn`* (not needed for function_driven operation) that specifies the root node for the LDAP operation to be performed. Other diff --git a/components/camel-spring-parent/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java b/components/camel-spring-parent/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java index bf11a2f4b67b..650aad2df94a 100644 --- a/components/camel-spring-parent/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java +++ b/components/camel-spring-parent/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java @@ -82,14 +82,14 @@ public class SpringLdapProducer extends DefaultProducer { */ @Override public void process(Exchange exchange) throws Exception { - @SuppressWarnings("unchecked") - Map<String, Object> body = exchange.getIn().getBody(Map.class); - LdapOperation operation = endpoint.getOperation(); if (null == operation) { throw new UnsupportedOperationException("LDAP operation must not be empty, but you provided an empty operation"); } + @SuppressWarnings("unchecked") + Map<String, Object> body = exchange.getIn().getMandatoryBody(Map.class); + LdapTemplate ldapTemplate = endpoint.getLdapTemplate(); String dn = (String) body.get(DN); @@ -128,6 +128,10 @@ public class SpringLdapProducer extends DefaultProducer { case FUNCTION_DRIVEN: BiFunction<LdapOperations, Object, ?> ldapOperationFunction = (BiFunction<LdapOperations, Object, ?>) body.get(FUNCTION); + if (ldapOperationFunction == null) { + throw new UnsupportedOperationException( + "The function_driven operation needs a BiFunction in the '" + FUNCTION + "' entry of the body"); + } Object ldapOperationRequest = body.get(REQUEST); exchange.getIn().setBody(ldapOperationFunction.apply(ldapTemplate, ldapOperationRequest)); break; diff --git a/components/camel-spring-parent/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java b/components/camel-spring-parent/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java index dbb1bdfd22e8..6a7af2e28344 100644 --- a/components/camel-spring-parent/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java +++ b/components/camel-spring-parent/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java @@ -27,6 +27,7 @@ import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; import org.apache.camel.Exchange; +import org.apache.camel.InvalidPayloadException; import org.apache.camel.Message; import org.apache.camel.support.DefaultExchange; import org.apache.camel.support.DefaultMessage; @@ -41,7 +42,9 @@ import org.springframework.ldap.core.LdapOperations; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.query.LdapQuery; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.mock; @@ -83,6 +86,44 @@ class SpringLdapProducerTest extends CamelTestSupport { () -> ldapProducer.process(exchange)); } + @Test + void testStringBodyNamesTheExpectedMap() { + Exchange exchange = new DefaultExchange(context); + exchange.getIn().setBody("cn=Alice,ou=people,dc=example,dc=org"); + + when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.SEARCH); + + InvalidPayloadException e = assertThrows(InvalidPayloadException.class, + () -> ldapProducer.process(exchange)); + assertEquals(Map.class, e.getType()); + } + + @Test + void testNoBodyNamesTheExpectedMap() { + Exchange exchange = new DefaultExchange(context); + + when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.UNBIND); + + InvalidPayloadException e = assertThrows(InvalidPayloadException.class, + () -> ldapProducer.process(exchange)); + assertEquals(Map.class, e.getType()); + } + + @Test + void testFunctionDrivenWithoutFunction() { + Exchange exchange = new DefaultExchange(context); + Message in = new DefaultMessage(context); + + Map<String, Object> body = new HashMap<>(); + body.put(SpringLdapProducer.REQUEST, "cn=dn"); + + when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN); + + UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, + () -> processBody(exchange, in, body)); + assertTrue(e.getMessage().contains("'function'"), e.getMessage()); + } + @Test public void testNoDN() throws Exception { Exchange exchange = new DefaultExchange(context);
