1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.sql.Connection; |
|
|
import java.sql.ResultSet; |
|
|
import java.sql.Statement; |
|
4 |
import java.util.List; |
import java.util.List; |
5 |
import java.util.logging.Level; |
import java.util.logging.Level; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
21 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
22 |
import org.w3c.dom.Element; |
import org.w3c.dom.Element; |
23 |
|
|
24 |
import dk.thoerup.curcuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
25 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.StationDAO; |
26 |
|
|
27 |
/** |
/** |
28 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
32 |
|
|
33 |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
34 |
|
|
35 |
DepartureFetcher fetcher = new DepartureFetcher(); |
DepartureFetcher fetcher; |
36 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
37 |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
38 |
|
|
39 |
|
|
40 |
@Override |
@Override |
41 |
|
public void init() throws ServletException { |
42 |
|
super.init(); |
43 |
|
|
44 |
|
boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); |
45 |
|
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
46 |
|
logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); |
47 |
|
fetcher = new DepartureFetcher(useTempSite, cacheTimeout); |
48 |
|
} |
49 |
|
|
50 |
|
@Override |
51 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
52 |
if (req.getParameter("station") == null) { |
if (req.getParameter("station") == null) { |
53 |
resp.sendError(400, "not enough parameters"); |
resp.sendError(400, "not enough parameters"); |
59 |
|
|
60 |
List<DepartureBean> beans; |
List<DepartureBean> beans; |
61 |
|
|
62 |
String stationName = getStationName(station); |
String stationName = StationDAO.getStationName(station); |
63 |
|
|
64 |
try { |
try { |
65 |
beans = fetcher.cachedLookupDepartures(station); |
beans = fetcher.cachedLookupDepartures(station); |
66 |
} catch (java.net.SocketTimeoutException ste) { |
} catch (java.io.IOException ioe) { |
67 |
logger.warning("Read timed out, station="+station); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
68 |
resp.sendError(500); |
resp.sendError(500); |
69 |
return; |
return; |
70 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
84 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
85 |
resp.getWriter().print( formatXml(beans, stationName) ); |
resp.getWriter().print( formatXml(beans, stationName) ); |
86 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
87 |
|
|
88 |
|
String advStr = req.getParameter("advanced"); |
89 |
|
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
90 |
|
|
91 |
|
req.setAttribute("advanced", advanced); |
92 |
req.setAttribute("stationname", stationName ); |
req.setAttribute("stationname", stationName ); |
93 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
94 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
144 |
note.setTextContent( departure.getNote() ); |
note.setTextContent( departure.getNote() ); |
145 |
train.appendChild(note); |
train.appendChild(note); |
146 |
|
|
147 |
|
Element type = doc.createElement("type"); |
148 |
|
type.setTextContent( departure.getType() ); |
149 |
|
train.appendChild(type); |
150 |
|
|
151 |
root.appendChild(train); |
root.appendChild(train); |
152 |
} |
} |
153 |
|
|
174 |
return xml; |
return xml; |
175 |
} |
} |
176 |
|
|
|
protected String getStationName(int stationID) { |
|
|
String station = ""; |
|
|
|
|
|
Connection conn = null; |
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
Statement stmt = conn.createStatement(); |
|
|
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); |
|
|
if (rs.next()) { |
|
|
station = rs.getString(1); |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
} finally { |
|
|
try { |
|
|
if (conn != null && !conn.isClosed()) |
|
|
conn.close(); |
|
|
} catch (Exception e) {} |
|
|
} |
|
|
|
|
|
return station; |
|
|
} |
|
|
|
|
177 |
} |
} |