use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method tokenizeExpression.
/**
* Returns a tokenize expression which will tokenize the string with the
* given token
*/
public static Expression tokenizeExpression(final Expression expression, final String token) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String text = simpleExpression(token).evaluate(exchange, String.class);
Object value = expression.evaluate(exchange, Object.class);
Scanner scanner = ObjectHelper.getScanner(exchange, value);
scanner.useDelimiter(text);
return scanner;
}
@Override
public String toString() {
return "tokenize(" + expression + ", " + token + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method typeExpression.
/**
* 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;
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method bodyOgnlExpression.
/**
* 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 bodyOgnlExpression(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 = exchange.getIn().getBody(type);
if (body != null) {
// 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);
} else {
return null;
}
}
@Override
public String toString() {
return "bodyOgnlAs[" + name + "](" + ognl + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method skipFirstExpression.
/**
* Returns an expression that skips the first element
*/
public static Expression skipFirstExpression(final Expression expression) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
Object value = expression.evaluate(exchange, Object.class);
Iterator it = exchange.getContext().getTypeConverter().tryConvertTo(Iterator.class, exchange, value);
if (it != null) {
// skip first
it.next();
return it;
} else {
return value;
}
}
@Override
public String toString() {
return "skipFirst(" + expression + ")";
}
};
}
use of org.apache.camel.support.ExpressionAdapter in project camel by apache.
the class ExpressionBuilder method exchangeExceptionStackTraceExpression.
/**
* 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";
}
};
}
Aggregations