Rev 5264 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.services;import in.shop2020.config.ConfigException;import in.shop2020.serving.model.ShipmentUpdate;import in.shop2020.thrift.clients.config.ConfigClient;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.StringReader;import java.util.ArrayList;import java.util.List;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.log4j.Logger;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;public class DelhiveryTrackingService {//public static String Delhivery_URL = "http://track.delhivery.com/api/packages/xml/?token=bda066415b6b56698559e80b1a8345980882c90f&";private static Logger log = Logger.getLogger(Class.class);private static String Delhivery_URL;static{try {Delhivery_URL = ConfigClient.getClient().get("Delhivery_update_url");} catch (ConfigException e) {log.error("Unable to get the tracking url from the config service", e);}}public List<ShipmentUpdate> getUpdates(String awbNumber){String uri = Delhivery_URL + "waybill=" + awbNumber;log.info("Delhivery Update URL: " + uri);try {HttpClient client = new DefaultHttpClient();HttpGet get_new = new HttpGet(uri);HttpResponse response = client.execute(get_new);BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line = "";int i=1;List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();while ((line = rd.readLine()) != null) {System.out.println(line);if(i==1){i++;continue;}InputSource inputSource = new InputSource(new StringReader(line));XPath xpath = XPathFactory.newInstance().newXPath();String expression = "/ShipmentData/Shipment/Scans/ScanDetail";NodeList nodes = null;nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );for(int j=0; j<=nodes.getLength()-1; j++) {Element hawbUpdate = (Element) nodes.item(j);ShipmentUpdate update = new ShipmentUpdate();String dateString = getElementTextContent(hawbUpdate, "ScanDateTime");String[] tokens = dateString.split("T");update.date = tokens[0];String[] timetokens = tokens[1].split("\\.");update.time = timetokens[0];update.city = getElementTextContent(hawbUpdate, "ScannedLocation");update.description = getElementTextContent(hawbUpdate, "Scan") + " - " + getElementTextContent(hawbUpdate, "Instructions");updates.add(update);}}return updates;} catch(Exception xpee) {xpee.printStackTrace();return null;}}private String getElementTextContent(Element element, String tagName){return element.getElementsByTagName(tagName).item(0).getTextContent();}public static void main(String[] args){System.out.println("Delhivery number is " + "13410934216");DelhiveryTrackingService service = new DelhiveryTrackingService();List<ShipmentUpdate> updates;try {updates = service.getUpdates("13410934216");for(ShipmentUpdate update: updates){System.out.println(update.toString());}} catch (Exception e) {e.printStackTrace();}}}