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