1 |
package dk.thoerup.traininfoservice.banedk;
|
2 |
|
3 |
import java.io.IOException;
|
4 |
import java.io.StringWriter;
|
5 |
import java.util.logging.Level;
|
6 |
import java.util.logging.Logger;
|
7 |
|
8 |
import javax.servlet.ServletException;
|
9 |
import javax.servlet.annotation.WebServlet;
|
10 |
import javax.servlet.http.HttpServlet;
|
11 |
import javax.servlet.http.HttpServletRequest;
|
12 |
import javax.servlet.http.HttpServletResponse;
|
13 |
import javax.xml.transform.Templates;
|
14 |
import javax.xml.transform.TransformerFactory;
|
15 |
|
16 |
import org.simpleframework.xml.Serializer;
|
17 |
import org.simpleframework.xml.core.Persister;
|
18 |
|
19 |
import dk.thoerup.android.traininfo.common.DepartureBean;
|
20 |
import dk.thoerup.android.traininfo.common.StationBean;
|
21 |
import dk.thoerup.circuitbreaker.CircuitBreakerException;
|
22 |
import dk.thoerup.traininfoservice.Statistics;
|
23 |
import dk.thoerup.traininfoservice.TraininfoSettings;
|
24 |
import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType;
|
25 |
import dk.thoerup.traininfoservice.db.StationDAO;
|
26 |
|
27 |
/**
|
28 |
* Servlet implementation class DepartureServlet
|
29 |
*/
|
30 |
@WebServlet(urlPatterns={"/DepartureServlet"})
|
31 |
public class DepartureServlet extends HttpServlet {
|
32 |
private static final long serialVersionUID = 1L;
|
33 |
|
34 |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() );
|
35 |
|
36 |
DepartureFetcher fetcher;
|
37 |
|
38 |
|
39 |
|
40 |
TransformerFactory xslTransFact;
|
41 |
Templates xslTemplate;
|
42 |
|
43 |
|
44 |
@Override
|
45 |
public void init() throws ServletException {
|
46 |
super.init();
|
47 |
|
48 |
|
49 |
TraininfoSettings settings = (TraininfoSettings) getServletContext().getAttribute("settings");
|
50 |
fetcher = new DepartureFetcher(settings);
|
51 |
|
52 |
/*
|
53 |
xslTransFact = TransformerFactory.newInstance();
|
54 |
|
55 |
String xslPath = getServletContext().getRealPath("/departures.xsl");
|
56 |
Source stylesheet = new StreamSource( new File(xslPath) );
|
57 |
|
58 |
try {
|
59 |
xslTemplate = xslTransFact.newTemplates(stylesheet);
|
60 |
} catch (Exception e) {
|
61 |
throw new ServletException(e);
|
62 |
}*/
|
63 |
}
|
64 |
|
65 |
@Override
|
66 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
67 |
if (req.getParameter("station") == null || req.getParameter("station").equals("") ) {
|
68 |
resp.sendError(400, "not enough parameters (station)");
|
69 |
return;
|
70 |
}
|
71 |
if (req.getParameter("format") == null || req.getParameter("format").equals("")) {
|
72 |
resp.sendError(400, "not enough parameters (format)");
|
73 |
return;
|
74 |
}
|
75 |
|
76 |
boolean arrival = false;
|
77 |
try {
|
78 |
arrival = Integer.parseInt( req.getParameter("arrival")) != 0;
|
79 |
} catch (Exception e) {}
|
80 |
|
81 |
Statistics.getInstance().incrementDepartureLookups();
|
82 |
|
83 |
int station = Integer.parseInt( req.getParameter("station") );
|
84 |
String format = req.getParameter("format");
|
85 |
|
86 |
DepartureBean beans;
|
87 |
|
88 |
DepartureFetcher.FetchTrainType type = FetchTrainType.BOTH;
|
89 |
if ( req.getParameter("type") != null) {
|
90 |
type = FetchTrainType.valueOf( req.getParameter("type") );
|
91 |
}
|
92 |
|
93 |
|
94 |
try {
|
95 |
beans = fetcher.cachedLookupDepartures(station, arrival, type);
|
96 |
beans.arrival = arrival;
|
97 |
} catch (java.io.IOException ioe) {
|
98 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() );
|
99 |
Statistics.getInstance().incrementDepartureErrors();
|
100 |
resp.sendError(500, "backend didnt answer");
|
101 |
return;
|
102 |
} catch (CircuitBreakerException cbe) {
|
103 |
logger.warning("Circuitbreaker - failing fast, station=" +station);
|
104 |
Statistics.getInstance().incrementDepartureErrors();
|
105 |
beans = generateErrorBean(1);
|
106 |
} catch (StationDAO.NostationException nse) {
|
107 |
logger.log(Level.WARNING, "Station not in Database, station=" +station);
|
108 |
Statistics.getInstance().incrementDepartureErrors();
|
109 |
resp.sendError(400, "invalid station ID");
|
110 |
return;
|
111 |
} catch (Exception e) {
|
112 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e);
|
113 |
Statistics.getInstance().incrementDepartureErrors();
|
114 |
resp.sendError(500);
|
115 |
return;
|
116 |
}
|
117 |
|
118 |
resp.setDateHeader("Expires", 0);
|
119 |
resp.setHeader("Cache-Control", "no-cache, must-revalidate");
|
120 |
|
121 |
if (format.equalsIgnoreCase("xml")) {
|
122 |
resp.setContentType("text/xml");
|
123 |
resp.getWriter().print( formatXml(beans) );
|
124 |
} else if (format.equalsIgnoreCase("html")) {
|
125 |
|
126 |
String advStr = req.getParameter("advanced");
|
127 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false;
|
128 |
|
129 |
req.setAttribute("advanced", advanced);
|
130 |
req.setAttribute("departurebeans", beans);
|
131 |
req.setAttribute("stationID", station );
|
132 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);
|
133 |
/*resp.setContentType("text/html");
|
134 |
resp.getWriter().print( xmlToHtml(formatXml(beans)) );*/
|
135 |
} else {
|
136 |
resp.sendError(400, "Unknown format");
|
137 |
}
|
138 |
}
|
139 |
|
140 |
protected DepartureBean generateErrorBean(int code) {
|
141 |
DepartureBean bean = new DepartureBean();
|
142 |
bean.stationName="";
|
143 |
bean.errorCode = code;
|
144 |
return bean;
|
145 |
}
|
146 |
|
147 |
protected String formatXml(DepartureBean beans) throws ServletException{
|
148 |
|
149 |
Serializer serializer = new Persister();
|
150 |
|
151 |
StringWriter out = new StringWriter();
|
152 |
|
153 |
try {
|
154 |
serializer.write(beans, out);
|
155 |
} catch (Exception e) {
|
156 |
throw new ServletException(e);
|
157 |
}
|
158 |
|
159 |
return out.toString();
|
160 |
}
|
161 |
/*
|
162 |
protected String xmlToHtml(String input) throws ServletException {
|
163 |
|
164 |
try {
|
165 |
Transformer trans = xslTemplate.newTransformer();
|
166 |
|
167 |
|
168 |
Source xml = new StreamSource( new StringReader(input));
|
169 |
StringWriter out = new StringWriter();
|
170 |
trans.transform(xml, new StreamResult(out));
|
171 |
|
172 |
return out.toString();
|
173 |
} catch (Exception e) {
|
174 |
throw new ServletException(e);
|
175 |
}
|
176 |
}*/
|
177 |
|
178 |
}
|