Search in sources :

Example 1 with MethodCallExpression

use of org.apache.camel.model.language.MethodCallExpression in project camel by apache.

the class ExpressionBuilder method bodyOgnlExpression.

/**
     * Returns the expression for the exchanges inbound message body converted
     * to the given type and invoking methods on the converted body defined in a simple OGNL notation
     */
public static Expression bodyOgnlExpression(final String name, final String ognl) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = simpleExpression(name).evaluate(exchange, String.class);
            Class<?> type;
            try {
                type = exchange.getContext().getClassResolver().resolveMandatoryClass(text);
            } catch (ClassNotFoundException e) {
                throw ObjectHelper.wrapCamelExecutionException(exchange, e);
            }
            Object body = exchange.getIn().getBody(type);
            if (body != null) {
                // ognl is able to evaluate method name if it contains nested functions
                // so we should not eager evaluate ognl as a string
                MethodCallExpression call = new MethodCallExpression(exchange, ognl);
                // set the instance to use
                call.setInstance(body);
                return call.evaluate(exchange);
            } else {
                return null;
            }
        }

        @Override
        public String toString() {
            return "bodyOgnlAs[" + name + "](" + ognl + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) MethodCallExpression(org.apache.camel.model.language.MethodCallExpression) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 2 with MethodCallExpression

use of org.apache.camel.model.language.MethodCallExpression in project camel by apache.

the class ExpressionBuilder method mandatoryBodyOgnlExpression.

/**
     * Returns the expression for the exchanges inbound message body converted
     * to the given type and invoking methods on the converted body defined in a simple OGNL notation
     */
public static Expression mandatoryBodyOgnlExpression(final String name, final String ognl) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = simpleExpression(name).evaluate(exchange, String.class);
            Class<?> type;
            try {
                type = exchange.getContext().getClassResolver().resolveMandatoryClass(text);
            } catch (ClassNotFoundException e) {
                throw ObjectHelper.wrapCamelExecutionException(exchange, e);
            }
            Object body;
            try {
                body = exchange.getIn().getMandatoryBody(type);
            } catch (InvalidPayloadException e) {
                throw ObjectHelper.wrapCamelExecutionException(exchange, e);
            }
            // ognl is able to evaluate method name if it contains nested functions
            // so we should not eager evaluate ognl as a string
            MethodCallExpression call = new MethodCallExpression(exchange, ognl);
            // set the instance to use
            call.setInstance(body);
            return call.evaluate(exchange);
        }

        @Override
        public String toString() {
            return "mandatoryBodyAs[" + name + "](" + ognl + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) MethodCallExpression(org.apache.camel.model.language.MethodCallExpression) InvalidPayloadException(org.apache.camel.InvalidPayloadException) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Aggregations

Exchange (org.apache.camel.Exchange)2 MethodCallExpression (org.apache.camel.model.language.MethodCallExpression)2 ExpressionAdapter (org.apache.camel.support.ExpressionAdapter)2 InvalidPayloadException (org.apache.camel.InvalidPayloadException)1