org.apache.camel.builder.ExpressionBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-core Show documentation
Show all versions of camel-core Show documentation
The Core Camel Java DSL based router
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.builder;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Message;
import org.apache.camel.NoSuchEndpointException;
import org.apache.camel.NoSuchLanguageException;
import org.apache.camel.Producer;
import org.apache.camel.component.bean.BeanInvocation;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.language.bean.BeanLanguage;
import org.apache.camel.model.language.MethodCallExpression;
import org.apache.camel.spi.Language;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.spi.UnitOfWork;
import org.apache.camel.support.ExpressionAdapter;
import org.apache.camel.support.TokenPairExpressionIterator;
import org.apache.camel.support.TokenXMLExpressionIterator;
import org.apache.camel.support.XMLTokenExpressionIterator;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.GroupIterator;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.OgnlHelper;
/**
* A helper class for working with expressions.
*
* @version
*/
public final class ExpressionBuilder {
/**
* Utility classes should not have a public constructor.
*/
private ExpressionBuilder() {
}
/**
* Returns an expression for the inbound message attachments
*
* @return an expression object which will return the inbound message attachments
*/
public static Expression attachmentsExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getIn().getAttachments();
}
@Override
public String toString() {
return "attachments";
}
};
}
/**
* Returns an expression for the inbound message attachments
*
* @return an expression object which will return the inbound message attachments
*/
public static Expression attachmentValuesExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getIn().getAttachments().values();
}
@Override
public String toString() {
return "attachments";
}
};
}
/**
* Returns an expression for the header value with the given name
*
* 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 headerExpression(final String headerName) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Object header = exchange.getIn().getHeader(headerName);
if (header == null) {
// fall back on a property
header = exchange.getProperty(headerName);
}
return header;
}
@Override
public String toString() {
return "header(" + headerName + ")";
}
};
}
/**
* Returns an expression for the header value with the given name converted to the given type
*
* Will fallback and look in properties if not found in headers.
*
* @param headerName the name of the header the expression will return
* @param type the type to convert to
* @return an expression object which will return the header value
*/
public static Expression headerExpression(final String headerName, final Class type) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Object header = exchange.getIn().getHeader(headerName, type);
if (header == null) {
// fall back on a property
header = exchange.getProperty(headerName, type);
}
return header;
}
@Override
public String toString() {
return "headerAs(" + headerName + ", " + type + ")";
}
};
}
/**
* Returns an expression for the header value with the given name converted to the given type
*
* Will fallback and look in properties if not found in headers.
*
* @param headerName the name of the header the expression will return
* @param name the type to convert to as a FQN class name
* @return an expression object which will return the header value
*/
public static Expression headerExpression(final String headerName, final String name) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Class type;
try {
type = exchange.getContext().getClassResolver().resolveMandatoryClass(name);
} catch (ClassNotFoundException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
Object header = exchange.getIn().getHeader(headerName, type);
if (header == null) {
// fall back on a property
header = exchange.getProperty(headerName, type);
}
return header;
}
@Override
public String toString() {
return "headerAs(" + headerName + ", " + name + ")";
}
};
}
/**
* Returns the expression for the exchanges inbound message header invoking methods defined
* in a simple OGNL notation
*
* @param ognl methods to invoke on the header in a simple OGNL syntax
*/
public static Expression headersOgnlExpression(final String ognl) {
return new KeyedOgnlExpressionAdapter(ognl, "headerOgnl(" + ognl + ")",
new KeyedOgnlExpressionAdapter.KeyedEntityRetrievalStrategy() {
public Object getKeyedEntity(Exchange exchange, String key) {
return exchange.getIn().getHeader(key);
}
});
}
/**
* Returns an expression for the inbound message headers
*
* @return an expression object which will return the inbound headers
*/
public static Expression headersExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getIn().getHeaders();
}
@Override
public String toString() {
return "headers";
}
};
}
/**
* Returns an expression for the out header value with the given name
*
* 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;
}
Message out = exchange.getOut();
Object header = out.getHeader(headerName);
if (header == null) {
// let's try the exchange header
header = exchange.getProperty(headerName);
}
return header;
}
@Override
public String toString() {
return "outHeader(" + headerName + ")";
}
};
}
/**
* Returns an expression for the outbound message headers
*
* @return an expression object which will return the headers, will be null if the
* exchange is not out capable.
*/
public static Expression outHeadersExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
// only get out headers if the MEP is out capable
if (ExchangeHelper.isOutCapable(exchange)) {
return exchange.getOut().getHeaders();
} else {
return null;
}
}
@Override
public String toString() {
return "outHeaders";
}
};
}
/**
* Returns an expression for the exchange pattern
*
* @see org.apache.camel.Exchange#getPattern()
* @return an expression object which will return the exchange pattern
*/
public static Expression exchangePatternExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getPattern();
}
@Override
public String toString() {
return "exchangePattern";
}
};
}
/**
* Returns an expression for an exception set on the exchange
*
* @see Exchange#getException()
* @return an expression object which will return the exception set on the exchange
*/
public static Expression exchangeExceptionExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Exception exception = exchange.getException();
if (exception == null) {
exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
}
return exception;
}
@Override
public String toString() {
return "exchangeException";
}
};
}
/**
* Returns an expression for an exception set on the exchange
*
* Is used to get the caused exception that typically have been wrapped in some sort
* of Camel wrapper exception
* @param type the exception type
* @see Exchange#getException(Class)
* @return an expression object which will return the exception set on the exchange
*/
public static Expression exchangeExceptionExpression(final Class type) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Exception exception = exchange.getException(type);
if (exception == null) {
exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
return ObjectHelper.getException(type, exception);
}
return exception;
}
@Override
public String toString() {
return "exchangeException[" + type + "]";
}
};
}
/**
* Returns the expression for the exchanges exception invoking methods defined
* in a simple OGNL notation
*
* @param ognl methods to invoke on the body in a simple OGNL syntax
*/
public static Expression exchangeExceptionOgnlExpression(final String ognl) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Object exception = exchange.getException();
if (exception == null) {
exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
}
if (exception == null) {
return null;
}
return new MethodCallExpression(exception, ognl).evaluate(exchange);
}
@Override
public String toString() {
return "exchangeExceptionOgnl(" + ognl + ")";
}
};
}
/**
* Returns an expression for the type converter
*
* @return an expression object which will return the type converter
*/
public static Expression typeConverterExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getContext().getTypeConverter();
}
@Override
public String toString() {
return "typeConverter";
}
};
}
/**
* Returns an expression for the {@link org.apache.camel.spi.Registry}
*
* @return an expression object which will return the registry
*/
public static Expression registryExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getContext().getRegistry();
}
@Override
public String toString() {
return "registry";
}
};
}
/**
* Returns an expression for lookup a bean in the {@link org.apache.camel.spi.Registry}
*
* @return an expression object which will return the bean
*/
public static Expression refExpression(final String ref) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getContext().getRegistry().lookupByName(ref);
}
@Override
public String toString() {
return "ref(" + ref + ")";
}
};
}
/**
* Returns an expression for the {@link org.apache.camel.CamelContext}
*
* @return an expression object which will return the camel context
*/
public static Expression camelContextExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getContext();
}
@Override
public String toString() {
return "camelContext";
}
};
}
/**
* Returns an expression for the {@link org.apache.camel.CamelContext} name
*
* @return an expression object which will return the camel context name
*/
public static Expression camelContextNameExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getContext().getName();
}
@Override
public String toString() {
return "camelContextName";
}
};
}
/**
* Returns an expression for an exception message set on the exchange
*
* @see Exchange.getException().getMessage()
* @return an expression object which will return the exception message set on the exchange
*/
public static Expression exchangeExceptionMessageExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Exception exception = exchange.getException();
if (exception == null) {
exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
}
return exception != null ? exception.getMessage() : null;
}
@Override
public String toString() {
return "exchangeExceptionMessage";
}
};
}
/**
* Returns an expression for an exception stacktrace set on the exchange
*
* @return an expression object which will return the exception stacktrace set on the exchange
*/
public static Expression exchangeExceptionStackTraceExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Exception exception = exchange.getException();
if (exception == null) {
exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
}
if (exception != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
IOHelper.close(pw, sw);
return sw.toString();
} else {
return null;
}
}
@Override
public String toString() {
return "exchangeExceptionStackTrace";
}
};
}
/**
* Returns an expression for the property value of exchange with the given name
*
* @param propertyName the name of the property the expression will return
* @return an expression object which will return the property value
* @deprecated use {@link #exchangePropertyExpression(String)} instead
*/
@Deprecated
public static Expression propertyExpression(final String propertyName) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getProperty(propertyName);
}
@Override
public String toString() {
return "exchangeProperty(" + propertyName + ")";
}
};
}
/**
* Returns an expression for the property value of exchange with the given name
*
* @param propertyName the name of the property the expression will return
* @return an expression object which will return the property value
*/
public static Expression exchangePropertyExpression(final String propertyName) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getProperty(propertyName);
}
@Override
public String toString() {
return "exchangeProperty(" + propertyName + ")";
}
};
}
/**
* Returns an expression for the property value of exchange with the given name invoking methods defined
* in a simple OGNL notation
*
* @param ognl methods to invoke on the property in a simple OGNL syntax
*/
public static Expression propertyOgnlExpression(final String ognl) {
return new KeyedOgnlExpressionAdapter(ognl, "propertyOgnl(" + ognl + ")",
new KeyedOgnlExpressionAdapter.KeyedEntityRetrievalStrategy() {
public Object getKeyedEntity(Exchange exchange, String key) {
return exchange.getProperty(key);
}
});
}
/**
* Returns an expression for the properties of exchange
*
* @return an expression object which will return the properties
*/
public static Expression propertiesExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getProperties();
}
@Override
public String toString() {
return "properties";
}
};
}
/**
* Returns an expression for the properties of the camel context
*
* @return an expression object which will return the properties
*/
public static Expression camelContextPropertiesExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getContext().getProperties();
}
@Override
public String toString() {
return "camelContextProperties";
}
};
}
/**
* Returns an expression for the property value of the camel context with the given name
*
* @param propertyName the name of the property the expression will return
* @return an expression object which will return the property value
*/
public static Expression camelContextPropertyExpression(final String propertyName) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getContext().getProperty(propertyName);
}
@Override
public String toString() {
return "camelContextProperty(" + propertyName + ")";
}
};
}
/**
* Returns an expression for a system property value with the given name
*
* @param propertyName the name of the system property the expression will return
* @return an expression object which will return the system property value
*/
public static Expression systemPropertyExpression(final String propertyName) {
return systemPropertyExpression(propertyName, null);
}
/**
* Returns an expression for a system property value with the given name
*
* @param propertyName the name of the system property the expression will return
* @param defaultValue default value to return if no system property exists
* @return an expression object which will return the system property value
*/
public static Expression systemPropertyExpression(final String propertyName,
final String defaultValue) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return System.getProperty(propertyName, defaultValue);
}
@Override
public String toString() {
return "systemProperty(" + propertyName + ")";
}
};
}
/**
* Returns an expression for a system environment value with the given name
*
* @param propertyName the name of the system environment the expression will return
* @return an expression object which will return the system property value
*/
public static Expression systemEnvironmentExpression(final String propertyName) {
return systemEnvironmentExpression(propertyName, null);
}
/**
* Returns an expression for a system environment value with the given name
*
* @param propertyName the name of the system environment the expression will return
* @param defaultValue default value to return if no system environment exists
* @return an expression object which will return the system environment value
*/
public static Expression systemEnvironmentExpression(final String propertyName,
final String defaultValue) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String answer = System.getenv(propertyName);
if (answer == null) {
answer = defaultValue;
}
return answer;
}
@Override
public String toString() {
return "systemEnvironment(" + propertyName + ")";
}
};
}
/**
* Returns an expression for the constant value
*
* @param value the value the expression will return
* @return an expression object which will return the constant value
*/
public static Expression constantExpression(final Object value) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return value;
}
@Override
public String toString() {
return "" + value;
}
};
}
/**
* Returns an expression for evaluating the expression/predicate using the given language
*
* @param expression the expression or predicate
* @return an expression object which will evaluate the expression/predicate using the given language
*/
public static Expression languageExpression(final String language, final String expression) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Language lan = exchange.getContext().resolveLanguage(language);
if (lan != null) {
return lan.createExpression(expression).evaluate(exchange, Object.class);
} else {
throw new NoSuchLanguageException(language);
}
}
@Override
public boolean matches(Exchange exchange) {
Language lan = exchange.getContext().resolveLanguage(language);
if (lan != null) {
return lan.createPredicate(expression).matches(exchange);
} else {
throw new NoSuchLanguageException(language);
}
}
@Override
public String toString() {
return "language[" + language + ":" + expression + "]";
}
};
}
/**
* Returns an expression for a type value
*
* @param name the type name
* @return an expression object which will return the type value
*/
public static Expression typeExpression(final String name) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
// it may refer to a class type
Class type = exchange.getContext().getClassResolver().resolveClass(name);
if (type != null) {
return type;
}
int pos = name.lastIndexOf(".");
if (pos > 0) {
String before = name.substring(0, pos);
String after = name.substring(pos + 1);
type = exchange.getContext().getClassResolver().resolveClass(before);
if (type != null) {
return ObjectHelper.lookupConstantFieldValue(type, after);
}
}
throw ObjectHelper.wrapCamelExecutionException(exchange, new ClassNotFoundException("Cannot find type " + name));
}
@Override
public String toString() {
return "type:" + name;
}
};
}
/**
* Returns an expression that caches the evaluation of another expression
* and returns the cached value, to avoid re-evaluating the expression.
*
* @param expression the target expression to cache
* @return the cached value
*/
public static Expression cacheExpression(final Expression expression) {
return new ExpressionAdapter() {
private final AtomicReference