Rev 5262 | Go to most recent revision | 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.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.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);InputSource inputSource = new InputSource(uri);XPath xpath = XPathFactory.newInstance().newXPath();String expression = "/ShipmentData/Shipment/Scans/ScanDetail";NodeList nodes = null;try {nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);} catch(XPathExpressionException xpee) {return null;}List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );for(int i=0; i<=nodes.getLength()-1; i++) {Element hawbUpdate = (Element) nodes.item(i);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;}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 " + "13410100004");DelhiveryTrackingService service = new DelhiveryTrackingService();List<ShipmentUpdate> updates = service.getUpdates("13410100004");for(ShipmentUpdate update: updates){System.out.println(update.toString());}}}