1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
import java.io.ByteArrayOutputStream; |
|
3 |
import java.io.File; |
import java.io.File; |
4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
|
import java.io.StringReader; |
6 |
|
import java.io.StringWriter; |
7 |
import java.util.logging.Level; |
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
9 |
|
|
12 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
|
|
|
15 |
import javax.xml.transform.Source; |
import javax.xml.transform.Source; |
16 |
import javax.xml.transform.Templates; |
import javax.xml.transform.Templates; |
17 |
import javax.xml.transform.Transformer; |
import javax.xml.transform.Transformer; |
24 |
|
|
25 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
26 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
27 |
|
import dk.thoerup.traininfoservice.StationDAO; |
28 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
29 |
|
import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType; |
30 |
|
|
31 |
/** |
/** |
32 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
70 |
@Override |
@Override |
71 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
72 |
if (req.getParameter("station") == null) { |
if (req.getParameter("station") == null) { |
73 |
resp.sendError(400, "not enough parameters"); |
resp.sendError(400, "not enough parameters (station)"); |
74 |
return; |
return; |
75 |
} |
} |
76 |
|
if (req.getParameter("format") == null) { |
77 |
|
resp.sendError(400, "not enough parameters (format)"); |
78 |
|
return; |
79 |
|
} |
80 |
|
|
81 |
boolean arrival = false; |
boolean arrival = false; |
82 |
try { |
try { |
88 |
int station = Integer.parseInt( req.getParameter("station") ); |
int station = Integer.parseInt( req.getParameter("station") ); |
89 |
String format = req.getParameter("format"); |
String format = req.getParameter("format"); |
90 |
|
|
91 |
DepartureBean beans; |
DepartureBean beans; |
92 |
|
|
93 |
|
DepartureFetcher.FetchTrainType type = FetchTrainType.BOTH; |
94 |
|
if ( req.getParameter("type") != null) { |
95 |
|
type = FetchTrainType.valueOf( req.getParameter("type") ); |
96 |
|
} |
97 |
|
|
98 |
try { |
try { |
99 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival, type); |
100 |
beans.arrival = arrival; |
beans.arrival = arrival; |
101 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
102 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
104 |
resp.sendError(500); |
resp.sendError(500); |
105 |
return; |
return; |
106 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
107 |
logger.warning("Circuitbreaker - failing fast"); |
logger.warning("Circuitbreaker - failing fast, station=" +station); |
108 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
109 |
resp.sendError(500); |
resp.sendError(500); |
110 |
return; |
return; |
111 |
|
} catch (StationDAO.NostationException nse) { |
112 |
|
logger.log(Level.WARNING, "Station not in Database, station=" +station); |
113 |
|
Statistics.getInstance().incrementDepartureErrors(); |
114 |
|
resp.sendError(400, "invalid station ID"); |
115 |
|
return; |
116 |
} catch (Exception e) { |
} catch (Exception e) { |
117 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
118 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
127 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
128 |
resp.getWriter().print( formatXml(beans) ); |
resp.getWriter().print( formatXml(beans) ); |
129 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
130 |
/* |
|
131 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
132 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
133 |
|
|
134 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
135 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
136 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
137 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);*/ |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
138 |
resp.setContentType("text/html"); |
/*resp.setContentType("text/html"); |
139 |
resp.getWriter().print( xmlToHtml(formatXml(beans)) ); |
resp.getWriter().print( xmlToHtml(formatXml(beans)) );*/ |
140 |
} else { |
} else { |
141 |
resp.sendError(400, "Unknown format"); |
resp.sendError(400, "Unknown format"); |
142 |
} |
} |
147 |
|
|
148 |
Serializer serializer = new Persister(); |
Serializer serializer = new Persister(); |
149 |
|
|
150 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
StringWriter out = new StringWriter(); |
151 |
|
|
152 |
try { |
try { |
153 |
serializer.write(beans, out); |
serializer.write(beans, out); |
161 |
protected String xmlToHtml(String input) throws ServletException { |
protected String xmlToHtml(String input) throws ServletException { |
162 |
|
|
163 |
try { |
try { |
164 |
Transformer trans = xslTemplate.newTransformer(); |
Transformer trans = xslTemplate.newTransformer(); |
165 |
|
|
166 |
|
|
167 |
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
Source xml = new StreamSource( new StringReader(input)); |
168 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
StringWriter out = new StringWriter(); |
169 |
trans.transform(xml, new StreamResult(out)); |
trans.transform(xml, new StreamResult(out)); |
170 |
|
|
171 |
return out.toString(); |
return out.toString(); |