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;
018    
019    /**
020     * @version $Revision: $
021     */
022    public class Expressions {
023    
024        /**
025         * Returns an expression for the header value with the given name
026         *
027         * @param headerName the name of the header the expression will return
028         * @return an expression object which will return the header value
029         */
030        public static <E extends Exchange> Expression<E> headerExpression(final String headerName) {
031            return new Expression<E>() {
032                public Object evaluate(E exchange) {
033                    Object header = exchange.getIn().getHeaders().getHeader(headerName);
034                    if (header == null) {
035                        // lets try the exchange header
036                        header = exchange.getHeaders().getHeader(headerName);
037                    }
038                    return header;
039                }
040    
041                @Override
042                public String toString() {
043                    return "header(" + headerName + ")";
044                }
045            };
046        }
047        /**
048         * Returns an expression for the contant value
049         *
050         * @param value the value the expression will return
051         * @return an expression object which will return the constant value
052         */
053        public static <E extends Exchange> Expression<E> constantExpression(final Object value) {
054            return new Expression<E>() {
055                public Object evaluate(E exchange) {
056                    return value;
057                }
058    
059                @Override
060                public String toString() {
061                    return "" + value;
062                }
063            };
064        }
065    }