Search in sources :

Example 1 with LdapOperations

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 + "'");
    }
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) BiFunction(java.util.function.BiFunction) LdapOperations(org.springframework.ldap.core.LdapOperations) Attributes(javax.naming.directory.Attributes)

Example 2 with LdapOperations

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));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultMessage(org.apache.camel.impl.DefaultMessage) ModificationItem(javax.naming.directory.ModificationItem) Message(org.apache.camel.Message) Matchers(org.mockito.Matchers) BiFunction(java.util.function.BiFunction) HashMap(java.util.HashMap) Exchange(org.apache.camel.Exchange) LdapTemplate(org.springframework.ldap.core.LdapTemplate) SearchControls(javax.naming.directory.SearchControls) BasicAttribute(javax.naming.directory.BasicAttribute) LdapOperations(org.springframework.ldap.core.LdapOperations) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Before(org.junit.Before) LdapQuery(org.springframework.ldap.query.LdapQuery) DefaultExchange(org.apache.camel.impl.DefaultExchange) Matchers.isNull(org.mockito.Matchers.isNull) DirContext(javax.naming.directory.DirContext) BasicAttributes(javax.naming.directory.BasicAttributes) DefaultMessage(org.apache.camel.impl.DefaultMessage) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) AttributesMapper(org.springframework.ldap.core.AttributesMapper) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) HashMap(java.util.HashMap) LdapOperations(org.springframework.ldap.core.LdapOperations) Test(org.junit.Test)

Aggregations

BiFunction (java.util.function.BiFunction)2 ModificationItem (javax.naming.directory.ModificationItem)2 LdapOperations (org.springframework.ldap.core.LdapOperations)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Attributes (javax.naming.directory.Attributes)1 BasicAttribute (javax.naming.directory.BasicAttribute)1 BasicAttributes (javax.naming.directory.BasicAttributes)1 DirContext (javax.naming.directory.DirContext)1 SearchControls (javax.naming.directory.SearchControls)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 DefaultExchange (org.apache.camel.impl.DefaultExchange)1 DefaultMessage (org.apache.camel.impl.DefaultMessage)1 CamelTestSupport (org.apache.camel.test.junit4.CamelTestSupport)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Matchers (org.mockito.Matchers)1 Matchers.any (org.mockito.Matchers.any)1 Matchers.eq (org.mockito.Matchers.eq)1