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