1 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
3 |
|
4 |
import java.io.IOException; |
5 |
import java.net.URL; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
import java.util.Map; |
9 |
import java.util.logging.Logger; |
10 |
|
11 |
import com.gargoylesoftware.htmlunit.BrowserVersion; |
12 |
import com.gargoylesoftware.htmlunit.Page; |
13 |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
14 |
import com.gargoylesoftware.htmlunit.WebClient; |
15 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
16 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
17 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
18 |
|
19 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
21 |
import dk.thoerup.traininfoservice.Statistics; |
22 |
|
23 |
public class TimetableFetcher { |
24 |
|
25 |
class NullRefreshHandler implements RefreshHandler { |
26 |
public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { |
27 |
} |
28 |
|
29 |
} |
30 |
|
31 |
Map<String, List<TimetableBean>> cache; |
32 |
|
33 |
|
34 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
35 |
|
36 |
private boolean useTempSite; |
37 |
|
38 |
public TimetableFetcher(boolean tmpSite, int cacheTimeout) { |
39 |
useTempSite = tmpSite; |
40 |
|
41 |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
42 |
} |
43 |
|
44 |
|
45 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
46 |
String key = trainID+type; |
47 |
List<TimetableBean> list = cache.get(key); |
48 |
|
49 |
if (list == null) { |
50 |
list = lookupTimetable(trainID,type); |
51 |
cache.put(key, list); |
52 |
} else { |
53 |
Statistics.getInstance().incrementTimetableCacheHits(); |
54 |
logger.info("Timetable: Cache hit " + trainID); |
55 |
} |
56 |
return list; |
57 |
} |
58 |
|
59 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
60 |
if (useTempSite == false ){ |
61 |
return lookupTimetableRealSite(trainID, type); |
62 |
} else { |
63 |
return new ArrayList<TimetableBean>(); // no timetable data on temp site |
64 |
} |
65 |
} |
66 |
|
67 |
List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception { |
68 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
69 |
|
70 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
71 |
|
72 |
|
73 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
74 |
webClient.setTimeout(2500); |
75 |
webClient.setJavaScriptEnabled(false); |
76 |
webClient.setRefreshHandler( new NullRefreshHandler() ); |
77 |
webClient.setCssEnabled(false); |
78 |
|
79 |
|
80 |
BanedkInvocation wrapper = new BanedkInvocation(webClient, url); |
81 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
82 |
|
83 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
84 |
|
85 |
|
86 |
boolean currentStation = false; |
87 |
boolean currentStationSaved = false; |
88 |
|
89 |
List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); |
90 |
if (tables.size() == 1) { |
91 |
HtmlElement timetable = tables.get(0); |
92 |
DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr"); |
93 |
|
94 |
for (int i=0; i<rows.size(); i++) { |
95 |
if (i==0) //First row is column headers |
96 |
continue; |
97 |
|
98 |
|
99 |
HtmlElement row = rows.get(i); |
100 |
DomNodeList<HtmlElement> fields = row.getElementsByTagName("td"); |
101 |
|
102 |
if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { |
103 |
currentStation = true; |
104 |
continue; |
105 |
} |
106 |
|
107 |
TimetableBean bean = new TimetableBean(); |
108 |
bean.setStation( fields.get(0).asText() ); |
109 |
bean.setArrival( fields.get(1).asText() ); |
110 |
bean.setDeparture( fields.get(2).asText() ); |
111 |
|
112 |
if (currentStation == true && currentStationSaved == false ) { |
113 |
bean.setCurrent(currentStation); |
114 |
currentStationSaved = true; |
115 |
} |
116 |
|
117 |
timetableList.add(bean); |
118 |
|
119 |
} |
120 |
|
121 |
} else { |
122 |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
123 |
} |
124 |
webClient.closeAllWindows(); |
125 |
|
126 |
return timetableList; |
127 |
} |
128 |
|
129 |
} |