Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.synclio.hawk; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.util.Iterator; import javax.xml.namespace.NamespaceContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.gae.http.GHttpException; import org.apache.camel.component.gae.mail.GMailBinding; import org.apache.camel.processor.aggregate.AggregationStrategy; import org.apache.commons.io.IOUtils; import com.google.appengine.api.urlfetch.HTTPMethod; import com.google.appengine.api.urlfetch.HTTPRequest; import com.google.appengine.api.urlfetch.HTTPResponse; import com.google.appengine.api.urlfetch.URLFetchService; import com.google.appengine.api.urlfetch.URLFetchServiceFactory; public class TutorialRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { from("ghttp:///weather").process(new RequestProcessor()).marshal().serialization().to("gtask://default") .unmarshal().serialization().process(new ResponseProcessor()); from("gtask://default").unmarshal().serialization().process(new Processor() { @Override public void process(Exchange exchange) throws Exception { ReportData data = exchange.getIn().getBody(ReportData.class); URLFetchService urlFetch = URLFetchServiceFactory.getURLFetchService(); HTTPRequest req = new HTTPRequest(new URL("http://where.yahooapis.com/v1/places.q(" + data.getCity() + ")?appid=0b4twNzV34GzaTHevW1SzEcnzG4Nyuhq6pvnSKZS.28kCqUw31jPRApe6_niElSL_waLc2M-"), HTTPMethod.GET); HTTPResponse resp = urlFetch.fetch(req); Document doc = toDocument(IOUtils.toInputStream(new String(resp.getContent()))); XPathFactory xpfactory = XPathFactory.newInstance(); XPath xpath = xpfactory.newXPath(); try { System.out.print(xpath.evaluate("//places/place[1]/woeid/text()", doc)); System.out.print(new String(resp.getContent())); } catch (Exception e) { e.printStackTrace(); } } }); // .setHeader(Exchange.HTTP_METHOD,constant("GET")) // .enrich("ghttp://where.yahooapis.com/v1/places.q(Chennai)?appid=0b4twNzV34GzaTHevW1SzEcnzG4Nyuhq6pvnSKZS.28kCqUw31jPRApe6_niElSL_waLc2M-") .setHeader(GMailBinding.GMAIL_SUBJECT, constant("Weather report")) // .setHeader(GMailBinding.GMAIL_SENDER, ReportData.requestor()) // .setHeader(GMailBinding.GMAIL_TO, ReportData.recipient()) // .process(new ReportGenerator()) // .to("gmail://default"); } private static AggregationStrategy reportDataAggregator() { return new AggregationStrategy() { public Exchange aggregate(Exchange reportExchange, Exchange weatherExchange) { ReportData reportData = reportExchange.getIn().getBody(ReportData.class); try { System.out.print(IOUtils.toString(weatherExchange.getIn().getBody(InputStream.class))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } reportData.setWeather(toDocument(weatherExchange.getIn().getBody(InputStream.class))); return reportExchange; } }; } // As GAE have trouble to load the javax.xml.transform.stax.StAXSource which is used the XmlConverter // Now we just do the transformation ourself. private static DocumentBuilderFactory createDocumentBuilderFactory() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true); return factory; } private static Document toDocument(InputStream stream) { DocumentBuilderFactory factory = createDocumentBuilderFactory(); try { Document doc = factory.newDocumentBuilder().parse(stream); return doc; } catch (Exception ex) { throw new RuntimeCamelException(ex); } } }