1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
|
4 |
import java.io.IOException; |
|
5 |
import java.net.URL; |
import java.net.URL; |
6 |
|
import java.sql.SQLException; |
7 |
import java.util.ArrayList; |
import java.util.ArrayList; |
8 |
import java.util.List; |
import java.util.List; |
9 |
|
import java.util.Map; |
10 |
|
import java.util.logging.Level; |
11 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
12 |
|
|
13 |
import com.gargoylesoftware.htmlunit.Page; |
import org.jsoup.nodes.Document; |
14 |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
import org.jsoup.nodes.Element; |
15 |
import com.gargoylesoftware.htmlunit.WebClient; |
import org.jsoup.select.Elements; |
16 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
|
17 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
18 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
19 |
|
import dk.thoerup.traininfoservice.StationDAO; |
20 |
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
import dk.thoerup.traininfoservice.Statistics; |
|
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
|
21 |
|
|
22 |
public class TimetableFetcher { |
public class TimetableFetcher { |
23 |
|
|
|
class NullRefreshHandler implements RefreshHandler { |
|
|
public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { |
|
|
} |
|
|
|
|
|
} |
|
24 |
|
|
25 |
TimeoutCache<String, List<TimetableBean>> cache = new TimeoutCache<String,List<TimetableBean>>(120 * 1000); |
Map<String, List<TimetableBean>> cache; |
26 |
|
Map<String, Integer> stationCache; |
27 |
|
|
28 |
|
StationDAO stationDao = new StationDAO(); |
29 |
|
|
30 |
|
|
31 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
32 |
|
|
33 |
|
private boolean useTempSite; |
34 |
|
|
35 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Throwable { |
public TimetableFetcher(boolean tmpSite, int cacheTimeout) { |
36 |
|
useTempSite = tmpSite; |
37 |
|
|
38 |
|
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
39 |
|
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
40 |
|
} |
41 |
|
|
42 |
|
|
43 |
|
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
44 |
String key = trainID+type; |
String key = trainID+type; |
45 |
List<TimetableBean> list = cache.get(key); |
List<TimetableBean> list = cache.get(key); |
46 |
|
|
48 |
list = lookupTimetable(trainID,type); |
list = lookupTimetable(trainID,type); |
49 |
cache.put(key, list); |
cache.put(key, list); |
50 |
} else { |
} else { |
51 |
|
Statistics.getInstance().incrementTimetableCacheHits(); |
52 |
logger.info("Timetable: Cache hit " + trainID); |
logger.info("Timetable: Cache hit " + trainID); |
53 |
} |
} |
54 |
return list; |
return list; |
55 |
} |
} |
56 |
|
|
57 |
|
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
58 |
|
if (useTempSite == false ){ |
59 |
|
return lookupTimetableRealSite(trainID, type); |
60 |
|
} else { |
61 |
|
return new ArrayList<TimetableBean>(); // no timetable data on temp site |
62 |
|
} |
63 |
|
} |
64 |
|
|
65 |
|
int getStationId(String name) { |
66 |
|
Integer id = stationCache.get(name); |
67 |
|
|
68 |
|
if (id == null) { |
69 |
|
try { |
70 |
|
id = stationDao.getIdByName(name); |
71 |
|
stationCache.put(name, id); |
72 |
|
} catch (SQLException e) { |
73 |
|
logger.log(Level.SEVERE, "getStationId failed", e); |
74 |
|
id = -1; |
75 |
|
} |
76 |
|
} |
77 |
|
|
78 |
|
return id; |
79 |
|
} |
80 |
|
|
81 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Throwable { |
List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception { |
82 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
83 |
|
|
84 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
//String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
85 |
|
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
86 |
|
|
|
final WebClient webClient = new WebClient(); |
|
|
webClient.setTimeout(2500); |
|
|
webClient.setJavaScriptEnabled(false); |
|
|
webClient.setRefreshHandler( new NullRefreshHandler() ); |
|
|
webClient.setCssEnabled(false); |
|
87 |
|
|
88 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , 2500); |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, url); |
|
89 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
90 |
|
|
91 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
92 |
|
|
93 |
|
|
94 |
boolean currentStation = false; |
boolean currentStation = false; |
95 |
boolean currentStationSaved = false; |
boolean currentStationSaved = false; |
96 |
|
|
97 |
List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); |
Elements tables = doc.getElementsByClass("Rute"); |
98 |
|
|
99 |
if (tables.size() == 1) { |
if (tables.size() == 1) { |
100 |
HtmlElement timetable = tables.get(0); |
Element timetable = tables.get(0); |
101 |
DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr"); |
Elements rows = timetable.getElementsByTag("tr"); |
102 |
|
|
103 |
for (int i=0; i<rows.size(); i++) { |
for (int i=0; i<rows.size(); i++) { |
104 |
if (i==0) //First row is column headers |
if (i==0) //First row is column headers |
105 |
continue; |
continue; |
106 |
|
|
107 |
|
|
108 |
HtmlElement row = rows.get(i); |
Element row = rows.get(i); |
109 |
DomNodeList<HtmlElement> fields = row.getElementsByTagName("td"); |
Elements fields = row.getElementsByTag("td"); |
110 |
|
|
111 |
|
|
112 |
if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { |
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
113 |
currentStation = true; |
currentStation = true; |
114 |
continue; |
continue; |
115 |
} |
} |
116 |
|
|
117 |
TimetableBean bean = new TimetableBean(); |
TimetableBean bean = new TimetableBean(); |
118 |
bean.setStation( fields.get(0).asText() ); |
|
119 |
bean.setArrival( fields.get(1).asText() ); |
String station = fields.get(0).text() ; |
120 |
bean.setDeparture( fields.get(2).asText() ); |
if (station.equals("København")) |
121 |
|
station = "København H"; //correct inconsistency in naming |
122 |
|
|
123 |
|
bean.setStation( station ); |
124 |
|
bean.setArrival( fields.get(1).text() ); |
125 |
|
bean.setDeparture( fields.get(2).text() ); |
126 |
|
|
127 |
|
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
128 |
|
bean.setCancelled(cancelled); |
129 |
|
|
130 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
131 |
bean.setCurrent(currentStation); |
bean.setCurrent(currentStation); |
132 |
currentStationSaved = true; |
currentStationSaved = true; |
133 |
} |
} |
134 |
|
|
135 |
|
bean.setStationId( getStationId( station )); |
136 |
|
|
137 |
timetableList.add(bean); |
timetableList.add(bean); |
138 |
|
} |
139 |
|
|
140 |
|
//TODO: There is an off-by-one error in this cancelled parser thingie |
141 |
|
final String cancelledString = "Aflyst"; |
142 |
|
for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels |
143 |
|
final int lastIdx = (timetableList.size() - 1); |
144 |
|
|
145 |
|
TimetableBean current = timetableList.get(i); |
146 |
|
if (current.isCancelled()) { |
147 |
|
if (i == 0) { |
148 |
|
current.setDeparture(cancelledString); |
149 |
|
} else if (i == lastIdx) { |
150 |
|
current.setArrival(cancelledString); |
151 |
|
} else if (i>0 && i<lastIdx) { |
152 |
|
TimetableBean next = timetableList.get(i+1); |
153 |
|
TimetableBean prev = timetableList.get(i-1); |
154 |
|
|
155 |
|
if (next.isCancelled()) |
156 |
|
current.setDeparture(cancelledString); |
157 |
|
if (prev.isCancelled()) |
158 |
|
current.setArrival(cancelledString); |
159 |
|
} |
160 |
|
} |
161 |
} |
} |
162 |
|
|
163 |
} else { |
} else { |
164 |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
165 |
} |
} |
166 |
|
|
167 |
|
|
168 |
return timetableList; |
return timetableList; |
169 |
} |
} |