Initial
[frank.git] / prod / net / jaekl / frank / ViewSchedule.java
1 package net.jaekl.frank;
2
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;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import net.jaekl.frank.octranspo.Server;
16 import net.jaekl.frank.octranspo.StopInfo;
17 import net.jaekl.qd.util.ExceptionUtils;
18
19 public class ViewSchedule extends HttpServlet {
20         private static final long serialVersionUID = 1L;
21         
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";
26         
27         int getParamInt(HttpServletRequest req, String paramName) {
28                 String valueStr = getParamString(req, paramName);
29                 try {
30                         return Integer.parseInt(valueStr);
31                 }
32                 catch (NumberFormatException exc) {
33                         // TODO:  Error page
34                         return 0;
35                 }
36         }
37         
38         String getParamString(HttpServletRequest req, String paramName) {
39                 String valueStr = req.getParameter(paramName);
40                 if (null == valueStr|| valueStr.equals("")) {
41                         // TODO:  Error page
42                         return null;
43                 }
44                 return valueStr;
45         }
46         
47         Locale getLocale(HttpServletRequest req) {
48                 Locale result = null;
49                 String lang = getParamString(req, LANG);
50                 if (null != lang) {
51                         result = new Locale(lang);
52                 }
53                 if (null == result) {
54                         result = Locale.getDefault();
55                 }
56                 return result;
57         }
58
59         @Override
60         public void doGet(HttpServletRequest req, HttpServletResponse res)
61         throws ServletException, IOException
62         {
63                 res.setContentType("text/html");
64                 PrintWriter pw = res.getWriter();
65                 
66                 Locale locale = getLocale(req);
67                 int stop = getParamInt(req, STOP);
68                 int route = getParamInt(req, ROUTE);
69                 
70                 try {
71                         FileInputStream fis = new FileInputStream("apikey.txt");
72                         try {
73                                 Server server = new Server("192f31d2", fis);
74                                 StopInfo stopInfo = null;
75                                 
76                                 if (0 == route) {
77                                         stopInfo = server.getNextTripsForStopAllRoutes(stop);
78                                 }
79                                 else {
80                                         stopInfo = server.getNextTripsForStop(stop, route);
81                                 }
82                                 Schedule schedule = new Schedule(locale);
83                                 
84                                 schedule.writePage(pw, stopInfo);
85                         }
86                         finally {
87                                 ExceptionUtils.tryClose(fis);
88                         }
89                 }
90                 catch (Throwable t) {
91                         writeErrorPage(pw, t, locale);
92                 }
93         }
94         
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);
99                 
100                 pw.println("<HTML><HEAD><TITLE>" + 
101                                 bundle.get(FrankBundle.FRANK) + ": " +
102                                 bundle.get(FrankBundle.ERROR_PAGE) + 
103                                 "</TITLE></HEAD>");
104                 pw.println("<BODY><H1>" +
105                                 bundle.get(FrankBundle.FRANK) + ": " +
106                                 bundle.get(FrankBundle.UNEXPECTED_ERROR) + 
107                                 "</H1><P><PRE>");
108
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);
119                                 
120                 pw.println("</PRE></P></BODY>");
121                 pw.println("</HTML>");
122         }
123         
124
125 }