@Override @SuppressWarnings({"unchecked", "rawtypes"}) public Processor createProcessor(RouteContext routeContext) throws Exception { // insert a set header definition so we can set the intercepted endpoint uri as a header // this allows us to use the same header for both the interceptFrom and interceptSendToEndpoint SetHeaderDefinition headerDefinition = new SetHeaderDefinition(Exchange.INTERCEPTED_ENDPOINT, new ExpressionAdapter() { public Object evaluate(Exchange exchange, Class type) { if (exchange.getFromEndpoint() != null) { return exchange.getFromEndpoint().getEndpointUri(); } else { return null; } } public String toString() { return ""; } }); getOutputs().add(0, headerDefinition); return this.createChildProcessor(routeContext, true); }
public Expression createExpression(final String expression) { final Expression exp = RefLanguage.ref(expression); return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { Expression lookup = exp.evaluate(exchange, Expression.class); if (lookup != null) { return lookup.evaluate(exchange, Object.class); } else { throw new IllegalArgumentException("Cannot find expression in registry with ref: " + expression); } } public String toString() { return exp.toString(); } }; }
/** * Returns an expression for the 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 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 * <p/> * 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 <T> Expression headerExpression(final String headerName, final Class<T> 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 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 + ")"; } }; }
/** * Returns an expression for the outbound message headers * * @return an expression object which will return the headers, will be <tt>null</tt> 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 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 * <p/> * 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<Exception> 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 an exception message set on the exchange * * @see <tt>Exchange.getException().getMessage()</tt> * @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 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 * @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); 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 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<Object> cache = new AtomicReference<Object>(); public Object evaluate(Exchange exchange) { Object answer = cache.get(); if (answer == null) { answer = expression.evaluate(exchange, Object.class); cache.set(answer); } return answer; } @Override public String toString() { return expression.toString(); } }; }
/** * Returns the expression for the exchanges inbound message body invoking methods defined * in a simple OGNL notation * * @param ognl methods to invoke on the body in a simple OGNL syntax */ public static Expression bodyOgnlExpression(final String ognl) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { Object body = exchange.getIn().getBody(); if (body == 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(body, ognl).evaluate(exchange); } @Override public String toString() { return "bodyOgnl(" + ognl + ")"; } }; }
/** * Returns the expression for invoking a method (support OGNL syntax) on the given expression * * @param exp the expression to evaluate and invoke the method on its result * @param ognl methods to invoke on the evaluated expression in a simple OGNL syntax */ public static Expression ognlExpression(final Expression exp, final String ognl) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { Object value = exp.evaluate(exchange, Object.class); if (value == 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(value, ognl).evaluate(exchange); } @Override public String toString() { return "ognl(" + exp + ", " + ognl + ")"; } }; }
/** * Returns the expression for the exchanges camelContext invoking methods defined * in a simple OGNL notation * * @param ognl methods to invoke on the context in a simple OGNL syntax */ public static Expression camelContextOgnlExpression(final String ognl) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { CamelContext context = exchange.getContext(); if (context == 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(context, ognl).evaluate(exchange); } @Override public String toString() { return "camelContextOgnl(" + ognl + ")"; } }; }
/** * Returns the expression for the exchanges inbound message body converted * to the given type */ public static Expression bodyExpression(final String name) { 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); } return exchange.getIn().getBody(type); } @Override public String toString() { return "bodyAs[" + name + "]"; } }; }
/** * Returns the expression for the out messages body */ public static Expression outBodyExpression() { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { if (exchange.hasOut()) { return exchange.getOut().getBody(); } else { return null; } } @Override public String toString() { return "outBody"; } }; }
/** * Returns the expression for the exchanges outbound message body converted * to the given type */ public static <T> Expression outBodyExpression(final Class<T> type) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { if (exchange.hasOut()) { return exchange.getOut().getBody(type); } else { return null; } } @Override public String toString() { return "outBodyAs[" + type.getName() + "]"; } }; }
/** * Returns an expression which converts the given expression to the given type */ public static Expression convertToExpression(final Expression expression, final Class<?> type) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { if (type != null) { return expression.evaluate(exchange, type); } else { return expression; } } @Override public String toString() { return "" + expression; } }; }
/** * Returns an expression which converts the given expression to the given type the type * expression is evaluated to */ public static Expression convertToExpression(final Expression expression, final Expression type) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { Object result = type.evaluate(exchange, Object.class); if (result != null) { return expression.evaluate(exchange, result.getClass()); } else { return expression; } } @Override public String toString() { return "" + expression; } }; }
/** * 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 + ")"; } }; }
/** * 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 + ")"; } }; }
/** * 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() + ")"; } }; }
public static Expression groupXmlIteratorExpression(final Expression expression, final int group) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { // evaluate expression as iterator Iterator<?> it = expression.evaluate(exchange, Iterator.class); ObjectHelper.notNull(it, "expression: " + expression + " evaluated on " + exchange + " must return an java.util.Iterator"); // must use GroupTokenIterator in xml mode as we want to concat the xml parts into a single message return new GroupTokenIterator(exchange, it, null, group, false); } @Override public String toString() { return "group " + expression + " " + group + " times"; } }; }
public static Expression groupIteratorExpression(final Expression expression, final String token, final int group, final boolean skipFirst) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { // evaluate expression as iterator Iterator<?> it = expression.evaluate(exchange, Iterator.class); ObjectHelper.notNull(it, "expression: " + expression + " evaluated on " + exchange + " must return an java.util.Iterator"); if (token != null) { return new GroupTokenIterator(exchange, it, token, group, skipFirst); } else { return new GroupIterator(exchange, it, group, skipFirst); } } @Override public String toString() { return "group " + expression + " " + group + " times"; } }; }
/** * Returns a sort expression which will sort the expression with the given comparator. * <p/> * The expression is evaluated as a {@link List} object to allow sorting. */ @SuppressWarnings({"unchecked", "rawtypes"}) public static Expression sortExpression(final Expression expression, final Comparator comparator) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { List<?> list = expression.evaluate(exchange, List.class); Collections.sort(list, comparator); return list; } @Override public String toString() { return "sort(" + expression + " by: " + comparator + ")"; } }; }
/** * 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 String replacement) { final Pattern pattern = Pattern.compile(regex); return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { String text = expression.evaluate(exchange, String.class); if (text == null) { return null; } return pattern.matcher(text).replaceAll(replacement); } @Override public String toString() { return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")"; } }; }
/** * 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() + ")"; } }; }
/** * Returns an expression which returns the string concatenation value of the various * expressions * * @param expressions the expression to be concatenated dynamically * @param desription the text description of the expression * @return an expression which when evaluated will return the concatenated values */ public static Expression concatExpression(final Collection<Expression> expressions, final String desription) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { StringBuilder buffer = new StringBuilder(); for (Expression expression : expressions) { String text = expression.evaluate(exchange, String.class); if (text != null) { buffer.append(text); } } return buffer.toString(); } @Override public String toString() { if (desription != null) { return desription; } else { return "concat" + expressions; } } }; }
/** * Returns an Expression for the route id */ public static Expression routeIdExpression() { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { String answer = null; UnitOfWork uow = exchange.getUnitOfWork(); RouteContext rc = uow != null ? uow.getRouteContext() : null; if (rc != null) { answer = rc.getRoute().getId(); } if (answer == null) { // fallback and get from route id on the exchange answer = exchange.getFromRouteId(); } return answer; } @Override public String toString() { return "routeId"; } }; }
public static Expression simpleExpression(final String expression) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { if (SimpleLanguage.hasSimpleFunction(expression)) { // resolve language using context to have a clear separation of packages // must call evaluate to return the nested language evaluate when evaluating // stacked expressions Language language = exchange.getContext().resolveLanguage("simple"); return language.createExpression(expression).evaluate(exchange, Object.class); } else { return expression; } } @Override public String toString() { return "simple(" + expression + ")"; } }; }
public static Expression beanExpression(final String expression) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { // bean is able to evaluate method name if it contains nested functions // so we should not eager evaluate expression as a string // resolve language using context to have a clear separation of packages // must call evaluate to return the nested language evaluate when evaluating // stacked expressions Language language = exchange.getContext().resolveLanguage("bean"); return language.createExpression(expression).evaluate(exchange, Object.class); } @Override public String toString() { return "bean(" + expression + ")"; } }; }
public static Expression fileOnlyNameExpression() { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { String answer = exchange.getIn().getHeader(Exchange.FILE_NAME_ONLY, String.class); if (answer == null) { answer = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); answer = FileUtil.stripPath(answer); } return answer; } @Override public String toString() { return "file:onlyname"; } }; }
/** * Returns a random number between min and max */ public static Expression randomExpression(final String min, final String max) { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { int num1 = simpleExpression(min).evaluate(exchange, Integer.class); int num2 = simpleExpression(max).evaluate(exchange, Integer.class); Random random = new Random(); int randomNum = random.nextInt(num2 - num1) + num1; return randomNum; } @Override public String toString() { return "random"; } }; }
protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { Expression catFightCats = new ExpressionAdapter() { public Object evaluate(Exchange exchange) { CatFight catFight = (CatFight) exchange.getIn().getBody(); String[] cats = catFight.getCats(); return cats; } }; from("direct:custom").split(catFightCats).to("mock:result"); from("direct:xpath").split(xpath("/a/b")).to("mock:result"); } }; }
protected Expression createProducerExpression() { return new ExpressionAdapter() { public Object evaluate(Exchange exchange) { Object answer; // must have a body try { if (getEntityType() == null) { answer = exchange.getIn().getMandatoryBody(); } else { answer = exchange.getIn().getMandatoryBody(getEntityType()); } } catch (InvalidPayloadException e) { throw new InvalidPayloadRuntimeException(exchange, getEntityType(), e.getCause()); } // is never null return answer; } }; }
public static Expression terser(final String expression) { ObjectHelper.notNull(expression, "expression"); return new ExpressionAdapter() { @Override public Object evaluate(Exchange exchange) { Message message = exchange.getIn().getBody(Message.class); try { return new Terser(message).get(expression.trim()); } catch (HL7Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); } } @Override public String toString() { return "terser(" + expression + ")"; } }; }
/** * Creates an expression that gets the value of a header and if it is null * it evaluates a default expression * * @param name - header name * @param defaultExpression - default expression to evaluate if the header is null * @return an expression */ public static final Expression headerWithDefault(final String name, final Expression defaultExpression) { return new ValueBuilder(new ExpressionAdapter() { public Object evaluate(Exchange exchange) { Object header = exchange.getIn().getHeader(name); if(header == null) { return defaultExpression.evaluate(exchange,Object.class); } else { return header; } } @Override public String toString() { return "headerWithDefault(" + name + ", " + defaultExpression + ")"; } }); }