Search in sources :

Example 36 with Predicate

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

the class LogicalExpression method createAndExpression.

private Expression createAndExpression(final Expression leftExp, final Expression rightExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            Predicate predicate = ExpressionToPredicateAdapter.toPredicate(leftExp);
            predicate = PredicateBuilder.and(predicate, ExpressionToPredicateAdapter.toPredicate(rightExp));
            boolean answer = predicate.matches(exchange);
            return exchange.getContext().getTypeConverter().convertTo(type, answer);
        }

        @Override
        public String toString() {
            return left + " " + token.getText() + " " + right;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) Predicate(org.apache.camel.Predicate)

Example 37 with Predicate

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

the class WhenSkipSendToEndpointDefinition method createPredicate.

@Override
protected Predicate createPredicate(RouteContext routeContext) {
    // we need to keep track whether the when matches or not, so delegate
    // the predicate and add the matches result as a property on the exchange
    final Predicate delegate = super.createPredicate(routeContext);
    return new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            boolean matches = delegate.matches(exchange);
            exchange.setProperty(Exchange.INTERCEPT_SEND_TO_ENDPOINT_WHEN_MATCHED, matches);
            return matches;
        }

        @Override
        public String toString() {
            return delegate.toString();
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Predicate(org.apache.camel.Predicate) AsPredicate(org.apache.camel.spi.AsPredicate)

Example 38 with Predicate

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

the class ExpressionDefinition method getLabel.

/**
     * Returns some descriptive text to describe this node
     */
public String getLabel() {
    Predicate predicate = getPredicate();
    if (predicate != null) {
        return predicate.toString();
    }
    Expression expressionValue = getExpressionValue();
    if (expressionValue != null) {
        return expressionValue.toString();
    }
    String exp = getExpression();
    return exp != null ? exp : "";
}
Also used : Expression(org.apache.camel.Expression) Predicate(org.apache.camel.Predicate)

Example 39 with Predicate

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

the class XPathTest method assertPredicate.

protected void assertPredicate(String xpath, String xml, boolean expected) {
    Predicate predicate = XPathBuilder.xpath(xpath);
    assertPredicate(predicate, createExchange(xml), expected);
}
Also used : Predicate(org.apache.camel.Predicate)

Example 40 with Predicate

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

the class RouteScopedOnExceptionWithInterceptSendToEndpointIssueWithPredicateTest method testIssue.

public void testIssue() throws Exception {
    final Predicate fail = PredicateBuilder.or(header(Exchange.REDELIVERY_COUNTER).isNull(), header(Exchange.REDELIVERY_COUNTER).isLessThan(5));
    RouteDefinition route = context.getRouteDefinitions().get(0);
    route.adviceWith(context, new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("seda:*").skipSendToOriginalEndpoint().process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    invoked.incrementAndGet();
                    if (fail.matches(exchange)) {
                        throw new ConnectException("Forced");
                    }
                }
            }).to("mock:ok");
        }
    });
    getMockEndpoint("mock:global").expectedMessageCount(0);
    getMockEndpoint("mock:ok").expectedMessageCount(1);
    getMockEndpoint("mock:exhausted").expectedMessageCount(0);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    // 5 retry + 1 ok
    assertEquals(6, invoked.get());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteDefinition(org.apache.camel.model.RouteDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder) ConnectException(java.net.ConnectException) Predicate(org.apache.camel.Predicate) ConnectException(java.net.ConnectException)

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