Search in sources :

Example 96 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class OnExceptionDefinition method createProcessor.

@Override
public CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
    // load exception classes
    if (exceptions != null && !exceptions.isEmpty()) {
        exceptionClasses = createExceptionClasses(routeContext.getCamelContext().getClassResolver());
    }
    if (useOriginalMessagePolicy != null && useOriginalMessagePolicy) {
        // ensure allow original is turned on
        routeContext.setAllowUseOriginalMessage(true);
    }
    // must validate configuration before creating processor
    validateConfiguration();
    Processor childProcessor = this.createChildProcessor(routeContext, false);
    Predicate when = null;
    if (onWhen != null) {
        when = onWhen.getExpression().createPredicate(routeContext);
    }
    Predicate handle = null;
    if (handled != null) {
        handle = handled.createPredicate(routeContext);
    }
    return new CatchProcessor(getExceptionClasses(), childProcessor, when, handle);
}
Also used : Processor(org.apache.camel.Processor) CatchProcessor(org.apache.camel.processor.CatchProcessor) AsPredicate(org.apache.camel.spi.AsPredicate) Predicate(org.apache.camel.Predicate) CatchProcessor(org.apache.camel.processor.CatchProcessor)

Example 97 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class ExpressionNodeHelper method toExpressionDefinition.

/**
     * Determines which {@link ExpressionDefinition} describes the given predicate best possible.
     * <p/>
     * This implementation will use types such as {@link SimpleExpression}, {@link XPathExpression} etc.
     * if the given predicate is detect as such a type.
     *
     * @param predicate the predicate
     * @return a definition which describes the predicate
     */
public static ExpressionDefinition toExpressionDefinition(Predicate predicate) {
    if (predicate instanceof SimpleBuilder) {
        SimpleBuilder builder = (SimpleBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        SimpleExpression answer = new SimpleExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof XPathBuilder) {
        XPathBuilder builder = (XPathBuilder) predicate;
        // we keep the original expression by using the constructor that accepts an expression
        XPathExpression answer = new XPathExpression(builder);
        answer.setExpression(builder.getText());
        return answer;
    } else if (predicate instanceof ValueBuilder) {
        // ValueBuilder wraps the actual predicate so unwrap
        ValueBuilder builder = (ValueBuilder) predicate;
        Expression expression = builder.getExpression();
        if (expression instanceof Predicate) {
            predicate = (Predicate) expression;
        }
    }
    if (predicate instanceof ExpressionDefinition) {
        return (ExpressionDefinition) predicate;
    }
    return new ExpressionDefinition(predicate);
}
Also used : XPathExpression(org.apache.camel.model.language.XPathExpression) ValueBuilder(org.apache.camel.builder.ValueBuilder) SimpleBuilder(org.apache.camel.builder.SimpleBuilder) XPathExpression(org.apache.camel.model.language.XPathExpression) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Expression(org.apache.camel.Expression) XPathBuilder(org.apache.camel.builder.xml.XPathBuilder) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition) SimpleExpression(org.apache.camel.model.language.SimpleExpression) Predicate(org.apache.camel.Predicate)

Example 98 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class LoopDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    Processor output = this.createChildProcessor(routeContext, true);
    boolean isCopy = getCopy() != null && getCopy();
    boolean isWhile = getDoWhile() != null && getDoWhile();
    Predicate predicate = null;
    Expression expression = null;
    if (isWhile) {
        predicate = getExpression().createPredicate(routeContext);
    } else {
        expression = getExpression().createExpression(routeContext);
    }
    return new LoopProcessor(output, expression, predicate, isCopy);
}
Also used : Processor(org.apache.camel.Processor) LoopProcessor(org.apache.camel.processor.LoopProcessor) Expression(org.apache.camel.Expression) LoopProcessor(org.apache.camel.processor.LoopProcessor) Predicate(org.apache.camel.Predicate) AsPredicate(org.apache.camel.spi.AsPredicate)

Example 99 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class OnCompletionDefinition method createProcessor.

@Override
public Processor createProcessor(RouteContext routeContext) throws Exception {
    // and therefore is in a better position to decide among context/route scoped OnCompletion at runtime
    if (routeScoped == null) {
        routeScoped = super.getParent() != null;
    }
    boolean isOnCompleteOnly = getOnCompleteOnly() != null && getOnCompleteOnly();
    boolean isOnFailureOnly = getOnFailureOnly() != null && getOnFailureOnly();
    boolean isParallelProcessing = getParallelProcessing() != null && getParallelProcessing();
    boolean original = getUseOriginalMessagePolicy() != null && getUseOriginalMessagePolicy();
    if (isOnCompleteOnly && isOnFailureOnly) {
        throw new IllegalArgumentException("Both onCompleteOnly and onFailureOnly cannot be true. Only one of them can be true. On node: " + this);
    }
    if (original) {
        // ensure allow original is turned on
        routeContext.setAllowUseOriginalMessage(true);
    }
    String routeId = routeContext.getRoute().idOrCreate(routeContext.getCamelContext().getNodeIdFactory());
    Processor childProcessor = this.createChildProcessor(routeContext, true);
    // wrap the on completion route in a unit of work processor
    CamelInternalProcessor internal = new CamelInternalProcessor(childProcessor);
    internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
    onCompletions.put(routeId, internal);
    Predicate when = null;
    if (onWhen != null) {
        when = onWhen.getExpression().createPredicate(routeContext);
    }
    boolean shutdownThreadPool = ProcessorDefinitionHelper.willCreateNewThreadPool(routeContext, this, isParallelProcessing);
    ExecutorService threadPool = ProcessorDefinitionHelper.getConfiguredExecutorService(routeContext, "OnCompletion", this, isParallelProcessing);
    // should be after consumer by default
    boolean afterConsumer = mode == null || mode == OnCompletionMode.AfterConsumer;
    OnCompletionProcessor answer = new OnCompletionProcessor(routeContext.getCamelContext(), internal, threadPool, shutdownThreadPool, isOnCompleteOnly, isOnFailureOnly, when, original, afterConsumer);
    return answer;
}
Also used : CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) CamelInternalProcessor(org.apache.camel.processor.CamelInternalProcessor) Processor(org.apache.camel.Processor) OnCompletionProcessor(org.apache.camel.processor.OnCompletionProcessor) OnCompletionProcessor(org.apache.camel.processor.OnCompletionProcessor) ExecutorService(java.util.concurrent.ExecutorService) AsPredicate(org.apache.camel.spi.AsPredicate) Predicate(org.apache.camel.Predicate)

Example 100 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class PredicateBuilderTest method testCompoundAndPredicatesVarargs.

public void testCompoundAndPredicatesVarargs() throws Exception {
    Predicate p1 = header("name").isEqualTo(constant("James"));
    Predicate p2 = header("size").isGreaterThanOrEqualTo(constant(10));
    Predicate p3 = header("location").contains(constant("London"));
    Predicate and = PredicateBuilder.and(p1, p2, p3);
    assertMatches(and);
}
Also used : Predicate(org.apache.camel.Predicate)

Aggregations

Predicate (org.apache.camel.Predicate)124 Exchange (org.apache.camel.Exchange)70 Test (org.junit.Test)40 HashMap (java.util.HashMap)25 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)23 Expression (org.apache.camel.Expression)22 Processor (org.apache.camel.Processor)19 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 RouteBuilder (org.apache.camel.builder.RouteBuilder)9 File (java.io.File)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Map (java.util.Map)7 Ignore (org.junit.Ignore)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)6 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)6 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)6 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)5