You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by vjboston <vj...@gmail.com> on 2013/09/30 19:49:12 UTC
Invoking remote RESTful services urgent !!
Hi ,
Can somebody share a sample code for invoking an remote restful service.
I tried it with following code
start **
package org.apache.camel.example.jmstofile;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.cxf.CxfConstants;
import org.apache.camel.component.yammer.model.Message;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
public class TestRest
{
protected CamelContext container = new DefaultCamelContext();
private PrepareRestRequest processor;
private Exchange exchange;
public static void main(String[] args)
{
// TODO Auto-generated method stub
try
{
TestRest restobj = new TestRest();
// System.out.println("before set up");
restobj.setUp();
//Thread.sleep(10000);
//restobj.tearDown();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
protected void tearDown() throws Exception {
container.stop();
}
protected void setUp() throws Exception {
//container.addRoutes(createRouteBuilder());
//System.out.println("after set up");
//container.start();
processor = new PrepareRestRequest();
exchange = new DefaultExchange(new DefaultCamelContext());
processor.process(exchange);
System.out.println(exchange.isFailed());
}
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
from("direct:start").to("http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/").process(new
Processor() {
public void process(Exchange e)
{
//String in = e.getIn().getBody(String.class);
// append newline at end to denote end of data for
textline codec
//e.getOut().setBody("Hello " + in + "\n");
System.out.println("we are here" +
e.getExchangeId());
}
});
}
};
}
@SuppressWarnings("deprecation")
private void process()
{
CamelContext context = new DefaultCamelContext();
ProducerTemplate template = context.createProducerTemplate();
Exchange exchange = template.send("direct://http", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = (Message)exchange.getIn();
//setupDestinationURL(inMessage);
// using the http central client API
((org.apache.camel.Message)
inMessage).setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
Boolean.TRUE);
// set the Http method
((org.apache.camel.Message)
inMessage).setHeader(Exchange.HTTP_METHOD, "GET");
// set the relative path
((org.apache.camel.Message)
inMessage).setHeader(Exchange.HTTP_URI,
"http://www.thomas-bayer.com/sqlrest/CUSTOMER/18/");
// Specify the response class , cxfrs will use InputStream as
the response object type
// ((org.apache.camel.Message)
inMessage).setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS,
String.class);
// set a customer header
//((org.apache.camel.Message) inMessage).setHeader("key",
"value");
// since we use the Get method, so we don't need to set the
message body
//inMessage.setBody(null);
}
});
// System.out.println(exchange.);
}
public void process1()
{
CamelContext context = new DefaultCamelContext();
ProducerTemplate template = context.createProducerTemplate();
Exchange exchange = template.send("direct://proxy", new Processor() {
public void process(Exchange exchange) throws Exception {
String dto = exchange.getIn().getBody(String.class);
exchange.getIn().setBody(null);
exchange.getIn().getHeaders().clear();
exchange.getIn().setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
Boolean.TRUE);
exchange.getIn().setHeader(Exchange.HTTP_METHOD, "POST");
exchange.getIn().setHeader(Exchange.ACCEPT_CONTENT_TYPE,
"application/json");
exchange.getIn().setHeader(Exchange.HTTP_PATH,
String.format("thomas-bayer.com/sqlrest/CUSTOMER/18/", dto));
exchange.getIn().setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS,
String.class);
System.out.println(dto);
}
});
}
}
end **
It keeps giving me body as null..How do I retrieve the response of the
restful web service ?
--
View this message in context: http://camel.465427.n5.nabble.com/Invoking-remote-RESTful-services-urgent-tp5740585.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Invoking remote RESTful services urgent !!
Posted by Tracy Snell <ts...@gmail.com>.
Here's code I use to expose a REST url externally to an internal REST service.
from(BASE_URI + "/doctor?bindingStyle=SimpleConsumer&resourceClasses=" + DoctorLookupService.class.getName())
.setHeader(Exchange.HTTP_URI,
simple("http://my.company.com/CT/GetContact.svc/npi/${header.npi}"))
.setHeader("Authorization", simple("Basic foobarred="))
.setHeader("CamelHttpPath", simple(""))
.to("http://igetoverridden.com?throwExceptionOnFailure=false");
Just use the http(4) component and set the appropriate URL. In my case it's dynamic so I stuff it in the header and that overrides the one in the .to
On Sep 30, 2013, at 1:49 PM, vjboston <vj...@gmail.com> wrote:
>
> Can somebody share a sample code for invoking an remote restful service.
> I tried it with following code