Search in sources :

Example 16 with ExpressionAdapter

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

the class ExpressionBuilder method outHeaderExpression.

/**
     * Returns an expression for the out header value with the given name
     * <p/>
     * Will fallback and look in properties if not found in headers.
     *
     * @param headerName the name of the header the expression will return
     * @return an expression object which will return the header value
     */
public static Expression outHeaderExpression(final String headerName) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            if (!exchange.hasOut()) {
                return null;
            }
            String text = simpleExpression(headerName).evaluate(exchange, String.class);
            Message out = exchange.getOut();
            Object header = out.getHeader(text);
            if (header == null) {
                // let's try the exchange header
                header = exchange.getProperty(text);
            }
            return header;
        }

        @Override
        public String toString() {
            return "outHeader(" + headerName + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 17 with ExpressionAdapter

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

Example 18 with ExpressionAdapter

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

the class ExpressionBuilder method regexTokenizeExpression.

/**
     * Returns a tokenize expression which will tokenize the string with the
     * given regex
     */
public static Expression regexTokenizeExpression(final Expression expression, final String regexTokenizer) {
    final Pattern pattern = Pattern.compile(regexTokenizer);
    return new ExpressionAdapter() {

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

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

Example 19 with ExpressionAdapter

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

the class ExpressionBuilder method dateExpression.

public static Expression dateExpression(final String commandWithOffsets, final String timezone, final String pattern) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            // Capture optional time offsets
            String command = commandWithOffsets.split("[+-]", 2)[0].trim();
            List<Long> offsets = new ArrayList<>();
            Matcher offsetMatcher = OFFSET_PATTERN.matcher(commandWithOffsets);
            while (offsetMatcher.find()) {
                try {
                    long value = exchange.getContext().getTypeConverter().mandatoryConvertTo(long.class, exchange, offsetMatcher.group(2).trim());
                    offsets.add(offsetMatcher.group(1).equals("+") ? value : -value);
                } catch (NoTypeConversionAvailableException e) {
                    throw ObjectHelper.wrapCamelExecutionException(exchange, e);
                }
            }
            Date date;
            if ("now".equals(command)) {
                date = new Date();
            } else if (command.startsWith("header.") || command.startsWith("in.header.")) {
                String key = command.substring(command.lastIndexOf('.') + 1);
                date = exchange.getIn().getHeader(key, Date.class);
                if (date == null) {
                    throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
                }
            } else if (command.startsWith("out.header.")) {
                String key = command.substring(command.lastIndexOf('.') + 1);
                date = exchange.getOut().getHeader(key, Date.class);
                if (date == null) {
                    throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
                }
            } else if ("file".equals(command)) {
                Long num = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Long.class);
                if (num != null && num > 0) {
                    date = new Date(num);
                } else {
                    date = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Date.class);
                    if (date == null) {
                        throw new IllegalArgumentException("Cannot find " + Exchange.FILE_LAST_MODIFIED + " header at command: " + command);
                    }
                }
            } else {
                throw new IllegalArgumentException("Command not supported for dateExpression: " + command);
            }
            // Apply offsets
            long dateAsLong = date.getTime();
            for (long offset : offsets) {
                dateAsLong += offset;
            }
            date = new Date(dateAsLong);
            if (pattern != null && !pattern.isEmpty()) {
                SimpleDateFormat df = new SimpleDateFormat(pattern);
                if (timezone != null && !timezone.isEmpty()) {
                    df.setTimeZone(TimeZone.getTimeZone(timezone));
                }
                return df.format(date);
            } else {
                return date;
            }
        }

        @Override
        public String toString() {
            return "date(" + commandWithOffsets + ":" + pattern + ":" + timezone + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Matcher(java.util.regex.Matcher) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 20 with ExpressionAdapter

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

the class ExpressionBuilder method regexReplaceAll.

/**
     * Transforms the expression into a String then performs the regex
     * replaceAll to transform the String and return the result
     */
public static Expression regexReplaceAll(final Expression expression, final String regex, final Expression replacementExpression) {
    final Pattern pattern = Pattern.compile(regex);
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = expression.evaluate(exchange, String.class);
            String replacement = replacementExpression.evaluate(exchange, String.class);
            if (text == null || replacement == null) {
                return null;
            }
            return pattern.matcher(text).replaceAll(replacement);
        }

        @Override
        public String toString() {
            return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Pattern(java.util.regex.Pattern) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

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