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 */ 017package org.apache.camel.builder; 018 019import org.apache.camel.Expression; 020import org.apache.camel.model.language.ConstantExpression; 021import org.apache.camel.model.language.ExchangePropertyExpression; 022import org.apache.camel.model.language.HeaderExpression; 023import org.apache.camel.model.language.LanguageExpression; 024import org.apache.camel.model.language.MethodCallExpression; 025import org.apache.camel.model.language.SimpleExpression; 026import org.apache.camel.util.ObjectHelper; 027 028/** 029 * A helper class for including portions of the 030 * <a href="http://camel.apache.org/expression.html">expression</a> and 031 * <a href="http://camel.apache.org/predicate.html">predicate</a> 032 * <a href="http://camel.apache.org/dsl.html">Java DSL</a> 033 * <p/> 034 * Implementation of this builder should favor build expressions using the 035 * definition classes from the <tt>org.apache.camel.model.language</tt> package, 036 * to build the routes using the same types as it would happen when using XML 037 * DSL. 038 */ 039public final class Builder { 040 041 /** 042 * Utility classes should not have a public constructor. 043 */ 044 private Builder() { 045 } 046 047 /** 048 * Returns a <a href="http://camel.apache.org/bean-language.html">bean 049 * expression</a> value builder. 050 * <p/> 051 * This method accepts dual parameters. Either an bean instance or a 052 * reference to a bean (String). 053 * 054 * @param beanOrBeanRef either an instanceof a bean or a reference to bean 055 * to lookup in the Registry 056 * @return the builder 057 */ 058 public static ValueBuilder bean(final Object beanOrBeanRef) { 059 return bean(beanOrBeanRef, null); 060 } 061 062 /** 063 * Returns a <a href="http://camel.apache.org/bean-language.html">bean 064 * expression</a> value builder. 065 * <p/> 066 * This method accepts dual parameters. Either an bean instance or a 067 * reference to a bean (String). 068 * 069 * @param beanOrBeanRef either an instanceof a bean or a reference to bean 070 * to lookup in the Registry 071 * @param method the method name 072 * @return the builder 073 */ 074 public static ValueBuilder bean(Object beanOrBeanRef, String method) { 075 Expression exp; 076 if (beanOrBeanRef instanceof String) { 077 exp = new MethodCallExpression((String)beanOrBeanRef, method); 078 } else { 079 exp = new MethodCallExpression(beanOrBeanRef, method); 080 } 081 return new ValueBuilder(exp); 082 } 083 084 /** 085 * Returns a <a href="http://camel.apache.org/bean-language.html">bean 086 * expression</a> value builder 087 * 088 * @param beanType the bean class which will be invoked 089 * @param method name of method to invoke 090 * @return the builder 091 */ 092 public static ValueBuilder bean(Class<?> beanType, String method) { 093 Expression exp = new MethodCallExpression(beanType, method); 094 return new ValueBuilder(exp); 095 } 096 097 /** 098 * Returns a constant expression 099 */ 100 public static ValueBuilder constant(Object value) { 101 Expression exp; 102 if (value instanceof String) { 103 exp = new ConstantExpression((String)value); 104 } else { 105 exp = ExpressionBuilder.constantExpression(value); 106 } 107 return new ValueBuilder(exp); 108 } 109 110 /** 111 * Returns a constant expression 112 */ 113 public static ValueBuilder language(String language, String expression) { 114 Expression exp = new LanguageExpression(language, expression); 115 return new ValueBuilder(exp); 116 } 117 118 /** 119 * Returns a simple expression 120 */ 121 public static ValueBuilder simple(String value) { 122 Expression exp = new SimpleExpression(value); 123 return new ValueBuilder(exp); 124 } 125 126 /** 127 * Returns a simple expression 128 */ 129 public static ValueBuilder simple(String value, Class<?> resultType) { 130 SimpleExpression exp = new SimpleExpression(value); 131 exp.setResultType(resultType); 132 return new ValueBuilder(exp); 133 } 134 135 /** 136 * Returns a predicate and value builder for headers on an exchange 137 */ 138 public static ValueBuilder header(String name) { 139 Expression exp = new HeaderExpression(name); 140 return new ValueBuilder(exp); 141 } 142 143 /** 144 * Returns a predicate and value builder for properties on an exchange 145 */ 146 public static ValueBuilder exchangeProperty(String name) { 147 Expression exp = new ExchangePropertyExpression(name); 148 return new ValueBuilder(exp); 149 } 150 151 /** 152 * Returns a predicate and value builder for the inbound body on an exchange 153 */ 154 public static ValueBuilder body() { 155 Expression exp = new SimpleExpression("${body}"); 156 return new ValueBuilder(exp); 157 } 158 159 /** 160 * Returns a predicate and value builder for the inbound message body as a 161 * specific type 162 */ 163 public static <T> ValueBuilder bodyAs(Class<T> type) { 164 ObjectHelper.notNull(type, "type"); 165 Expression exp = new SimpleExpression(String.format("${bodyAs(%s)}", type.getCanonicalName())); 166 return new ValueBuilder(exp); 167 } 168 169 /** 170 * Returns an expression for the given system property 171 */ 172 public static ValueBuilder systemProperty(final String name) { 173 Expression exp = new SimpleExpression(String.format("${sys.%s}", name)); 174 return new ValueBuilder(exp); 175 } 176 177 /** 178 * Returns an expression for the given system property 179 */ 180 public static ValueBuilder systemProperty(final String name, final String defaultValue) { 181 return new ValueBuilder(ExpressionBuilder.systemPropertyExpression(name, defaultValue)); 182 } 183 184 /** 185 * Returns a predicate and value builder for the exception message on an 186 * exchange 187 */ 188 public static ValueBuilder exceptionMessage() { 189 Expression exp = new SimpleExpression("${exception.message}"); 190 return new ValueBuilder(exp); 191 } 192 193 /** 194 * Returns a predicate and value builder for the exception stacktrace on an 195 * exchange 196 */ 197 public static ValueBuilder exceptionStackTrace() { 198 Expression exp = new SimpleExpression("${exception.stacktrace}"); 199 return new ValueBuilder(exp); 200 } 201 202 /** 203 * Returns an expression that replaces all occurrences of the regular 204 * expression with the given replacement 205 */ 206 public static ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) { 207 Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement); 208 return new ValueBuilder(newExp); 209 } 210 211 /** 212 * Returns an expression that replaces all occurrences of the regular 213 * expression with the given replacement 214 */ 215 public static ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) { 216 Expression newExp = ExpressionBuilder.regexReplaceAll(content, regex, replacement); 217 return new ValueBuilder(newExp); 218 } 219 220}