1 package net.jaekl.frank;
3 import java.io.ByteArrayOutputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.PrintStream;
7 import java.io.PrintWriter;
8 import java.util.Locale;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
15 import net.jaekl.frank.octranspo.Server;
16 import net.jaekl.frank.octranspo.StopInfo;
17 import net.jaekl.qd.util.ExceptionUtils;
19 public class ViewSchedule extends HttpServlet {
20 private static final long serialVersionUID = 1L;
22 static final String BUNDLE_NAME = "frank";
23 static final String STOP = "stop";
24 static final String ROUTE = "route";
25 static final String LANG = "lang";
27 int getParamInt(HttpServletRequest req, String paramName) {
28 String valueStr = getParamString(req, paramName);
30 return Integer.parseInt(valueStr);
32 catch (NumberFormatException exc) {
38 String getParamString(HttpServletRequest req, String paramName) {
39 String valueStr = req.getParameter(paramName);
40 if (null == valueStr|| valueStr.equals("")) {
47 Locale getLocale(HttpServletRequest req) {
49 String lang = getParamString(req, LANG);
51 result = new Locale(lang);
54 result = Locale.getDefault();
60 public void doGet(HttpServletRequest req, HttpServletResponse res)
61 throws ServletException, IOException
63 res.setContentType("text/html");
64 PrintWriter pw = res.getWriter();
66 Locale locale = getLocale(req);
67 int stop = getParamInt(req, STOP);
68 int route = getParamInt(req, ROUTE);
71 FileInputStream fis = new FileInputStream("apikey.txt");
73 Server server = new Server("192f31d2", fis);
74 StopInfo stopInfo = null;
77 stopInfo = server.getNextTripsForStopAllRoutes(stop);
80 stopInfo = server.getNextTripsForStop(stop, route);
82 Schedule schedule = new Schedule(locale);
84 schedule.writePage(pw, stopInfo);
87 ExceptionUtils.tryClose(fis);
91 writeErrorPage(pw, t, locale);
95 void writeErrorPage(PrintWriter pw, Throwable t, Locale locale) {
96 ByteArrayOutputStream baos = new ByteArrayOutputStream();
97 PrintStream ps = new PrintStream(baos);
98 FrankBundle bundle = FrankBundle.getInst(locale);
100 pw.println("<HTML><HEAD><TITLE>" +
101 bundle.get(FrankBundle.FRANK) + ": " +
102 bundle.get(FrankBundle.ERROR_PAGE) +
104 pw.println("<BODY><H1>" +
105 bundle.get(FrankBundle.FRANK) + ": " +
106 bundle.get(FrankBundle.UNEXPECTED_ERROR) +
109 // Note that, if we cared about security, we would log this stack trace to a
110 // server log, and only report a cross-reference to the log file back to the
111 // end user's browser, to avoid potentially exposing internal info that we
112 // don't want to share.
113 // At least at this point, we don't care (that much), and trade off a
114 // potential information leak in favour of reducing our code complexity
115 // and the administrator's workload.
116 t.printStackTrace(ps);
117 String stackTrace = baos.toString();
118 pw.println(stackTrace);
120 pw.println("</PRE></P></BODY>");
121 pw.println("</HTML>");