3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
import java.io.PrintWriter; |
import java.io.PrintWriter; |
5 |
import java.net.URL; |
import java.net.URL; |
6 |
|
import java.net.URLDecoder; |
7 |
|
import java.net.URLEncoder; |
8 |
import java.util.Map; |
import java.util.Map; |
9 |
import java.util.Set; |
import java.util.Set; |
10 |
import java.util.TreeMap; |
import java.util.TreeMap; |
26 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
27 |
import dk.thoerup.traininfoservice.db.StationDAO; |
import dk.thoerup.traininfoservice.db.StationDAO; |
28 |
|
|
29 |
|
import dk.thoerup.genericjavautils.TimeoutMap; |
30 |
|
|
31 |
|
|
32 |
@WebServlet(urlPatterns={"/CompareStations"}) |
@WebServlet(urlPatterns={"/CompareStations"}) |
33 |
public class CompareStations extends HttpServlet { |
public class CompareStations extends HttpServlet { |
34 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
|
|
|
35 |
|
|
36 |
|
Map<String,String> cache = new TimeoutMap<String,String>(600 * 1000); |
37 |
|
|
38 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
39 |
|
try { |
40 |
|
String data = ""; |
41 |
|
String uri = request.getParameter("uri"); |
42 |
|
|
43 |
|
if (uri != null) { |
44 |
|
boolean require = Boolean.parseBoolean( request.getParameter("requiredata") ); |
45 |
|
data = hasData(uri, require); |
46 |
|
} else { |
47 |
|
data = getData(request); |
48 |
|
} |
49 |
|
|
50 |
|
PrintWriter out = response.getWriter(); |
51 |
|
out.print(data); |
52 |
|
|
53 |
|
} catch (Exception e) { |
54 |
|
throw new ServletException(e); |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
|
String getData(HttpServletRequest request) throws Exception { |
59 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
60 |
|
|
61 |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk/Trafikinformation/Stationsliste"), 5000); |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk/Trafikinformation/Stationsliste"), 5000); |
62 |
|
|
63 |
Map<String,String> banedkStations = new TreeMap<String,String>(); |
Map<String,String> banedkStations = new TreeMap<String,String>(); |
64 |
Set<String> dbStations = new TreeSet<String>(); |
Set<String> dbStations = new TreeSet<String>(); |
65 |
|
|
66 |
|
StringBuilder sb = new StringBuilder(); |
67 |
|
sb.append("<html>"); |
68 |
|
sb.append("<head>"); |
69 |
|
sb.append("<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>\n"); |
70 |
|
sb.append("<script type='text/javascript' src='compareloader.js'></script>\n"); |
71 |
|
|
72 |
|
sb.append("</head>"); |
73 |
|
sb.append("<body>"); |
74 |
|
|
75 |
StationDAO dao = new StationDAO(); |
StationDAO dao = new StationDAO(); |
76 |
|
|
77 |
try { |
try { |
88 |
} |
} |
89 |
} |
} |
90 |
|
|
91 |
|
sb.append("<h2>Active stations in DB</h2>\n"); |
92 |
|
sb.append("<ul>"); |
93 |
|
|
94 |
StationBean bean = dao.dumpAll(); |
StationBean bean = dao.dumpAll(); |
95 |
for (StationEntry station : bean.entries) { |
for (StationEntry station : bean.entries) { |
96 |
if (station.isRegional() || station.isStrain()) { |
if (station.isRegional() || station.isStrain()) { |
97 |
dbStations.add(station.getName()); |
String href = banedkStations.get(station.getName() ); |
98 |
|
String hrefCode = "null"; |
99 |
|
if ( href != null) { |
100 |
|
String hrefParts[] = href.split("/"); |
101 |
|
hrefCode = URLDecoder.decode(hrefParts[4], "UTF-8"); |
102 |
|
} |
103 |
|
|
104 |
|
String id = ""; |
105 |
|
String uri = ""; |
106 |
|
if (station.isRegional() ) { |
107 |
|
id = station.getRegional(); |
108 |
|
uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/Fjerntog"; |
109 |
|
} |
110 |
|
if (station.isStrain() ) { |
111 |
|
id = station.getStrain(); |
112 |
|
uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/S-Tog"; |
113 |
|
} |
114 |
|
|
115 |
|
String idMismatch = ""; |
116 |
|
if (!hrefCode.equals(id) ) { |
117 |
|
idMismatch = " ID Mismatch (DB="+id+ ", BaneDK=" + hrefCode + ")"; |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + station.getName() + "</a> <span class='uri' requiredata='true' uri='" + uri + "'></span> " + idMismatch + "</li>\n" ); |
122 |
|
|
123 |
|
dbStations.add(station.getName()); |
124 |
} |
} |
125 |
|
|
126 |
|
|
127 |
} |
} |
128 |
|
sb.append("</ul>\n"); |
129 |
|
|
130 |
} catch (Exception e) { |
} catch (Exception e) { |
131 |
throw new ServletException(e); |
throw new ServletException(e); |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
StringBuilder sb = new StringBuilder(); |
sb.append("<h2>stations on website not in db</h2>\n"); |
|
|
|
|
sb.append("<h2>stations on website not in db</h2>"); |
|
137 |
sb.append("<ul>"); |
sb.append("<ul>"); |
138 |
Set<String> tmpDbStations = new TreeSet<String>( dbStations ); |
Set<String> tmpDbStations = new TreeSet<String>( dbStations ); |
139 |
Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations.keySet()) ; |
Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations.keySet()) ; |
144 |
for(String s : tmpBanedkStations) { |
for(String s : tmpBanedkStations) { |
145 |
String uri = banedkStations.get(s); |
String uri = banedkStations.get(s); |
146 |
String disabled = ""; |
String disabled = ""; |
147 |
String data = hasData(uri); |
//String data = hasData(uri, false); |
148 |
|
|
149 |
try { |
try { |
150 |
boolean tmpdisabled = dao.hasDisabledStation(s); |
boolean tmpdisabled = dao.hasDisabledStation(s); |
151 |
if (tmpdisabled == true) |
if (tmpdisabled == true) |
152 |
disabled = "disabled"; |
disabled = " - disabled"; |
153 |
} catch (Exception e) { |
} catch (Exception e) { |
154 |
throw new ServletException(e); |
throw new ServletException(e); |
155 |
} |
} |
156 |
|
|
157 |
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + s + "</a> " + data + " - " + disabled + "</li>" ); |
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + s + "</a> <span class='uri' requiredata='false' uri='" + uri + "'></span>" + disabled + "</li>\n" ); |
158 |
} |
} |
159 |
sb.append("</ul>"); |
sb.append("</ul>"); |
160 |
|
|
165 |
tmpBanedkStations = new TreeSet<String>( banedkStations.keySet() ) ; |
tmpBanedkStations = new TreeSet<String>( banedkStations.keySet() ) ; |
166 |
tmpDbStations.removeAll(tmpBanedkStations); |
tmpDbStations.removeAll(tmpBanedkStations); |
167 |
for(String s : tmpDbStations) { |
for(String s : tmpDbStations) { |
168 |
sb.append( "<li>" + s + "</li>" ); |
sb.append( "<li>" + s + "</li>\n" ); |
169 |
} |
} |
170 |
sb.append("</ul>"); |
sb.append("</ul>"); |
171 |
|
sb.append("<font size='1' id='antal'></font>"); |
172 |
|
sb.append("</body></html>\n"); |
173 |
|
|
174 |
|
return sb.toString(); |
|
PrintWriter out = response.getWriter(); |
|
|
out.print(sb.toString()); |
|
175 |
|
|
176 |
} |
} |
177 |
|
|
178 |
String hasData(String uri) { |
String hasData(String uri, boolean requiredata) { |
179 |
String returnVal = "unknown"; |
String key = uri + requiredata; |
180 |
|
|
181 |
|
String data = cache.get(key); |
182 |
|
if (data == null) { |
183 |
|
|
184 |
try { |
data = "<font color='yellow'>unknown</font>"; |
185 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
try { |
186 |
|
data = hasDataWorker(uri,requiredata); |
187 |
|
cache.put(key,data); |
188 |
|
} catch (Exception e) { |
189 |
|
System.out.println( e.getMessage() ); |
190 |
|
} |
191 |
|
} |
192 |
|
return data; |
193 |
|
} |
194 |
|
|
195 |
|
String hasDataWorker(String uri, boolean requiredata) throws Exception { |
196 |
|
String returnVal = ""; |
197 |
|
|
198 |
|
String parts[] = uri.split("\\/"); |
199 |
|
String id = URLEncoder.encode(parts[4], "ISO-8859-1"); |
200 |
|
String uri2 = "/TrafikInformation/AfgangAnkomst/Afgang/" + id + "/" + parts[5]; |
201 |
|
|
202 |
|
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
203 |
|
|
204 |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri), 5000); |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri2), 2000); |
205 |
|
|
206 |
Document doc = (Document) cb.invoke(jsoup); |
Document doc = (Document) cb.invoke(jsoup); |
207 |
|
|
208 |
Element tabel = doc.getElementById("afgangtabel"); |
Element tabel = doc.getElementById("afgangtabel"); |
209 |
if ( tabel != null) { |
if ( tabel != null) { |
210 |
|
if (requiredata == true) { |
211 |
|
returnVal = ""; |
212 |
|
} else { |
213 |
returnVal = "<font color='red'>Data!</font>"; |
returnVal = "<font color='red'>Data!</font>"; |
214 |
|
} |
215 |
|
} else { |
216 |
|
if (requiredata == true) { |
217 |
|
returnVal = "<font color='red'>No data</font>"; |
218 |
} else { |
} else { |
219 |
returnVal = ""; |
returnVal = ""; |
220 |
} |
} |
|
|
|
|
} catch (Exception e) { |
|
|
System.out.println( e.getMessage() ); |
|
221 |
} |
} |
222 |
|
|
223 |
|
|
224 |
return returnVal; |
return returnVal; |
225 |
} |
} |