Surasin Tancharoen Surasin Tancharoen - 2 months ago 17
Java Question

Camel: how to check response http response

I am pretty new with Camel. I have been trying to submit a data (Json from a file) to a webservice. This is my code:

public static void main(String args[]) throws Exception {
// create CamelContext
CamelContext context = new DefaultCamelContext();

// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("file:data/inbox?noop=true")
.marshal()
.string()
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.to("http://www.a-service.com");
}
});

// start the route and let it do its work
context.start();
Thread.sleep(10000);

// stop the CamelContext
context.stop();
}


Then the webservice will response with Json which can be
{result:OK}
or
{result:FAIL}

Now, if a response has responseCode as 200, Camel will consider as success.

My question is, how can I have a validating process for responsed JSon so that if it is FAIL, Camel should not consider as success?

Solution Credit @Namphibian:

By adding processor and the end. This code has been tested:

from("file:data/inbox?noop=true")
.marshal()
.string("UTF-8")
.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
.to("http://monrif-test.userspike.com/monrif/rss/monrif_-all-global")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
String msg = in.getBody(String.class);
System.out.println("Response: " + msg);
if(msg.contains("OK")){
// go to party
}else{
throw new Exception("test exception");
}
}
});

Answer

There are two broad strategies you can use to achieve this.

Processor Based:

Add a processor to the end of the route. In this processor do the check if the webservice then responds with a true or false value.

A processor would look something like this:

    package com.example;

    import java.util.Map;

    import org.apache.camel.Body;
    import org.apache.camel.Exchange;
    import org.apache.camel.Handler;
    import org.apache.camel.Headers;
    import org.apache.camel.Message;

    public class GeneralProcessor {
        @Handler

        public void PrepapreErrorImportReport
        (

                @Headers Map hdr
                , Exchange exch
        )
        {

            //todo: Get the message as a string;

            Message in = exch.getIn();
            String msg = in.getBody(String.class);
            // Now check if body contains failed or ok.
            if(msg.contains("OK")){
                //todo: go party the message was OK
            }
            else{
               //todo: Oh Oh! Houston we have a problem
            }


        }

    }

You can then modify your route to use this processor.

The Simple Expression Language

This is one way the other way is to use the simple expression language. See the example below on how to use this.

 from("file:data/inbox?noop=true")
                .marshal()
                .string()
                .setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
                .to("http://www.a-service.com")
                .choice()
                .when(simple("${body} contains 'OK'")).to("activemq:okqueue")
                .otherwise().to("activemq:queue:other");

Notice the simple("${body} contains 'OK'") piece of code. That is the power of simple.

Both approaches have uses.

Comments