Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
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.NoTypeConversionAvailableException;
import org.apache.camel.Producer;
import org.apache.camel.RuntimeExchangeException;
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.language.simple.SimpleLanguage;
import org.apache.camel.model.language.MethodCallExpression;
import org.apache.camel.processor.DefaultExchangeFormatter;
import org.apache.camel.spi.ExchangeFormatter;
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.CamelContextHelper;
import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.FileUtil;
import org.apache.camel.util.GroupIterator;
import org.apache.camel.util.GroupTokenIterator;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.MessageHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.OgnlHelper;
import org.apache.camel.util.SkipIterator;
import org.apache.camel.util.StringHelper;
/**
* A helper class for working with expressions.
*
* @version
*/
public final class ExpressionBuilder {
private static final Pattern OFFSET_PATTERN = Pattern.compile("([+-])([^+-]+)");
/**
* 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 attachmentObjectsExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getIn().getAttachmentObjects();
}
@Override
public String toString() {
return "attachmentObjects";
}
};
}
/**
* Returns an expression for the inbound message attachments
*
* @return an expression object which will return the inbound message attachments
*/
public static Expression attachmentObjectValuesExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getIn().getAttachmentObjects().values();
}
@Override
public String toString() {
return "attachmentObjects";
}
};
}
/**
* 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) {
String name = simpleExpression(headerName).evaluate(exchange, String.class);
Object header = exchange.getIn().getHeader(name);
if (header == null) {
// fall back on a property
header = exchange.getProperty(name);
}
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) {
String name = simpleExpression(headerName).evaluate(exchange, String.class);
Object header = exchange.getIn().getHeader(name, type);
if (header == null) {
// fall back on a property
header = exchange.getProperty(name, 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 typeName 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 typeName) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Class type;
try {
String text = simpleExpression(typeName).evaluate(exchange, String.class);
type = exchange.getContext().getClassResolver().resolveMandatoryClass(text);
} catch (ClassNotFoundException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
String text = simpleExpression(headerName).evaluate(exchange, String.class);
Object header = exchange.getIn().getHeader(text, type);
if (header == null) {
// fall back on a property
header = exchange.getProperty(text, type);
}
return header;
}
@Override
public String toString() {
return "headerAs(" + headerName + ", " + typeName + ")";
}
};
}
/**
* 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) {
String text = simpleExpression(key).evaluate(exchange, String.class);
return exchange.getIn().getHeader(text);
}
});
}
/**
* 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;
}
String text = simpleExpression(headerName).evaluate(exchange, String.class);
Message out = exchange.getOut();
Object header = out.getHeader(text);
if (header == null) {
// let's try the exchange header
header = exchange.getProperty(text);
}
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;
}
// ognl is able to evaluate method name if it contains nested functions
// so we should not eager evaluate ognl as a string
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) {
String text = simpleExpression(ref).evaluate(exchange, String.class);
return exchange.getContext().getRegistry().lookupByName(text);
}
@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) {
String text = simpleExpression(propertyName).evaluate(exchange, String.class);
return exchange.getProperty(text);
}
@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) {
String text = simpleExpression(propertyName).evaluate(exchange, String.class);
return exchange.getProperty(text);
}
@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) {
String text = simpleExpression(key).evaluate(exchange, String.class);
return exchange.getProperty(text);
}
});
}
/**
* Returns an expression for the properties of exchange
*
* @return an expression object which will return the properties
* @deprecated use {@link #exchangeExceptionExpression()} instead
*/
@Deprecated
public static Expression propertiesExpression() {
return exchangeExceptionExpression();
}
/**
* Returns an expression for the exchange properties of exchange
*
* @return an expression object which will return the exchange properties
*/
public static Expression exchangePropertiesExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
return exchange.getProperties();
}
@Override
public String toString() {
return "exchangeProperties";
}
};
}
/**
* 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().getGlobalOptions();
}
@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) {
String text = simpleExpression(propertyName).evaluate(exchange, String.class);
return exchange.getContext().getGlobalOption(text);
}
@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) {
String text = simpleExpression(propertyName).evaluate(exchange, String.class);
String text2 = simpleExpression(defaultValue).evaluate(exchange, String.class);
return System.getProperty(text, text2);
}
@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 text = simpleExpression(propertyName).evaluate(exchange, String.class);
String answer = System.getenv(text.toUpperCase());
if (answer == null) {
String text2 = simpleExpression(defaultValue).evaluate(exchange, String.class);
answer = text2;
}
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
String text = simpleExpression(name).evaluate(exchange, String.class);
Class type = exchange.getContext().getClassResolver().resolveClass(text);
if (type != null) {
return type;
}
int pos = text.lastIndexOf(".");
if (pos > 0) {
String before = text.substring(0, pos);
String after = text.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 " + text));
}
@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