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 | 0 | 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 | 0 | HttpServletResponse response) throws IOException { |
50 | 0 | this.request = request; |
51 | 0 | this.out = response.getWriter(); |
52 | 0 | this.locale = request.getLocale(); |
53 | 0 | this.messages = new Messages(locale); |
54 | 0 | } |
55 | |
|
56 | |
public void print() { |
57 | 0 | final String path = request.getParameter("path"); |
58 | |
|
59 | 0 | out.println("<form name=\"pathSerachForm\">"); |
60 | |
|
61 | 0 | out.print("<label for=\"path\">"); |
62 | 0 | out.print(messages.getString("lbl.path")); |
63 | 0 | out.print("</label>"); |
64 | 0 | out.print("<input type=\"text\" name=\"path\" size=\"40\" value=\""); |
65 | 0 | out.print(escapeHtml(path)); |
66 | 0 | out.println("\" />"); |
67 | |
|
68 | 0 | out.print("<input type=\"submit\" value=\""); |
69 | 0 | out.print(messages.getString("lbl.test")); |
70 | 0 | out.println("\">"); |
71 | |
|
72 | 0 | out.print("<input type=\"button\" value=\""); |
73 | 0 | out.print(messages.getString("lbl.clear")); |
74 | 0 | out |
75 | |
.println("\" onclick=\"pathSerachForm.path.value='';pathSerachForm.submit();\">"); |
76 | |
|
77 | 0 | out.println("</form>"); |
78 | |
|
79 | 0 | out.println("<table>"); |
80 | 0 | out.println("<thead>"); |
81 | 0 | out.println(th(messages.getString("lbl.no"))); |
82 | 0 | out.println(th(messages.getString("lbl.regexp"))); |
83 | 0 | out.println(th(messages.getString("lbl.requestMethod"))); |
84 | 0 | out.println(th(messages.getString("lbl.actionMethod"))); |
85 | 0 | out.println(th(messages.getString("lbl.pathParams"))); |
86 | 0 | out.println(th(messages.getString("lbl.priority"))); |
87 | 0 | out.println("</thead>"); |
88 | 0 | out.println("<tbody>"); |
89 | |
|
90 | 0 | final PathResolverProvider pathResolverProvider = ProviderFactory |
91 | |
.get(PathResolverProvider.class); |
92 | 0 | final PathResolver pathResolver = pathResolverProvider |
93 | |
.getPathResolver(); |
94 | |
|
95 | 0 | int no = 1; |
96 | 0 | for (final Routing routing : pathResolver.getRoutings()) { |
97 | |
final boolean write; |
98 | |
final Map<String, String> pathParameters; |
99 | 0 | if (path == null || path.length() == 0) { |
100 | 0 | write = true; |
101 | 0 | pathParameters = null; |
102 | |
} else { |
103 | 0 | final Matcher matcher = routing.getPattern().matcher(path); |
104 | 0 | if (matcher.matches()) { |
105 | 0 | write = true; |
106 | 0 | pathParameters = new HashMap<String, String>(); |
107 | 0 | for (int i = 0; i < matcher.groupCount(); i++) { |
108 | 0 | final String name = routing.getUriParameterNames().get( |
109 | |
i); |
110 | 0 | final String value = matcher.group(i + 1); |
111 | 0 | pathParameters.put(name, value); |
112 | |
} |
113 | |
} else { |
114 | 0 | write = false; |
115 | 0 | pathParameters = null; |
116 | |
} |
117 | |
} |
118 | 0 | if (write) { |
119 | 0 | out.print("<tr class=\""); |
120 | 0 | out.print(rowClasses.next()); |
121 | 0 | out.println("\">"); |
122 | 0 | out.println(td(no)); |
123 | 0 | out.println(td(routing.getPattern().pattern())); |
124 | 0 | out.println(td(routing.getRequestMethod())); |
125 | 0 | out.println(td(routing.getActionClass().getName() + "#" |
126 | |
+ routing.getActionMethod().getName())); |
127 | 0 | if (pathParameters == null) { |
128 | 0 | out.println(td(routing.getUriParameterNames())); |
129 | |
} else { |
130 | 0 | out.println(td(pathParameters)); |
131 | |
} |
132 | 0 | out.println(td(routing.getPriority())); |
133 | 0 | out.println("</tr>"); |
134 | |
} |
135 | 0 | no++; |
136 | 0 | } |
137 | 0 | out.println("</tbody>"); |
138 | 0 | out.println("</table>"); |
139 | 0 | } |
140 | |
|
141 | |
private static String th(final Object body) { |
142 | 0 | return "<th>" + String.valueOf(body) + "</th>"; |
143 | |
} |
144 | |
|
145 | |
private static String td(final Object body) { |
146 | 0 | 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 | 0 | if (str == null) { |
188 | 0 | return ""; |
189 | |
} |
190 | 0 | String text = str.toString(); |
191 | 0 | text = StringUtils.replace(text, "&", "&"); |
192 | 0 | text = StringUtils.replace(text, "<", "<"); |
193 | 0 | text = StringUtils.replace(text, ">", ">"); |
194 | 0 | text = StringUtils.replace(text, "\"", """); |
195 | 0 | text = StringUtils.replace(text, "'", "'"); |
196 | 0 | return text; |
197 | |
} |
198 | |
|
199 | |
} |