Search in sources :

Example 6 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class ExpressionBuilder method randomExpression.

/**
     * Returns a random number between min and max
     */
public static Expression randomExpression(final int min, final int max) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            Random random = new Random();
            int randomNum = random.nextInt(max - min) + min;
            return randomNum;
        }

        @Override
        public String toString() {
            return "random";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Random(java.util.Random) Endpoint(org.apache.camel.Endpoint) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 7 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class ExpressionBuilder method randomExpression.

/**
     * Returns a random number between min and max
     */
public static Expression randomExpression(final String min, final String max) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            int num1 = simpleExpression(min).evaluate(exchange, Integer.class);
            int num2 = simpleExpression(max).evaluate(exchange, Integer.class);
            Random random = new Random();
            int randomNum = random.nextInt(num2 - num1) + num1;
            return randomNum;
        }

        @Override
        public String toString() {
            return "random";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Random(java.util.Random) Endpoint(org.apache.camel.Endpoint) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 8 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class ExpressionBuilder method routeIdExpression.

/**
     * Returns an Expression for the route id
     */
public static Expression routeIdExpression() {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String answer = null;
            UnitOfWork uow = exchange.getUnitOfWork();
            RouteContext rc = uow != null ? uow.getRouteContext() : null;
            if (rc != null) {
                answer = rc.getRoute().getId();
            }
            if (answer == null) {
                // fallback and get from route id on the exchange
                answer = exchange.getFromRouteId();
            }
            return answer;
        }

        @Override
        public String toString() {
            return "routeId";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) UnitOfWork(org.apache.camel.spi.UnitOfWork) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter) RouteContext(org.apache.camel.spi.RouteContext)

Example 9 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class ExpressionBuilder method propertiesComponentExpression.

public static Expression propertiesComponentExpression(final String key, final String locations, final String defaultValue) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = simpleExpression(key).evaluate(exchange, String.class);
            String text2 = simpleExpression(locations).evaluate(exchange, String.class);
            try {
                if (text2 != null) {
                    // the properties component is optional as we got locations
                    // getComponent will create a new component if none already exists
                    Component component = exchange.getContext().getComponent("properties");
                    PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
                    // enclose key with {{ }} to force parsing
                    String[] paths = text2.split(",");
                    return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken(), paths);
                } else {
                    // the properties component is mandatory if no locations provided
                    Component component = exchange.getContext().hasComponent("properties");
                    if (component == null) {
                        throw new IllegalArgumentException("PropertiesComponent with name properties must be defined" + " in CamelContext to support property placeholders in expressions");
                    }
                    PropertiesComponent pc = exchange.getContext().getTypeConverter().mandatoryConvertTo(PropertiesComponent.class, component);
                    // enclose key with {{ }} to force parsing
                    return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken());
                }
            } catch (Exception e) {
                // property with key not found, use default value if provided
                if (defaultValue != null) {
                    return defaultValue;
                }
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
        }

        @Override
        public String toString() {
            return "properties(" + key + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) Component(org.apache.camel.Component) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 10 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter 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)

Aggregations

Exchange (org.apache.camel.Exchange)21 ExpressionAdapter (org.apache.camel.support.ExpressionAdapter)21 InvalidPayloadException (org.apache.camel.InvalidPayloadException)5 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)5 Endpoint (org.apache.camel.Endpoint)4 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)4 NoSuchLanguageException (org.apache.camel.NoSuchLanguageException)4 Random (java.util.Random)2 Scanner (java.util.Scanner)2 Pattern (java.util.regex.Pattern)2 Expression (org.apache.camel.Expression)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 MethodCallExpression (org.apache.camel.model.language.MethodCallExpression)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Matcher (java.util.regex.Matcher)1