Subversion Repositories SmartDukaan

Rev

Rev 5264 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5262 phani.kuma 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.serving.model.ShipmentUpdate;
5
import in.shop2020.thrift.clients.config.ConfigClient;
6
 
18989 manish.sha 7
import java.io.BufferedReader;
8
import java.io.InputStreamReader;
9
import java.io.StringReader;
5262 phani.kuma 10
import java.util.ArrayList;
11
import java.util.List;
12
 
13
import javax.xml.xpath.XPath;
14
import javax.xml.xpath.XPathConstants;
15
import javax.xml.xpath.XPathExpressionException;
16
import javax.xml.xpath.XPathFactory;
17
 
18989 manish.sha 18
import org.apache.http.HttpResponse;
19
import org.apache.http.client.HttpClient;
20
import org.apache.http.client.methods.HttpGet;
21
import org.apache.http.impl.client.DefaultHttpClient;
5262 phani.kuma 22
import org.apache.log4j.Logger;
23
import org.w3c.dom.Element;
24
import org.w3c.dom.NodeList;
25
import org.xml.sax.InputSource;
26
 
27
public class DelhiveryTrackingService {
28
    //public static String Delhivery_URL = "http://track.delhivery.com/api/packages/xml/?token=bda066415b6b56698559e80b1a8345980882c90f&";
29
    private static Logger log = Logger.getLogger(Class.class);
30
 
31
    private static String Delhivery_URL;
32
 
33
    static{
34
        try {
35
        	Delhivery_URL = ConfigClient.getClient().get("Delhivery_update_url");
36
        } catch (ConfigException e) {
37
            log.error("Unable to get the tracking url from the config service", e);
38
        }
39
    }
40
 
41
    public List<ShipmentUpdate> getUpdates(String awbNumber){
18989 manish.sha 42
    	String uri = Delhivery_URL + "waybill=" + awbNumber;
43
    	log.info("Delhivery Update URL: " + uri);
44
    	try {
45
    		HttpClient client = new DefaultHttpClient();
5262 phani.kuma 46
 
18989 manish.sha 47
    		HttpGet get_new = new HttpGet(uri);
48
    		HttpResponse response = client.execute(get_new);
49
    		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
50
    		String line = "";
51
    		int i=1;
52
    		List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
53
    		while ((line = rd.readLine()) != null) {
54
    			System.out.println(line);
55
    			if(i==1){
56
    				i++;
57
    				continue;
58
    			}
59
    			InputSource inputSource = new InputSource(new StringReader(line));
60
    			XPath xpath = XPathFactory.newInstance().newXPath();
5262 phani.kuma 61
 
18989 manish.sha 62
    			String expression = "/ShipmentData/Shipment/Scans/ScanDetail";
63
    			NodeList nodes = null;
64
 
65
    			nodes = (NodeList) xpath.evaluate(expression, inputSource,  XPathConstants.NODESET);
66
 
67
 
68
    			System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
69
    			for(int j=0; j<=nodes.getLength()-1; j++) {
70
    				Element hawbUpdate = (Element) nodes.item(j);
71
    				ShipmentUpdate update = new ShipmentUpdate();
72
 
73
    				String dateString = getElementTextContent(hawbUpdate, "ScanDateTime");
74
    				String[] tokens = dateString.split("T");
75
    				update.date = tokens[0];
76
    				String[] timetokens = tokens[1].split("\\.");
77
    				update.time = timetokens[0];
78
    				update.city = getElementTextContent(hawbUpdate, "ScannedLocation");
79
    				update.description = getElementTextContent(hawbUpdate, "Scan") + " - " + getElementTextContent(hawbUpdate, "Instructions");         
80
    				updates.add(update);
81
    			}
82
 
83
    		}
84
    		return updates;
85
    	} catch(Exception xpee) {
86
    		xpee.printStackTrace();
87
    		return null;
88
    	}
5262 phani.kuma 89
    }
90
 
91
    private String getElementTextContent(Element element, String tagName){
92
        return element.getElementsByTagName(tagName).item(0).getTextContent();
93
    }
94
 
95
    public static void main(String[] args){
18989 manish.sha 96
        System.out.println("Delhivery number is " + "13410934216");
5262 phani.kuma 97
        DelhiveryTrackingService service = new DelhiveryTrackingService();
18989 manish.sha 98
        List<ShipmentUpdate> updates;
99
		try {
100
			updates = service.getUpdates("13410934216");
101
			for(ShipmentUpdate update: updates){
102
	            System.out.println(update.toString());
103
	        }
104
		} catch (Exception e) {
105
			e.printStackTrace();
106
		}
5262 phani.kuma 107
    }
108
}