private Processor divideByValueOf(final ValueBuilder valueBuilder) { return new Processor() { @Override public void process(Exchange exchange) throws Exception { Message in = exchange.getIn(); BigDecimal sum = new BigDecimal(checkNotNull( in.getBody(Number.class), "Body of %s is null", in) .toString()); BigDecimal divisor = new BigDecimal(checkNotNull( valueBuilder.evaluate(exchange, Integer.class), "No %s set in exchange %s", valueBuilder, exchange) .toString()); in.setBody(sum.divide(divisor, HALF_UP)); } }; }
/** * 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 + ")"; } }); }
private Expression createInExpression(final Expression leftExp, final Expression rightExp) { return new Expression() { @Override public <T> T evaluate(Exchange exchange, Class<T> type) { // okay the in operator is a bit more complex as we need to build a list of values // from the right hand side expression. // each element on the right hand side must be separated by comma (default for create iterator) Iterator<Object> it = ObjectHelper.createIterator(rightExp.evaluate(exchange, Object.class)); List<Object> values = new ArrayList<Object>(); while (it.hasNext()) { values.add(it.next()); } // then reuse value builder to create the in predicate with the list of values ValueBuilder vb = new ValueBuilder(leftExp); Predicate predicate = vb.in(values.toArray()); if (operator == BinaryOperatorType.NOT_IN) { predicate = PredicateBuilder.not(predicate); } boolean answer = predicate.matches(exchange); return exchange.getContext().getTypeConverter().convertTo(type, answer); } @Override public String toString() { return left + " " + token.getText() + " " + right; } }; }
public static ValueBuilder convertLFToCR() { return new ValueBuilder(new ExpressionAdapter() { @Override public Object evaluate(Exchange exchange) { String s = exchange.getIn().getBody(String.class); return s != null ? s.replace('\n', '\r') : null; } }); }
@Override protected ValueBuilder onNewValueBuilder(Expression exp) { return new PredicateValueBuilder(exp); }
/** * Returns a value builder for the given header */ public static ValueBuilder header(String name) { return Builder.header(name); }
/** * Returns a value builder for the given exchange property */ public static ValueBuilder exchangeProperty(String name) { return Builder.exchangeProperty(name); }
/** * Returns a predicate and value builder for the inbound body on an exchange */ public static ValueBuilder body() { return Builder.body(); }
/** * Returns a value builder for the given system property */ public static ValueBuilder systemProperty(String name) { return Builder.systemProperty(name); }
/** * Returns a value builder for the given system property */ public static ValueBuilder systemProperty(String name, String defaultValue) { return Builder.systemProperty(name, defaultValue); }
/** * Returns a value builder for the given property */ public static ValueBuilder property(String name) { return Builder.exchangeProperty(name); }
public static ValueBuilder terser(String expression) { return new ValueBuilder(new TerserExpression(expression)); }
public static ValueBuilder ack() { return new ValueBuilder(new AckExpression()); }
/** * @deprecated Use {@link #ack(ca.uhn.hl7v2.AcknowledgmentCode)} */ @Deprecated public static ValueBuilder ack(AckCode code) { return new ValueBuilder(new AckExpression(code)); }
public static ValueBuilder ack(AcknowledgmentCode code) { return new ValueBuilder(new AckExpression(code)); }
/** * @deprecated Use {@link #ack(ca.uhn.hl7v2.AcknowledgmentCode, String, ErrorCode)} */ @Deprecated public static ValueBuilder ack(AckCode code, String errorMessage, int errorCode) { return ack(code.toAcknowledgmentCode(), errorMessage, ErrorCode.errorCodeFor(errorCode)); }
/** * @deprecated Use {@link #ack(ca.uhn.hl7v2.AcknowledgmentCode, String, ErrorCode)} */ @Deprecated public static ValueBuilder ack(AckCode code, String errorMessage, ErrorCode errorCode) { return ack(code.toAcknowledgmentCode(), errorMessage, errorCode); }
public static ValueBuilder ack(AcknowledgmentCode code, String errorMessage, ErrorCode errorCode) { return new ValueBuilder(new AckExpression(code, errorMessage, errorCode)); }
/** * Returns a value builder for the given exchange property * * @deprecated use {@link #exchangeProperty(String)} */ @Deprecated public static ValueBuilder property(String name) { return Builder.exchangeProperty(name); }
/** * Returns a predicate and value builder for the inbound message body as a * specific type */ public static <T> ValueBuilder bodyAs(Class<T> type) { return Builder.bodyAs(type); }
/** * Returns a predicate and value builder for the outbound body on an * exchange * * @deprecated use {@link #body()} */ @Deprecated public static ValueBuilder outBody() { return Builder.outBody(); }
/** * Returns a predicate and value builder for the outbound message body as a * specific type * * @deprecated use {@link #bodyAs(Class)} */ @Deprecated public static <T> ValueBuilder outBodyAs(Class<T> type) { return Builder.outBodyAs(type); }
/** * Returns a predicate and value builder for the fault body on an * exchange */ public static ValueBuilder faultBody() { return Builder.faultBody(); }
/** * Returns a predicate and value builder for the fault message body as a * specific type * * @deprecated use {@link #bodyAs(Class)} */ @Deprecated public static <T> ValueBuilder faultBodyAs(Class<T> type) { return Builder.faultBodyAs(type); }