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.language.simple;
018
019 import org.apache.camel.Exchange;
020 import org.apache.camel.Expression;
021 import org.apache.camel.builder.ExpressionBuilder;
022 import org.apache.camel.language.IllegalSyntaxException;
023 import org.apache.camel.util.ObjectHelper;
024
025 /**
026 * A <a href="http://activemq.apache.org/camel/simple.html">simple language</a>
027 * which maps simple property style notations to access headers and bodies.
028 * Examples of supported expressions are:
029 * <ul>
030 * <li>id to access the inbound message Id</li>
031 * <li>in.body or body to access the inbound body</li>
032 * <li>out.body to access the inbound body</li>
033 * <li>in.header.foo or header.foo to access an inbound header called 'foo'</li>
034 * <li>out.header.foo to access an outbound header called 'foo'</li>
035 * <li>property.foo to access the exchange property called 'foo'</li>
036 * <li>sys.foo to access the system property called 'foo'</li>
037 * </ul>
038 *
039 * @version $Revision: 47086 $
040 */
041 public class SimpleLanguage extends AbstractSimpleLanguage {
042
043 public static Expression simple(String expression) {
044 SimpleLanguage language = new SimpleLanguage();
045 return language.createExpression(expression);
046 }
047
048 protected Expression<Exchange> createSimpleExpression(String expression) {
049 if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
050 return ExpressionBuilder.bodyExpression();
051 } else if (ObjectHelper.equal(expression, "out.body")) {
052 return ExpressionBuilder.outBodyExpression();
053 } else if (ObjectHelper.equal(expression, "id")) {
054 return ExpressionBuilder.messageIdExpression();
055 }
056
057 // in header expression
058 String remainder = ifStartsWithReturnRemainder("in.header.", expression);
059 if (remainder == null) {
060 remainder = ifStartsWithReturnRemainder("header.", expression);
061 }
062 if (remainder == null) {
063 remainder = ifStartsWithReturnRemainder("headers.", expression);
064 }
065 if (remainder == null) {
066 remainder = ifStartsWithReturnRemainder("in.headers.", expression);
067 }
068 if (remainder != null) {
069 return ExpressionBuilder.headerExpression(remainder);
070 }
071
072 // out header expression
073 remainder = ifStartsWithReturnRemainder("out.header.", expression);
074 if (remainder == null) {
075 remainder = ifStartsWithReturnRemainder("out.headers.", expression);
076 }
077 if (remainder != null) {
078 return ExpressionBuilder.outHeaderExpression(remainder);
079 }
080
081 // property
082 remainder = ifStartsWithReturnRemainder("property.", expression);
083 if (remainder != null) {
084 return ExpressionBuilder.propertyExpression(remainder);
085 }
086
087 // system property
088 remainder = ifStartsWithReturnRemainder("sys.", expression);
089 if (remainder != null) {
090 return ExpressionBuilder.systemProperty(remainder);
091 }
092
093 throw new IllegalSyntaxException(this, expression);
094 }
095
096 }