use of org.springframework.ldap.core.LdapOperations in project camel by apache.
the class SpringLdapProducer method process.
/**
* Performs the LDAP operation defined in SpringLdapEndpoint that created
* this producer. The in-message in the exchange must be a map, containing
* the following entries:
*
* <pre>
* key: "dn" - base DN for the LDAP operation
* key: "filter" - necessary for the search operation only; LDAP filter for the search operation,
* see <a http://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol>http://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol</a>
* key: "attributes" - necessary for the bind operation only; an instance of javax.naming.directory.Attributes,
* containing the information necessary to create an LDAP node.
* key: "password" - necessary for the authentication operation only;
* key: "modificationItems" - necessary for the modify_attributes operation only;
* key: "function" - necessary for the function_driven operation only; provides a flexible hook into the {@link LdapTemplate} to call any method
* key: "request" - necessary for the function_driven operation only; passed into the "function" to enable the client to bind parameters that need to be passed into the {@link LdapTemplate}
* </pre>
*
* The keys are defined as final fields above.
*/
@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");
}
String dn = (String) body.get(DN);
if (operation != LdapOperation.FUNCTION_DRIVEN && (StringUtils.isBlank(dn))) {
throw new UnsupportedOperationException("DN must not be empty, but you provided an empty DN");
}
LdapOperations ldapTemplate = endpoint.getLdapTemplate();
switch(operation) {
case SEARCH:
String filter = (String) body.get(FILTER);
exchange.getIn().setBody(ldapTemplate.search(dn, filter, endpoint.scopeValue(), mapper));
break;
case BIND:
Attributes attributes = (Attributes) body.get(ATTRIBUTES);
ldapTemplate.bind(dn, null, attributes);
break;
case UNBIND:
ldapTemplate.unbind(dn);
break;
case AUTHENTICATE:
ldapTemplate.authenticate(LdapQueryBuilder.query().base(dn).filter((String) body.get(FILTER)), (String) body.get(PASSWORD));
break;
case MODIFY_ATTRIBUTES:
ModificationItem[] modificationItems = (ModificationItem[]) body.get(MODIFICATION_ITEMS);
ldapTemplate.modifyAttributes(dn, modificationItems);
break;
case FUNCTION_DRIVEN:
BiFunction<LdapOperations, Object, ?> ldapOperationFunction = (BiFunction<LdapOperations, Object, ?>) body.get(FUNCTION);
Object ldapOperationRequest = body.get(REQUEST);
exchange.getIn().setBody(ldapOperationFunction.apply(ldapTemplate, ldapOperationRequest));
break;
default:
throw new UnsupportedOperationException("Bug in the Spring-LDAP component. Despite of all assertions, you managed to call an unsupported operation '" + operation + "'");
}
}
use of org.springframework.ldap.core.LdapOperations in project camel by apache.
the class SpringLdapProducerTest method testFunctionDriven.
@Test
public void testFunctionDriven() throws Exception {
String dn = "cn=dn";
Exchange exchange = new DefaultExchange(context);
Message in = new DefaultMessage();
Map<String, Object> body = new HashMap<String, Object>();
body.put(SpringLdapProducer.DN, dn);
body.put(SpringLdapProducer.REQUEST, dn);
body.put(SpringLdapProducer.FUNCTION, (BiFunction<LdapOperations, String, Void>) (l, q) -> {
l.lookup(q);
return null;
});
when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
processBody(exchange, in, body);
verify(ldapTemplate).lookup(eq(dn));
}
Aggregations