Search in sources :

Example 6 with Expression

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

the class GenericFileDefaultSorter method sortByFileLanguage.

/**
     * Returns a new sort by file language expression
     *
     * @param context    the camel context
     * @param expression the file language expression
     * @param reverse    true to reverse order
     * @param ignoreCase ignore case if comparing strings
     * @param nested     nested comparator for sub group sorting, can be null
     * @return the comparator
     */
public static Comparator<Exchange> sortByFileLanguage(final CamelContext context, final String expression, final boolean reverse, final boolean ignoreCase, final Comparator<Exchange> nested) {
    // the expression should be enclosed by ${ }
    String text = expression;
    if (!expression.startsWith("${")) {
        text = "${" + text;
    }
    if (!expression.endsWith("}")) {
        text = text + "}";
    }
    Language language = context.resolveLanguage("file");
    final Expression exp = language.createExpression(text);
    return new Comparator<Exchange>() {

        public int compare(Exchange o1, Exchange o2) {
            Object result1 = exp.evaluate(o1, Object.class);
            Object result2 = exp.evaluate(o2, Object.class);
            int answer = ObjectHelper.compare(result1, result2, ignoreCase);
            // if equal then sub sort by nested comparator
            if (answer == 0 && nested != null) {
                answer = nested.compare(o1, o2);
            }
            return reverse ? -1 * answer : answer;
        }

        public String toString() {
            return expression + (nested != null ? ";" + nested.toString() : "");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Language(org.apache.camel.spi.Language) Expression(org.apache.camel.Expression) Comparator(java.util.Comparator)

Example 7 with Expression

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

the class MockEndpoint method expectedBodyReceived.

/**
     * Adds an expectation that the given body value are received by this endpoint
     */
public AssertionClause expectedBodyReceived() {
    expectedMessageCount(1);
    final AssertionClause clause = new AssertionClause(this) {

        public void run() {
            Exchange exchange = getReceivedExchange(0);
            assertTrue("No exchange received for counter: " + 0, exchange != null);
            Object actualBody = exchange.getIn().getBody();
            Expression exp = createExpression(getCamelContext());
            Object expectedBody = exp.evaluate(exchange, Object.class);
            assertEquals("Body of message: " + 0, expectedBody, actualBody);
        }
    };
    expects(clause);
    return clause;
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression)

Example 8 with Expression

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

the class RefLanguage method createExpression.

public Expression createExpression(final String expression) {
    final Expression exp = RefLanguage.ref(expression);
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            Expression target = null;
            Object lookup = exp.evaluate(exchange, Object.class);
            // must favor expression over predicate
            if (lookup != null && lookup instanceof Expression) {
                target = (Expression) lookup;
            } else if (lookup != null && lookup instanceof Predicate) {
                target = PredicateToExpressionAdapter.toExpression((Predicate) lookup);
            }
            if (target != null) {
                return target.evaluate(exchange, Object.class);
            } else {
                throw new IllegalArgumentException("Cannot find expression or predicate in registry with ref: " + expression);
            }
        }

        public String toString() {
            return exp.toString();
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter) PredicateToExpressionAdapter(org.apache.camel.util.PredicateToExpressionAdapter) Predicate(org.apache.camel.Predicate)

Example 9 with Expression

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

the class BinaryExpression method createInExpression.

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

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            // okay the in operator is a bit more complex as we need to build a list of values
            // from the right hand side expression.
            // each element on the right hand side must be separated by comma (default for create iterator)
            Iterator<Object> it = ObjectHelper.createIterator(rightExp.evaluate(exchange, Object.class));
            List<Object> values = new ArrayList<Object>();
            while (it.hasNext()) {
                values.add(it.next());
            }
            // then reuse value builder to create the in predicate with the list of values
            ValueBuilder vb = new ValueBuilder(leftExp);
            Predicate predicate = vb.in(values.toArray());
            if (operator == BinaryOperatorType.NOT_IN) {
                predicate = PredicateBuilder.not(predicate);
            }
            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) ValueBuilder(org.apache.camel.builder.ValueBuilder) Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate)

Example 10 with Expression

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

the class BinaryExpression method createRegexExpression.

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

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            // reg ex should use String pattern, so we evaluate the right hand side as a String
            Predicate predicate = PredicateBuilder.regex(leftExp, rightExp.evaluate(exchange, String.class));
            if (operator == BinaryOperatorType.NOT_REGEX) {
                predicate = PredicateBuilder.not(predicate);
            }
            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)

Aggregations

Expression (org.apache.camel.Expression)183 Exchange (org.apache.camel.Exchange)44 Processor (org.apache.camel.Processor)24 Predicate (org.apache.camel.Predicate)22 DefaultExchange (org.apache.camel.impl.DefaultExchange)21 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)18 ArrayList (java.util.ArrayList)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)15 Language (org.apache.camel.spi.Language)15 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 SendProcessor (org.apache.camel.processor.SendProcessor)14 Test (org.junit.Test)8 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)7 File (java.io.File)4 ExecutorService (java.util.concurrent.ExecutorService)4 SimpleIllegalSyntaxException (org.apache.camel.language.simple.types.SimpleIllegalSyntaxException)4 HashMap (java.util.HashMap)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 CamelExchangeException (org.apache.camel.CamelExchangeException)3