use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method outHeaderExpression.
/**
* Returns an expression for the out header value with the given name
* <p/>
* 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 + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method mandatoryBodyOgnlExpression.
/**
* Returns the expression for the exchanges inbound message body converted
* to the given type and invoking methods on the converted body defined in a simple OGNL notation
*/
public static Expression mandatoryBodyOgnlExpression(final String name, final String ognl) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String text = simpleExpression(name).evaluate(exchange, String.class);
Class<?> type;
try {
type = exchange.getContext().getClassResolver().resolveMandatoryClass(text);
} catch (ClassNotFoundException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
Object body;
try {
body = exchange.getIn().getMandatoryBody(type);
} catch (InvalidPayloadException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
// ognl is able to evaluate method name if it contains nested functions
// so we should not eager evaluate ognl as a string
MethodCallExpression call = new MethodCallExpression(exchange, ognl);
// set the instance to use
call.setInstance(body);
return call.evaluate(exchange);
}
@Override
public String toString() {
return "mandatoryBodyAs[" + name + "](" + ognl + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method regexTokenizeExpression.
/**
* Returns a tokenize expression which will tokenize the string with the
* given regex
*/
public static Expression regexTokenizeExpression(final Expression expression, final String regexTokenizer) {
final Pattern pattern = Pattern.compile(regexTokenizer);
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Object value = expression.evaluate(exchange, Object.class);
Scanner scanner = ObjectHelper.getScanner(exchange, value);
scanner.useDelimiter(pattern);
return scanner;
}
@Override
public String toString() {
return "regexTokenize(" + expression + ", " + pattern.pattern() + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method dateExpression.
public static Expression dateExpression(final String commandWithOffsets, final String timezone, final String pattern) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
// Capture optional time offsets
String command = commandWithOffsets.split("[+-]", 2)[0].trim();
List<Long> offsets = new ArrayList<>();
Matcher offsetMatcher = OFFSET_PATTERN.matcher(commandWithOffsets);
while (offsetMatcher.find()) {
try {
long value = exchange.getContext().getTypeConverter().mandatoryConvertTo(long.class, exchange, offsetMatcher.group(2).trim());
offsets.add(offsetMatcher.group(1).equals("+") ? value : -value);
} catch (NoTypeConversionAvailableException e) {
throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
}
Date date;
if ("now".equals(command)) {
date = new Date();
} else if (command.startsWith("header.") || command.startsWith("in.header.")) {
String key = command.substring(command.lastIndexOf('.') + 1);
date = exchange.getIn().getHeader(key, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
}
} else if (command.startsWith("out.header.")) {
String key = command.substring(command.lastIndexOf('.') + 1);
date = exchange.getOut().getHeader(key, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find java.util.Date object at command: " + command);
}
} else if ("file".equals(command)) {
Long num = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Long.class);
if (num != null && num > 0) {
date = new Date(num);
} else {
date = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Date.class);
if (date == null) {
throw new IllegalArgumentException("Cannot find " + Exchange.FILE_LAST_MODIFIED + " header at command: " + command);
}
}
} else {
throw new IllegalArgumentException("Command not supported for dateExpression: " + command);
}
// Apply offsets
long dateAsLong = date.getTime();
for (long offset : offsets) {
dateAsLong += offset;
}
date = new Date(dateAsLong);
if (pattern != null && !pattern.isEmpty()) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
if (timezone != null && !timezone.isEmpty()) {
df.setTimeZone(TimeZone.getTimeZone(timezone));
}
return df.format(date);
} else {
return date;
}
}
@Override
public String toString() {
return "date(" + commandWithOffsets + ":" + pattern + ":" + timezone + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method regexReplaceAll.
/**
* Transforms the expression into a String then performs the regex
* replaceAll to transform the String and return the result
*/
public static Expression regexReplaceAll(final Expression expression, final String regex, final Expression replacementExpression) {
final Pattern pattern = Pattern.compile(regex);
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String text = expression.evaluate(exchange, String.class);
String replacement = replacementExpression.evaluate(exchange, String.class);
if (text == null || replacement == null) {
return null;
}
return pattern.matcher(text).replaceAll(replacement);
}
@Override
public String toString() {
return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")";
}
};
}
Aggregations