1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.seasar.cubby.admin.servlet;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.Locale;
25 import java.util.Map;
26 import java.util.regex.Matcher;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.seasar.cubby.routing.PathResolver;
32 import org.seasar.cubby.routing.Routing;
33 import org.seasar.cubby.spi.PathResolverProvider;
34 import org.seasar.cubby.spi.ProviderFactory;
35
36 class RoutingSection implements Section {
37
38 private HttpServletRequest request;
39
40 private PrintWriter out;
41
42 private Locale locale;
43
44 private Iterator<String> rowClasses = new LoopingIterator<String>(Arrays
45 .asList(new String[] { "odd", "even" }));
46
47 private Messages messages;
48
49 public RoutingSection(HttpServletRequest request,
50 HttpServletResponse response) throws IOException {
51 this.request = request;
52 this.out = response.getWriter();
53 this.locale = request.getLocale();
54 this.messages = new Messages(locale);
55 }
56
57 public void print() {
58 final String path = request.getParameter("path");
59
60 out.println("<form name=\"pathSerachForm\">");
61
62 out.print("<label for=\"path\">");
63 out.print(messages.getString("lbl.path"));
64 out.print("</label>");
65 out.print("<input type=\"text\" name=\"path\" size=\"40\" value=\"");
66 out.print(escapeHtml(path));
67 out.println("\" />");
68
69 out.print("<input type=\"submit\" value=\"");
70 out.print(messages.getString("lbl.test"));
71 out.println("\">");
72
73 out.print("<input type=\"button\" value=\"");
74 out.print(messages.getString("lbl.clear"));
75 out
76 .println("\" onclick=\"pathSerachForm.path.value='';pathSerachForm.submit();\">");
77
78 out.println("</form>");
79
80 out.println("<table>");
81 out.println("<thead>");
82 out.println(th(messages.getString("lbl.no")));
83 out.println(th(messages.getString("lbl.regexp")));
84 out.println(th(messages.getString("lbl.requestMethod")));
85 out.println(th(messages.getString("lbl.actionMethod")));
86 out.println(th(messages.getString("lbl.pathParams")));
87 out.println(th(messages.getString("lbl.priority")));
88 out.println("</thead>");
89 out.println("<tbody>");
90
91 final PathResolverProvider pathResolverProvider = ProviderFactory
92 .get(PathResolverProvider.class);
93 final PathResolver pathResolver = pathResolverProvider
94 .getPathResolver();
95
96 int no = 1;
97 for (final Routing routing : pathResolver.getRoutings()) {
98 final boolean write;
99 final Map<String, String> pathParameters;
100 if (path == null || path.length() == 0) {
101 write = true;
102 pathParameters = null;
103 } else {
104 final Matcher matcher = routing.getPattern().matcher(path);
105 if (matcher.matches()) {
106 write = true;
107 pathParameters = new HashMap<String, String>();
108 for (int i = 0; i < matcher.groupCount(); i++) {
109 final String name = routing.getUriParameterNames().get(
110 i);
111 final String value = matcher.group(i + 1);
112 pathParameters.put(name, value);
113 }
114 } else {
115 write = false;
116 pathParameters = null;
117 }
118 }
119 if (write) {
120 out.print("<tr class=\"");
121 out.print(rowClasses.next());
122 out.println("\">");
123 out.println(td(no));
124 out.println(td(routing.getPattern().pattern()));
125 out.println(td(routing.getRequestMethod()));
126 out.println(td(routing.getActionClass().getName() + "#"
127 + routing.getActionMethod().getName()));
128 if (pathParameters == null) {
129 out.println(td(routing.getUriParameterNames()));
130 } else {
131 out.println(td(pathParameters));
132 }
133 out.println(td(routing.getPriority()));
134 out.println("</tr>");
135 }
136 no++;
137 }
138 out.println("</tbody>");
139 out.println("</table>");
140 }
141
142 private static String th(final Object body) {
143 return "<th>" + String.valueOf(body) + "</th>";
144 }
145
146 private static String td(final Object body) {
147 return "<td>" + String.valueOf(body) + "</td>";
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public static String escapeHtml(final Object str) {
188 if (str == null) {
189 return "";
190 }
191 String text = str.toString();
192 text = StringUtils.replace(text, "&", "&");
193 text = StringUtils.replace(text, "<", "<");
194 text = StringUtils.replace(text, ">", ">");
195 text = StringUtils.replace(text, "\"", """);
196 text = StringUtils.replace(text, "'", "'");
197 return text;
198 }
199
200 }