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.util.ArrayList; |
import java.sql.SQLException; |
|
import java.util.List; |
|
7 |
import java.util.Map; |
import java.util.Map; |
8 |
|
import java.util.logging.Level; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
11 |
import com.gargoylesoftware.htmlunit.BrowserVersion; |
import org.jsoup.nodes.Document; |
12 |
import com.gargoylesoftware.htmlunit.Page; |
import org.jsoup.nodes.Element; |
13 |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
import org.jsoup.select.Elements; |
|
import com.gargoylesoftware.htmlunit.WebClient; |
|
|
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
|
14 |
|
|
15 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
16 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
17 |
|
import dk.thoerup.traininfoservice.StationDAO; |
18 |
|
import dk.thoerup.traininfoservice.Statistics; |
19 |
|
|
20 |
public class TimetableFetcher { |
public class TimetableFetcher { |
21 |
|
|
|
class NullRefreshHandler implements RefreshHandler { |
|
|
public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { |
|
|
} |
|
|
|
|
|
} |
|
22 |
|
|
23 |
Map<String, List<TimetableBean>> cache; |
Map<String, TimetableBean> cache; |
24 |
|
Map<String, Integer> stationCache; |
25 |
|
|
26 |
|
StationDAO stationDao = new StationDAO(); |
27 |
|
|
28 |
|
|
29 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
30 |
|
|
31 |
private boolean useTempSite; |
private boolean useAzureSite; |
32 |
|
private int replyTimeout; |
33 |
|
|
34 |
public TimetableFetcher(boolean tmpSite, int cacheTimeout) { |
public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
35 |
useTempSite = tmpSite; |
useAzureSite = azureSite; |
36 |
|
this.replyTimeout = replyTimeout; |
37 |
|
|
38 |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
cache = new TimeoutMap<String,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 { |
TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { |
44 |
String key = trainID+type; |
String key = trainID+type; |
45 |
List<TimetableBean> list = cache.get(key); |
TimetableBean list = cache.get(key); |
46 |
|
|
47 |
if (list == null) { |
if (list == null) { |
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 { |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
58 |
if (useTempSite == false ){ |
if (useAzureSite == true ){ |
59 |
return lookupTimetableRealSite(trainID, type); |
return lookupTimetableAzureSite(trainID, type); |
60 |
|
|
61 |
} else { |
} else { |
62 |
return new ArrayList<TimetableBean>(); // no timetable data on temp site |
return lookupTimetableWwwSite(trainID, type); |
63 |
} |
} |
64 |
} |
} |
65 |
|
|
66 |
|
int getStationId(String name) { |
67 |
|
Integer id = stationCache.get(name); |
68 |
|
|
69 |
|
if (id == null) { |
70 |
|
try { |
71 |
|
id = stationDao.getIdByName(name); |
72 |
|
stationCache.put(name, id); |
73 |
|
} catch (SQLException e) { |
74 |
|
logger.log(Level.SEVERE, "getStationId failed", e); |
75 |
|
id = -1; |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
|
return id; |
80 |
|
} |
81 |
|
|
82 |
List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
83 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
84 |
|
|
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
|
|
|
|
85 |
|
|
86 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
87 |
webClient.setTimeout(2500); |
logger.fine("URL:" + url); |
|
webClient.setJavaScriptEnabled(false); |
|
|
webClient.setRefreshHandler( new NullRefreshHandler() ); |
|
|
webClient.setCssEnabled(false); |
|
88 |
|
|
89 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, url); |
|
90 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
91 |
|
|
92 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
93 |
|
|
94 |
|
|
95 |
boolean currentStation = false; |
boolean currentStation = false; |
96 |
boolean currentStationSaved = false; |
boolean currentStationSaved = false; |
97 |
|
|
98 |
List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); |
Elements tables = doc.getElementsByClass("Rute"); |
99 |
|
|
100 |
if (tables.size() == 1) { |
if (tables.size() == 1) { |
101 |
HtmlElement timetable = tables.get(0); |
Element timetable = tables.get(0); |
102 |
DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr"); |
Elements rows = timetable.getElementsByTag("tr"); |
103 |
|
|
104 |
for (int i=0; i<rows.size(); i++) { |
for (int i=0; i<rows.size(); i++) { |
105 |
if (i==0) //First row is column headers |
if (i==0) //First row is column headers |
106 |
continue; |
continue; |
107 |
|
|
108 |
|
|
109 |
HtmlElement row = rows.get(i); |
Element row = rows.get(i); |
110 |
DomNodeList<HtmlElement> fields = row.getElementsByTagName("td"); |
Elements fields = row.getElementsByTag("td"); |
111 |
|
|
112 |
|
|
113 |
if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { |
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
114 |
currentStation = true; |
currentStation = true; |
115 |
continue; |
continue; |
116 |
} |
} |
117 |
|
|
118 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
119 |
bean.setStation( fields.get(0).asText() ); |
|
120 |
bean.setArrival( fields.get(1).asText() ); |
String station = fields.get(0).text() ; |
121 |
bean.setDeparture( fields.get(2).asText() ); |
if (station.equals("København")) |
122 |
|
station = "København H"; //correct inconsistency in naming |
123 |
|
|
124 |
|
entry.setStation( station ); |
125 |
|
entry.setArrival( fields.get(1).text() ); |
126 |
|
entry.setDeparture( fields.get(2).text() ); |
127 |
|
|
128 |
|
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
129 |
|
entry.setCancelled(cancelled); |
130 |
|
|
131 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
132 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
133 |
currentStationSaved = true; |
currentStationSaved = true; |
134 |
} |
} |
135 |
|
|
136 |
timetableList.add(bean); |
entry.setStationId( getStationId( station )); |
137 |
|
|
138 |
|
timetableBean.entries.add(entry); |
139 |
|
} |
140 |
|
|
141 |
|
//TODO: There is an off-by-one error in this cancelled parser thingie |
142 |
|
final String cancelledString = "Aflyst"; |
143 |
|
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
144 |
|
final int lastIdx = (timetableBean.entries.size() - 1); |
145 |
|
|
146 |
|
TimetableEntry current = timetableBean.entries.get(i); |
147 |
|
if (current.isCancelled()) { |
148 |
|
if (i == 0) { |
149 |
|
current.setDeparture(cancelledString); |
150 |
|
} else if (i == lastIdx) { |
151 |
|
current.setArrival(cancelledString); |
152 |
|
} else if (i>0 && i<lastIdx) { |
153 |
|
TimetableEntry next = timetableBean.entries.get(i+1); |
154 |
|
TimetableEntry prev = timetableBean.entries.get(i-1); |
155 |
|
|
156 |
|
if (next.isCancelled()) |
157 |
|
current.setDeparture(cancelledString); |
158 |
|
if (prev.isCancelled()) |
159 |
|
current.setArrival(cancelledString); |
160 |
|
} |
161 |
|
} |
162 |
} |
} |
163 |
|
|
164 |
} else { |
} else { |
165 |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
166 |
} |
} |
167 |
webClient.closeAllWindows(); |
|
168 |
|
|
169 |
return timetableList; |
return timetableBean; |
170 |
} |
} |
171 |
|
|
172 |
|
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
173 |
|
TimetableBean timetableBean = new TimetableBean(); |
174 |
|
|
175 |
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
176 |
|
logger.fine("URL:" + url); |
177 |
|
|
178 |
|
|
179 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); |
180 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
181 |
|
|
182 |
|
Document doc = (Document) breaker.invoke(wrapper); |
183 |
|
|
184 |
|
|
185 |
|
boolean currentStation = false; |
186 |
|
boolean currentStationSaved = false; |
187 |
|
|
188 |
|
Elements tables = doc.getElementsByClass("Rute"); |
189 |
|
|
190 |
|
if (tables.size() == 1) { |
191 |
|
Element timetable = tables.get(0); |
192 |
|
Elements rows = timetable.getElementsByTag("tr"); |
193 |
|
|
194 |
|
for (int i=0; i<rows.size(); i++) { |
195 |
|
if (i==0) //First row is column headers |
196 |
|
continue; |
197 |
|
|
198 |
|
|
199 |
|
Element row = rows.get(i); |
200 |
|
Elements fields = row.getElementsByTag("td"); |
201 |
|
|
202 |
|
|
203 |
|
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
204 |
|
currentStation = true; |
205 |
|
continue; |
206 |
|
} |
207 |
|
|
208 |
|
TimetableEntry entry = new TimetableEntry(); |
209 |
|
|
210 |
|
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
211 |
|
if (station.equals("København")) |
212 |
|
station = "København H"; //correct inconsistency in naming |
213 |
|
|
214 |
|
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
215 |
|
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
216 |
|
|
217 |
|
entry.setStation( station ); |
218 |
|
entry.setArrival( arrival ); |
219 |
|
entry.setDeparture( departure ); |
220 |
|
|
221 |
|
|
222 |
|
if (currentStation == true && currentStationSaved == false ) { |
223 |
|
entry.setCurrent(currentStation); |
224 |
|
currentStationSaved = true; |
225 |
|
} |
226 |
|
|
227 |
|
entry.setStationId( getStationId( station )); |
228 |
|
|
229 |
|
timetableBean.entries.add(entry); |
230 |
|
} |
231 |
|
|
232 |
|
} else { |
233 |
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
return timetableBean; |
238 |
|
} |
239 |
|
|
240 |
} |
} |