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 |
Block: 1/1 |
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 |
Block: 1/1 |
String valueStr = getParamString(req, paramName); |
29 |
|
try { |
30 |
|
return Integer.parseInt(valueStr); |
31 |
|
} |
32 |
Block: 1/1 |
catch (NumberFormatException exc) { |
33 |
|
|
34 |
|
return 0; |
35 |
|
} |
36 |
|
} |
37 |
|
|
38 |
|
String getParamString(HttpServletRequest req, String paramName) { |
39 |
Block: 1/1 |
String valueStr = req.getParameter(paramName); |
40 |
|
if (null == valueStr|| valueStr.equals("")) { |
41 |
|
|
42 |
Block: 1/1 Branch: 1/2 |
return null; |
43 |
|
} |
44 |
Block: 1/1 Branch: 1/1 |
return valueStr; |
45 |
|
} |
46 |
|
|
47 |
|
Locale getLocale(HttpServletRequest req) { |
48 |
Block: 0/1 |
Locale result = null; |
49 |
|
String lang = getParamString(req, LANG); |
50 |
|
if (null != lang) { |
51 |
Block: 0/1 Branch: 0/1 |
result = new Locale(lang); |
52 |
|
} |
53 |
Block: 0/1 Branch: 0/1 |
if (null == result) { |
54 |
Block: 0/1 Branch: 0/1 |
result = Locale.getDefault(); |
55 |
|
} |
56 |
Block: 0/1 Branch: 0/1 |
return result; |
57 |
|
} |
58 |
|
|
59 |
|
@Override |
60 |
|
public void doGet(HttpServletRequest req, HttpServletResponse res) |
61 |
|
throws ServletException, IOException |
62 |
|
{ |
63 |
Block: 0/1 |
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 |
Block: 0/1 Branch: 0/1 |
stopInfo = server.getNextTripsForStopAllRoutes(stop); |
78 |
|
} |
79 |
|
else { |
80 |
Block: 0/1 Branch: 0/1 |
stopInfo = server.getNextTripsForStop(stop, route); |
81 |
|
} |
82 |
Block: 0/1 |
Schedule schedule = new Schedule(locale); |
83 |
|
|
84 |
|
schedule.writePage(pw, stopInfo); |
85 |
|
} |
86 |
|
finally { |
87 |
Block: 0/1 |
ExceptionUtils.tryClose(fis); |
88 |
|
} |
89 |
|
} |
90 |
Block: 0/1 |
catch (Throwable t) { |
91 |
|
writeErrorPage(pw, t, locale); |
92 |
Block: 0/1 |
} |
93 |
Block: 0/1 |
} |
94 |
|
|
95 |
|
void writeErrorPage(PrintWriter pw, Throwable t, Locale locale) { |
96 |
Block: 1/1 |
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 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
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 |
|
} |