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