Search in sources :

Example 1 with ExpressionAdapter

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

the class ExpressionBuilder method tokenizeExpression.

/**
     * Returns a tokenize expression which will tokenize the string with the
     * given token
     */
public static Expression tokenizeExpression(final Expression expression, final String token) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = simpleExpression(token).evaluate(exchange, String.class);
            Object value = expression.evaluate(exchange, Object.class);
            Scanner scanner = ObjectHelper.getScanner(exchange, value);
            scanner.useDelimiter(text);
            return scanner;
        }

        @Override
        public String toString() {
            return "tokenize(" + expression + ", " + token + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Scanner(java.util.Scanner) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 2 with ExpressionAdapter

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

the class ExpressionBuilder method typeExpression.

/**
     * Returns an expression for a type value
     *
     * @param name the type name
     * @return an expression object which will return the type value
     */
public static Expression typeExpression(final String name) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            // it may refer to a class type
            String text = simpleExpression(name).evaluate(exchange, String.class);
            Class<?> type = exchange.getContext().getClassResolver().resolveClass(text);
            if (type != null) {
                return type;
            }
            int pos = text.lastIndexOf(".");
            if (pos > 0) {
                String before = text.substring(0, pos);
                String after = text.substring(pos + 1);
                type = exchange.getContext().getClassResolver().resolveClass(before);
                if (type != null) {
                    return ObjectHelper.lookupConstantFieldValue(type, after);
                }
            }
            throw ObjectHelper.wrapCamelExecutionException(exchange, new ClassNotFoundException("Cannot find type " + text));
        }

        @Override
        public String toString() {
            return "type:" + name;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 3 with ExpressionAdapter

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

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

the class ExpressionBuilder method skipFirstExpression.

/**
     * Returns an expression that skips the first element
     */
public static Expression skipFirstExpression(final Expression expression) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            Object value = expression.evaluate(exchange, Object.class);
            Iterator it = exchange.getContext().getTypeConverter().tryConvertTo(Iterator.class, exchange, value);
            if (it != null) {
                // skip first
                it.next();
                return it;
            } else {
                return value;
            }
        }

        @Override
        public String toString() {
            return "skipFirst(" + expression + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) XMLTokenExpressionIterator(org.apache.camel.support.XMLTokenExpressionIterator) GroupTokenIterator(org.apache.camel.util.GroupTokenIterator) TokenXMLExpressionIterator(org.apache.camel.support.TokenXMLExpressionIterator) TokenPairExpressionIterator(org.apache.camel.support.TokenPairExpressionIterator) Iterator(java.util.Iterator) SkipIterator(org.apache.camel.util.SkipIterator) GroupIterator(org.apache.camel.util.GroupIterator) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 5 with ExpressionAdapter

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

the class ExpressionBuilder method exchangeExceptionStackTraceExpression.

/**
     * Returns an expression for an exception stacktrace set on the exchange
     *
     * @return an expression object which will return the exception stacktrace set on the exchange
     */
public static Expression exchangeExceptionStackTraceExpression() {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            Exception exception = exchange.getException();
            if (exception == null) {
                exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
            }
            if (exception != null) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                exception.printStackTrace(pw);
                IOHelper.close(pw, sw);
                return sw.toString();
            } else {
                return null;
            }
        }

        @Override
        public String toString() {
            return "exchangeExceptionStackTrace";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) StringWriter(java.io.StringWriter) 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) PrintWriter(java.io.PrintWriter)

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