java - Apache camel simple http example -
i pretty new camel. have been trying fetch data http source. here's code:
from("timer://runonce?repeatcount=1") .to("http4://webservice.com/example.xml") .process(new structurexml()) .to("mock:resource") .stop(); and:
class structurexml implements processor { public void process(exchange httpexchange) throws exception { string httpres = httpexchange.getin().getbody(string.class); string[] lines = httpres.split("\n"); pattern p = pattern.compile("<map key='(.+)' value='(.+)'/>"); hashmap<string, integer> mapdata = new hashmap<string, integer>(); for(string line : lines) { matcher m = p.matcher(line); if(m.find()) mapdata.put(m.group(1), integer.parseint(m.group(2))); } httpexchange.getin().setbody(mapdata); } } well example works right want know possible ways further improve situation(e.g xml processing using xpath , etc), want know ways can store java object inside message can use in route(e.g: direct:resource instead of mock)
about java objects:
more information can found here: http://camel.apache.org/data-format.html
- jaxb
- xstream
- beanio
- jibx
- xmlbeans
these data formats useful transforming xml pojo. recomend try beanio (detailed documentation, many examples, etc).
about xpath:
it's hard tell specified without web-service response.
example:
setbody().xpath("/soap:envelope/soap:body/s:insertresponse/s:data", xmlnamespaces.getnamespace()). about example:
you need set lot of properties , header (before http request), worked fine. example:
setproperty(exchange.content_type).constant("application/soap+xml"). setproperty(exchange.content_encoding).constant("gzip"). setproperty(exchange.charset_name).constant("utf-8"). setheader(exchange.content_type).exchangeproperty(exchange.content_type). and don't see creating request web-service. easy of velocity (http://camel.apache.org/velocity.html), or, maybe, using soap date format (http://camel.apache.org/soap.html).
you can use jetty (http://camel.apache.org/jetty.html) instead of http4 (for me it's easier)
Comments
Post a Comment