Search in sources :

Example 11 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class NeilSplitterTest method createRouteBuilder.

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");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) Expression(org.apache.camel.Expression) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 12 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class InterceptFromDefinition method createProcessor.

@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);
}
Also used : Exchange(org.apache.camel.Exchange) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 13 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class ExpressionBuilder method toExpression.

/**
     * Returns an expression processing the exchange to the given endpoint uri
     *
     * @param uri endpoint uri to send the exchange to
     * @return an expression object which will return the OUT body
     */
public static Expression toExpression(final String uri) {
    return new ExpressionAdapter() {

        public Object evaluate(Exchange exchange) {
            String text = simpleExpression(uri).evaluate(exchange, String.class);
            Endpoint endpoint = exchange.getContext().getEndpoint(text);
            if (endpoint == null) {
                throw new NoSuchEndpointException(text);
            }
            Producer producer;
            try {
                producer = endpoint.createProducer();
                producer.start();
                producer.process(exchange);
                producer.stop();
            } catch (Exception e) {
                throw ObjectHelper.wrapRuntimeCamelException(e);
            }
            // return the OUT body, but check for exchange pattern
            if (ExchangeHelper.isOutCapable(exchange)) {
                return exchange.getOut().getBody();
            } else {
                return exchange.getIn().getBody();
            }
        }

        @Override
        public String toString() {
            return "to(" + uri + ")";
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoSuchLanguageException(org.apache.camel.NoSuchLanguageException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException)

Example 14 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class ProcessorDefinition method setHeader.

/**
     * Adds a processor which sets the header on the IN message
     *
     * @param name  the header name
     * @param supplier the supplier used to set the header
     * @return the builder
     */
@SuppressWarnings("unchecked")
public Type setHeader(String name, final Supplier<Object> supplier) {
    SetHeaderDefinition answer = new SetHeaderDefinition(name, new ExpressionAdapter() {

        @Override
        public Object evaluate(Exchange exchange) {
            return supplier.get();
        }
    });
    addOutput(answer);
    return (Type) this;
}
Also used : Exchange(org.apache.camel.Exchange) XmlAccessorType(javax.xml.bind.annotation.XmlAccessorType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Example 15 with ExpressionAdapter

use of org.apache.camel.support.ExpressionAdapter in project camel by apache.

the class SetHeaderUsingDslExpressionsTest method testUseExpression.

public void testUseExpression() throws Exception {
    context.addRoutes(new RouteBuilder() {

        public void configure() throws Exception {
            from("direct:start").setHeader("foo").expression(new ExpressionAdapter() {

                public Object evaluate(Exchange exchange) {
                    return "ABC";
                }
            }).to("mock:result");
        }
    });
    template.sendBodyAndHeader("direct:start", body, "bar", "ABC");
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) RouteBuilder(org.apache.camel.builder.RouteBuilder) ExpressionAdapter(org.apache.camel.support.ExpressionAdapter)

Aggregations

Exchange (org.apache.camel.Exchange)21 ExpressionAdapter (org.apache.camel.support.ExpressionAdapter)21 InvalidPayloadException (org.apache.camel.InvalidPayloadException)5 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)5 Endpoint (org.apache.camel.Endpoint)4 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)4 NoSuchLanguageException (org.apache.camel.NoSuchLanguageException)4 Random (java.util.Random)2 Scanner (java.util.Scanner)2 Pattern (java.util.regex.Pattern)2 Expression (org.apache.camel.Expression)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 MethodCallExpression (org.apache.camel.model.language.MethodCallExpression)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Matcher (java.util.regex.Matcher)1