001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.builder;
018    
019    import java.text.SimpleDateFormat;
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.Comparator;
023    import java.util.Date;
024    import java.util.Iterator;
025    import java.util.List;
026    import java.util.Scanner;
027    import java.util.regex.Pattern;
028    
029    import org.apache.camel.Endpoint;
030    import org.apache.camel.Exchange;
031    import org.apache.camel.Expression;
032    import org.apache.camel.Message;
033    import org.apache.camel.NoSuchEndpointException;
034    import org.apache.camel.Producer;
035    import org.apache.camel.impl.ExpressionAdapter;
036    import org.apache.camel.language.bean.BeanLanguage;
037    import org.apache.camel.spi.Language;
038    import org.apache.camel.util.ExchangeHelper;
039    import org.apache.camel.util.ObjectHelper;
040    
041    /**
042     * A helper class for working with <a href="http://camel.apache.org/expression.html">expressions</a>.
043     *
044     * @version $Revision: 788778 $
045     */
046    public final class ExpressionBuilder {
047    
048        /**
049         * Utility classes should not have a public constructor.
050         */
051        private ExpressionBuilder() {
052        }
053    
054        /**
055         * Returns an expression for the header value with the given name
056         *
057         * @param headerName the name of the header the expression will return
058         * @return an expression object which will return the header value
059         */
060        public static Expression headerExpression(final String headerName) {
061            return new ExpressionAdapter() {
062                public Object evaluate(Exchange exchange) {
063                    Object header = exchange.getIn().getHeader(headerName);
064                    if (header == null) {
065                        // fall back on a property
066                        header = exchange.getProperty(headerName);
067                    }
068                    return header;
069                }
070    
071                @Override
072                public String toString() {
073                    return "header(" + headerName + ")";
074                }
075            };
076        }
077    
078        /**
079         * Returns an expression for the inbound message headers
080         *
081         * @return an expression object which will return the inbound headers
082         */
083        public static Expression headersExpression() {
084            return new ExpressionAdapter() {
085                public Object evaluate(Exchange exchange) {
086                    return exchange.getIn().getHeaders();
087                }
088    
089                @Override
090                public String toString() {
091                    return "headers";
092                }
093            };
094        }
095    
096        /**
097         * Returns an expression for the out header value with the given name
098         *
099         * @param headerName the name of the header the expression will return
100         * @return an expression object which will return the header value
101         */
102        public static Expression outHeaderExpression(final String headerName) {
103            return new ExpressionAdapter() {
104                public Object evaluate(Exchange exchange) {
105                    if (!exchange.hasOut()) {
106                        return null;
107                    }
108    
109                    Message out = exchange.getOut();
110                    Object header = out.getHeader(headerName);
111                    if (header == null) {
112                        // lets try the exchange header
113                        header = exchange.getProperty(headerName);
114                    }
115                    return header;
116                }
117    
118                @Override
119                public String toString() {
120                    return "outHeader(" + headerName + ")";
121                }
122            };
123        }
124    
125        /**
126         * Returns an expression for the outbound message headers
127         *
128         * @return an expression object which will return the headers
129         */
130        public static Expression outHeadersExpression() {
131            return new ExpressionAdapter() {
132                public Object evaluate(Exchange exchange) {
133                    return exchange.getOut().getHeaders();
134                }
135    
136                @Override
137                public String toString() {
138                    return "outHeaders";
139                }
140            };
141        }
142    
143        /**
144         * Returns an expression for an exception set on the exchange
145         *
146         * @see Exchange#getException()
147         * @return an expression object which will return the exception set on the exchange
148         */
149        public static Expression exchangeExceptionExpression() {
150            return new ExpressionAdapter() {
151                public Object evaluate(Exchange exchange) {
152                    Exception exception = exchange.getException();
153                    if (exception == null) {
154                        exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
155                    }
156                    return exception;
157                }
158    
159                @Override
160                public String toString() {
161                    return "exchangeException";
162                }
163            };
164        }   
165        
166        /**
167         * Returns an expression for an exception set on the exchange
168         * <p/>
169         * Is used to get the caused exception that typically have been wrapped in some sort
170         * of Camel wrapper exception
171         * @param type the exception type
172         * @see Exchange#getException(Class)
173         * @return an expression object which will return the exception set on the exchange
174         */
175        public static Expression exchangeExceptionExpression(final Class<Exception> type) {
176            return new ExpressionAdapter() {
177                public Object evaluate(Exchange exchange) {
178                    Exception exception = exchange.getException(type);
179                    if (exception == null) {
180                        exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
181                        // must use exception iterator to walk it and find the type we are looking for
182                        Iterator<Throwable> it = ObjectHelper.createExceptionIterator(exception);
183                        while (it.hasNext()) {
184                            Throwable e = it.next();
185                            if (type.isInstance(e)) {
186                                return type.cast(e);
187                            }
188                        }
189                        // not found
190                        return null;
191    
192                    }
193                    return exception;
194                }
195    
196                @Override
197                public String toString() {
198                    return "exchangeException[" + type + "]";
199                }
200            };
201        }
202    
203        /**
204         * Returns an expression for the type converter
205         *
206         * @return an expression object which will return the type converter
207         */
208        public static Expression typeConverterExpression() {
209            return new ExpressionAdapter() {
210                public Object evaluate(Exchange exchange) {
211                    return exchange.getContext().getTypeConverter();
212                }
213    
214                @Override
215                public String toString() {
216                    return "typeConverter";
217                }
218            };
219        }
220    
221        /**
222         * Returns an expression for the {@link org.apache.camel.spi.Registry}
223         *
224         * @return an expression object which will return the registry
225         */
226        public static Expression registryExpression() {
227            return new ExpressionAdapter() {
228                public Object evaluate(Exchange exchange) {
229                    return exchange.getContext().getRegistry();
230                }
231    
232                @Override
233                public String toString() {
234                    return "registry";
235                }
236            };
237        }
238    
239        /**
240         * Returns an expression for the {@link org.apache.camel.CamelContext}
241         *
242         * @return an expression object which will return the camel context
243         */
244        public static Expression camelContextExpression() {
245            return new ExpressionAdapter() {
246                public Object evaluate(Exchange exchange) {
247                    return exchange.getContext();
248                }
249    
250                @Override
251                public String toString() {
252                    return "camelContext";
253                }
254            };
255        }
256    
257        /**
258         * Returns an expression for an exception message set on the exchange
259         *
260         * @see <tt>Exchange.getException().getMessage()</tt>
261         * @return an expression object which will return the exception message set on the exchange
262         */
263        public static Expression exchangeExceptionMessageExpression() {
264            return new ExpressionAdapter() {
265                public Object evaluate(Exchange exchange) {
266                    Exception exception = exchange.getException();
267                    if (exception == null) {
268                        exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
269                    }
270                    return exception != null ? exception.getMessage() : null;
271                }
272    
273                @Override
274                public String toString() {
275                    return "exchangeExceptionMessage";
276                }
277            };
278        }
279    
280        /**
281         * Returns an expression for the property value of exchange with the given name
282         *
283         * @param propertyName the name of the property the expression will return
284         * @return an expression object which will return the property value
285         */
286        public static Expression propertyExpression(final String propertyName) {
287            return new ExpressionAdapter() {
288                public Object evaluate(Exchange exchange) {
289                    return exchange.getProperty(propertyName);
290                }
291    
292                @Override
293                public String toString() {
294                    return "property(" + propertyName + ")";
295                }
296            };
297        }
298    
299        /**
300         * Returns an expression for the properties of exchange
301         *
302         * @return an expression object which will return the properties
303         */
304        public static Expression propertiesExpression() {
305            return new ExpressionAdapter() {
306                public Object evaluate(Exchange exchange) {
307                    return exchange.getProperties();
308                }
309    
310                @Override
311                public String toString() {
312                    return "properties";
313                }
314            };
315        }
316        
317        /**
318         * Returns an expression for the properties of the camel context
319         *
320         * @return an expression object which will return the properties
321         */
322        public static Expression camelContextPropertiesExpression() {
323            return new ExpressionAdapter() {
324                public Object evaluate(Exchange exchange) {
325                    return exchange.getContext().getProperties();
326                }
327    
328                @Override
329                public String toString() {
330                    return "camelContextProperties";
331                }
332            };
333        }
334        
335        /**
336         * Returns an expression for the property value of the camel context with the given name
337         *
338         * @param propertyName the name of the property the expression will return
339         * @return an expression object which will return the property value
340         */
341        public static Expression camelContextPropertyExpression(final String propertyName) {
342            return new ExpressionAdapter() {
343                public Object evaluate(Exchange exchange) {
344                    return exchange.getContext().getProperties().get(propertyName);
345                }
346    
347                @Override
348                public String toString() {
349                    return "camelContextProperty(" + propertyName + ")";
350                }
351            };
352        }
353    
354        /**
355         * Returns an expression for a system property value with the given name
356         *
357         * @param propertyName the name of the system property the expression will return
358         * @return an expression object which will return the system property value
359         */
360        public static Expression systemPropertyExpression(final String propertyName) {
361            return systemPropertyExpression(propertyName, null);
362        }
363    
364        /**
365         * Returns an expression for a system property value with the given name
366         *
367         * @param propertyName the name of the system property the expression will return
368         * @param defaultValue default value to return if no system property exists
369         * @return an expression object which will return the system property value
370         */
371        public static Expression systemPropertyExpression(final String propertyName,
372                                                          final String defaultValue) {
373            return new ExpressionAdapter() {
374                public Object evaluate(Exchange exchange) {
375                    return System.getProperty(propertyName, defaultValue);
376                }
377    
378                @Override
379                public String toString() {
380                    return "systemProperty(" + propertyName + ")";
381                }
382            };
383        }
384    
385        /**
386         * Returns an expression for the constant value
387         *
388         * @param value the value the expression will return
389         * @return an expression object which will return the constant value
390         */
391        public static Expression constantExpression(final Object value) {
392            return new ExpressionAdapter() {
393                public Object evaluate(Exchange exchange) {
394                    return value;
395                }
396    
397                @Override
398                public String toString() {
399                    return "" + value;
400                }
401            };
402        }
403    
404        /**
405         * Returns the expression for the exchanges inbound message body
406         */
407        public static Expression bodyExpression() {
408            return new ExpressionAdapter() {
409                public Object evaluate(Exchange exchange) {
410                    return exchange.getIn().getBody();
411                }
412    
413                @Override
414                public String toString() {
415                    return "body";
416                }
417            };
418        }
419    
420        /**
421         * Returns the expression for the exchanges inbound message body converted
422         * to the given type
423         */
424        public static <T> Expression bodyExpression(final Class<T> type) {
425            return new ExpressionAdapter() {
426                public Object evaluate(Exchange exchange) {
427                    return exchange.getIn().getBody(type);
428                }
429    
430                @Override
431                public String toString() {
432                    return "bodyAs[" + type.getName() + "]";
433                }
434            };
435        }
436    
437        /**
438         * Returns the expression for the exchanges inbound message body type
439         */
440        public static Expression bodyTypeExpression() {
441            return new ExpressionAdapter() {
442                public Object evaluate(Exchange exchange) {
443                    return exchange.getIn().getBody().getClass();
444                }
445    
446                @Override
447                public String toString() {
448                    return "bodyType";
449                }
450            };
451        }
452    
453        /**
454         * Returns the expression for the out messages body
455         */
456        public static Expression outBodyExpression() {
457            return new ExpressionAdapter() {
458                public Object evaluate(Exchange exchange) {
459                    if (exchange.hasOut()) {
460                        return exchange.getOut().getBody();
461                    } else {
462                        return null;
463                    }
464                }
465    
466                @Override
467                public String toString() {
468                    return "outBody";
469                }
470            };
471        }
472    
473        /**
474         * Returns the expression for the exchanges outbound message body converted
475         * to the given type
476         */
477        public static <T> Expression outBodyExpression(final Class<T> type) {
478            return new ExpressionAdapter() {
479                public Object evaluate(Exchange exchange) {
480                    if (exchange.hasOut()) {
481                        return exchange.getOut().getBody(type);
482                    } else {
483                        return null;
484                    }
485                }
486    
487                @Override
488                public String toString() {
489                    return "outBodyAs[" + type.getName() + "]";
490                }
491            };
492        }
493    
494        /**
495         * Returns the expression for the fault messages body
496         */
497        public static Expression faultBodyExpression() {
498            return new ExpressionAdapter() {
499                public Object evaluate(Exchange exchange) {
500                    return exchange.getFault().getBody();
501                }
502    
503                @Override
504                public String toString() {
505                    return "faultBody";
506                }
507            };
508        }
509    
510        /**
511         * Returns the expression for the exchanges fault message body converted
512         * to the given type
513         */
514        public static <T> Expression faultBodyExpression(final Class<T> type) {
515            return new ExpressionAdapter() {
516                public Object evaluate(Exchange exchange) {
517                    return exchange.getFault().getBody(type);
518                }
519    
520                @Override
521                public String toString() {
522                    return "faultBodyAs[" + type.getName() + "]";
523                }
524            };
525        }
526    
527        /**
528         * Returns the expression for the exchange
529         */
530        public static Expression exchangeExpression() {
531            return new ExpressionAdapter() {
532                public Object evaluate(Exchange exchange) {
533                    return exchange;
534                }
535    
536                @Override
537                public String toString() {
538                    return "exchange";
539                }
540            };
541        }
542    
543        /**
544         * Returns the expression for the IN message
545         */
546        public static Expression inMessageExpression() {
547            return new ExpressionAdapter() {
548                public Object evaluate(Exchange exchange) {
549                    return exchange.getIn();
550                }
551    
552                @Override
553                public String toString() {
554                    return "inMessage";
555                }
556            };
557        }
558    
559        /**
560         * Returns the expression for the OUT message
561         */
562        public static Expression outMessageExpression() {
563            return new ExpressionAdapter() {
564                public Object evaluate(Exchange exchange) {
565                    return exchange.getOut();
566                }
567    
568                @Override
569                public String toString() {
570                    return "outMessage";
571                }
572            };
573        }
574    
575        /**
576         * Returns an expression which converts the given expression to the given type
577         */
578        @SuppressWarnings("unchecked")
579        public static Expression convertToExpression(final Expression expression, final Class type) {
580            return new ExpressionAdapter() {
581                public Object evaluate(Exchange exchange) {
582                    return expression.evaluate(exchange, type);
583                }
584    
585                @Override
586                public String toString() {
587                    return "" + expression + ".convertTo(" + type.getCanonicalName() + ".class)";
588                }
589            };
590        }
591    
592        /**
593         * Returns an expression which converts the given expression to the given type the type
594         * expression is evaluted to
595         */
596        public static Expression convertToExpression(final Expression expression, final Expression type) {
597            return new ExpressionAdapter() {
598                public Object evaluate(Exchange exchange) {
599                    return expression.evaluate(exchange, type.evaluate(exchange, Object.class).getClass());
600                }
601    
602                @Override
603                public String toString() {
604                    return "" + expression + ".convertToEvaluatedType(" + type + ")";
605                }
606            };
607        }
608    
609        /**
610         * Returns a tokenize expression which will tokenize the string with the
611         * given token
612         */
613        public static Expression tokenizeExpression(final Expression expression,
614                                                    final String token) {
615            return new ExpressionAdapter() {
616                public Object evaluate(Exchange exchange) {
617                    Object value = expression.evaluate(exchange, Object.class);
618                    Scanner scanner = ObjectHelper.getScanner(exchange, value);
619                    scanner.useDelimiter(token);
620                    return scanner;
621                }
622    
623                @Override
624                public String toString() {
625                    return "tokenize(" + expression + ", " + token + ")";
626                }
627            };
628        }
629    
630        /**
631         * Returns a tokenize expression which will tokenize the string with the
632         * given regex
633         */
634        public static Expression regexTokenizeExpression(final Expression expression,
635                                                         final String regexTokenizer) {
636            final Pattern pattern = Pattern.compile(regexTokenizer);
637            return new ExpressionAdapter() {
638                public Object evaluate(Exchange exchange) {
639                    Object value = expression.evaluate(exchange, Object.class);
640                    Scanner scanner = ObjectHelper.getScanner(exchange, value);
641                    scanner.useDelimiter(regexTokenizer);
642                    return scanner;
643                }
644    
645                @Override
646                public String toString() {
647                    return "regexTokenize(" + expression + ", " + pattern.pattern() + ")";
648                }
649            };
650        }
651    
652        /**
653         * Returns a sort expression which will sort the expression with the given comparator.
654         * <p/>
655         * The expression is evaluted as a {@link List} object to allow sorting.
656         */
657        @SuppressWarnings("unchecked")
658        public static Expression sortExpression(final Expression expression, final Comparator comparator) {
659            return new ExpressionAdapter() {
660                public Object evaluate(Exchange exchange) {
661                    List list = expression.evaluate(exchange, List.class);
662                    Collections.sort(list, comparator);
663                    return list;
664                }
665    
666                @Override
667                public String toString() {
668                    return "sort(" + expression + " by: " + comparator + ")";
669                }
670            };
671        }
672    
673        /**
674         * Transforms the expression into a String then performs the regex
675         * replaceAll to transform the String and return the result
676         */
677        public static Expression regexReplaceAll(final Expression expression,
678                                                 final String regex, final String replacement) {
679            final Pattern pattern = Pattern.compile(regex);
680            return new ExpressionAdapter() {
681                public Object evaluate(Exchange exchange) {
682                    String text = expression.evaluate(exchange, String.class);
683                    if (text == null) {
684                        return null;
685                    }
686                    return pattern.matcher(text).replaceAll(replacement);
687                }
688    
689                @Override
690                public String toString() {
691                    return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
692                }
693            };
694        }
695    
696        /**
697         * Transforms the expression into a String then performs the regex
698         * replaceAll to transform the String and return the result
699         */
700        public static Expression regexReplaceAll(final Expression expression,
701                                                 final String regex, final Expression replacementExpression) {
702    
703            final Pattern pattern = Pattern.compile(regex);
704            return new ExpressionAdapter() {
705                public Object evaluate(Exchange exchange) {
706                    String text = expression.evaluate(exchange, String.class);
707                    String replacement = replacementExpression.evaluate(exchange, String.class);
708                    if (text == null || replacement == null) {
709                        return null;
710                    }
711                    return pattern.matcher(text).replaceAll(replacement);
712                }
713    
714                @Override
715                public String toString() {
716                    return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
717                }
718            };
719        }
720    
721        /**
722         * Appends the String evaluations of the two expressions together
723         */
724        public static Expression append(final Expression left, final Expression right) {
725            return new ExpressionAdapter() {
726                public Object evaluate(Exchange exchange) {
727                    return left.evaluate(exchange, String.class) + right.evaluate(exchange, String.class);
728                }
729    
730                @Override
731                public String toString() {
732                    return "append(" + left + ", " + right + ")";
733                }
734            };
735        }
736    
737        /**
738         * Prepends the String evaluations of the two expressions together
739         */
740        public static Expression prepend(final Expression left, final Expression right) {
741            return new ExpressionAdapter() {
742                public Object evaluate(Exchange exchange) {
743                    return right.evaluate(exchange, String.class) + left.evaluate(exchange, String.class);
744                }
745    
746                @Override
747                public String toString() {
748                    return "prepend(" + left + ", " + right + ")";
749                }
750            };
751        }
752    
753        /**
754         * Returns an expression which returns the string concatenation value of the various
755         * expressions
756         *
757         * @param expressions the expression to be concatenated dynamically
758         * @return an expression which when evaluated will return the concatenated values
759         */
760        public static Expression concatExpression(final Collection<Expression> expressions) {
761            return concatExpression(expressions, null);
762        }
763    
764        /**
765         * Returns an expression which returns the string concatenation value of the various
766         * expressions
767         *
768         * @param expressions the expression to be concatenated dynamically
769         * @param expression the text description of the expression
770         * @return an expression which when evaluated will return the concatenated values
771         */
772        public static Expression concatExpression(final Collection<Expression> expressions, final String expression) {
773            return new ExpressionAdapter() {
774                public Object evaluate(Exchange exchange) {
775                    StringBuffer buffer = new StringBuffer();
776                    for (Expression expression : expressions) {
777                        String text = expression.evaluate(exchange, String.class);
778                        if (text != null) {
779                            buffer.append(text);
780                        }
781                    }
782                    return buffer.toString();
783                }
784    
785                @Override
786                public String toString() {
787                    if (expression != null) {
788                        return expression;
789                    } else {
790                        return "concat" + expressions;
791                    }
792                }
793            };
794        }
795    
796        /**
797         * Returns an Expression for the inbound message id
798         */
799        public static Expression messageIdExpression() {
800            return new ExpressionAdapter() {
801                public Object evaluate(Exchange exchange) {
802                    return exchange.getIn().getMessageId();
803                }
804    
805                @Override
806                public String toString() {
807                    return "messageId";
808                }
809            };
810        }
811    
812        public static Expression dateExpression(final String command, final String pattern) {
813            return new ExpressionAdapter() {
814                public Object evaluate(Exchange exchange) {
815                    Date date;
816                    if ("now".equals(command)) {
817                        date = new Date();
818                    } else if (command.startsWith("header.") || command.startsWith("in.header.")) {
819                        String key = command.substring(command.lastIndexOf('.') + 1);
820                        date = exchange.getIn().getHeader(key, Date.class);
821                        if (date == null) {
822                            throw new IllegalArgumentException("Cannot find java.util.Date object at " + command);
823                        }
824                    } else if (command.startsWith("out.header.")) {
825                        String key = command.substring(command.lastIndexOf('.') + 1);
826                        date = exchange.getOut().getHeader(key, Date.class);
827                        if (date == null) {
828                            throw new IllegalArgumentException("Cannot find java.util.Date object at " + command);
829                        }
830                    } else {
831                        throw new IllegalArgumentException("Command not supported for dateExpression: " + command);
832                    }
833    
834                    SimpleDateFormat df = new SimpleDateFormat(pattern);
835                    return df.format(date);
836                }
837    
838                @Override
839                public String toString() {
840                    return "date(" + command + ":" + pattern + ")";
841                }
842            };
843        }
844    
845        public static Expression simpleExpression(final String expression) {
846            return new ExpressionAdapter() {
847                public Object evaluate(Exchange exchange) {
848                    // resolve language using context to have a clear separation of packages
849                    // must call evalute to return the nested langauge evaluate when evaluating
850                    // stacked expressions
851                    Language language = exchange.getContext().resolveLanguage("simple");
852                    return language.createExpression(expression).evaluate(exchange, Object.class);
853                }
854    
855                @Override
856                public String toString() {
857                    return "simple(" + expression + ")";
858                }
859            };
860        }
861    
862        public static Expression beanExpression(final String expression) {
863            return new ExpressionAdapter() {
864                public Object evaluate(Exchange exchange) {
865                    // resolve language using context to have a clear separation of packages
866                    // must call evaluate to return the nested language evaluate when evaluating
867                    // stacked expressions
868                    Language language = exchange.getContext().resolveLanguage("bean");
869                    return language.createExpression(expression).evaluate(exchange, Object.class);
870                }
871    
872                @Override
873                public String toString() {
874                    return "bean(" + expression + ")";
875                }
876            };
877        }
878        
879        public static Expression beanExpression(final Class beanType, final String methodName) {
880            return BeanLanguage.bean(beanType, methodName);        
881        }
882    
883        public static Expression beanExpression(final String beanRef, final String methodName) {
884            String expression = methodName != null ? beanRef + "." + methodName : beanRef;
885            return beanExpression(expression);
886        }
887    
888        /**
889         * Returns an expression processing the exchange to the given endpoint uri
890         *
891         * @param uri endpoint uri to send the exchange to
892         * @return an expression object which will return the OUT body
893         */
894        public static Expression toExpression(final String uri) {
895            return new ExpressionAdapter() {
896                public Object evaluate(Exchange exchange) {
897                    Endpoint endpoint = exchange.getContext().getEndpoint(uri);
898                    if (endpoint == null) {
899                        throw new NoSuchEndpointException(uri);
900                    }
901    
902                    Producer producer;
903                    try {
904                        producer = endpoint.createProducer();
905                        producer.start();
906                        producer.process(exchange);
907                        producer.stop();
908                    } catch (Exception e) {
909                        throw ObjectHelper.wrapRuntimeCamelException(e);
910                    }
911    
912                    // return the OUT body, but check for exchange pattern
913                    if (ExchangeHelper.isOutCapable(exchange)) {
914                        return exchange.getOut().getBody();
915                    } else {
916                        return exchange.getIn().getBody();
917                    }
918                }
919    
920                @Override
921                public String toString() {
922                    return "to(" + uri + ")";
923                }
924            };
925        }
926    
927    
928    }