use of java.util.function.BiFunction 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 java.util.function.BiFunction 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));
}
use of java.util.function.BiFunction in project torodb by torodb.
the class DefaultOplogApplier method createBatcherFlow.
/**
* Creates a flow that batches and analyze a input of {@link AnalyzedOplogBatch remote jobs}.
*
* This flow tries to accummulate several remote jobs into a bigger one and does not emit until:
* <ul>
* <li>A maximum number of operations are batched</li>
* <li>Or a maximum time has happen since the last emit</li>
* <li>Or the recived job is not {@link AnalyzedOplogBatch#isReadyForMore()}</li>
* </ul>
*
*/
private Flow<OplogBatch, AnalyzedStreamElement, NotUsed> createBatcherFlow(ApplierContext context) {
Predicate<OplogBatch> finishBatchPredicate = (OplogBatch rawBatch) -> !rawBatch.isReadyForMore();
ToIntFunction<OplogBatch> costFunction = (rawBatch) -> rawBatch.count();
Supplier<RawStreamElement> zeroFun = () -> RawStreamElement.INITIAL_ELEMENT;
BiFunction<RawStreamElement, OplogBatch, RawStreamElement> acumFun = (streamElem, newBatch) -> streamElem.concat(newBatch);
BatchAnalyzer batchAnalyzer = batchAnalyzerFactory.createBatchAnalyzer(context);
return Flow.of(OplogBatch.class).via(new BatchFlow<>(batchLimits.maxSize, batchLimits.maxPeriod, finishBatchPredicate, costFunction, zeroFun, acumFun)).filter(rawElem -> rawElem.rawBatch != null && !rawElem.rawBatch.isEmpty()).map(rawElem -> {
List<OplogOperation> rawOps = rawElem.rawBatch.getOps();
List<AnalyzedOplogBatch> analyzed = batchAnalyzer.apply(rawOps);
return new AnalyzedStreamElement(rawElem, analyzed);
});
}
use of java.util.function.BiFunction in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method biFunctionWithTracingAndMdc_separate_args_works_as_expected.
@Test
public void biFunctionWithTracingAndMdc_separate_args_works_as_expected() {
// given
Pair<Deque<Span>, Map<String, String>> setupInfo = generateTracingAndMdcInfo();
// when
BiFunction result = AsyncNettyHelper.biFunctionWithTracingAndMdc(biFunctionMock, setupInfo.getLeft(), setupInfo.getRight());
// then
verifyBiFunctionWithTracingAndMdcSupport(result, biFunctionMock, setupInfo.getLeft(), setupInfo.getRight());
}
use of java.util.function.BiFunction in project riposte by Nike-Inc.
the class AsyncNettyHelperTest method biFunctionWithTracingAndMdc_ctx_works_as_expected.
@Test
public void biFunctionWithTracingAndMdc_ctx_works_as_expected() {
// given
Pair<Deque<Span>, Map<String, String>> setupInfo = setupStateWithTracingAndMdcInfo();
// when
BiFunction result = AsyncNettyHelper.biFunctionWithTracingAndMdc(biFunctionMock, ctxMock);
// then
verifyBiFunctionWithTracingAndMdcSupport(result, biFunctionMock, setupInfo.getLeft(), setupInfo.getRight());
}
Aggregations