camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "praneeth.purighalla" <praneeth.b...@gmail.com>
Subject Using cxfrs producer with a dynamic endpoint from a metadata file
Date Mon, 15 Jun 2015 03:35:28 GMT
Hi All,

I'm completely new to Camel. So please bear with this requirement of mine.
I have a file that contains some metadata in XML. This metadata includes the
URI of my external HTTP service along with authentication details to connect
to the endpoint. I'm trying to build my camel route in such a way that I
read the metadata file, retrieve all the relevant details like URI,
username, password, etc and then invoke the URI.

This is my routing code:

--------------------------------------------------START
from("file:src/data?fileName=metadata.xml&noop=true")
    	.setProperty("username", xpath("/endpoint/authentication/username",
String.class))
    	.setProperty("password", xpath("/endpoint/authentication/password",
String.class))
    	.setProperty("uri", xpath("/endpoint/uri", String.class))
    	.process(new Processor() {

			@Override
			public void process(Exchange exchange) throws Exception {
				// set exchange pattern as InOut
				exchange.setPattern(ExchangePattern.InOut);
				Message inMessage = exchange.getIn();
				
				// using the http central client API
		        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API,
Boolean.TRUE);
		        
		        // set the Http method
		        inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
		        
		        // set authorization header
		        String username = exchange.getProperty("username", String.class);
		        String password = exchange.getProperty("password", String.class);
		        inMessage.setHeader("Authorization", "Basic " +
base64Encode(username + ":" + password));
		        exchange.getIn().setBody("");
		        CxfRsEndpoint myCxfEp = (CxfRsEndpoint)
getContext().getEndpoint(exchange.getProperty("uri", String.class));

		        ProducerTemplate template = getContext().createProducerTemplate();
		        Exchange response = template.send(myCxfEp, exchange);
				exchange.getIn().setBody(response.getIn().getBody());		        
			}

			private String base64Encode(String input) {
				return Base64Utility.encode(input.getBytes());
			}
    		
    	})
    	.to("file:src/data/outbox");

--------------------------------------------------------END

I want to capture the response from the external HTTP service into a file.
When I sniff the request using TCPMON, I can see the expected request and
response. However, the exchange object that gets written to the outbox in my
final route node is still the metadata file in my "from" endpoint.

Can someone help me with this? Am I doing something wrong or is there a
better/easier way to achieve this?

Here is a snippet of my metadata file:

-------------------------START
<endpoint>
    <uri>cxfrs://http://localhost:1111/test</ur>
    <authentication>
        <username>foo</username>
        <password>bar</password>
    </authentication>
</endpoint>
-------------------------END

Thanks in advance,
Praneeth



--
View this message in context: http://camel.465427.n5.nabble.com/Using-cxfrs-producer-with-a-dynamic-endpoint-from-a-metadata-file-tp5768224.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
View raw message