View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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 	 * 指定された文字列をHTMLとしてエスケープします。
152 	 * <p>
153 	 * <table>
154 	 * <thead>
155 	 * <tr>
156 	 * <th>変換前</th>
157 	 * <th>変換後</th>
158 	 * </tr>
159 	 * </thead> <tbody>
160 	 * <tr>
161 	 * <td>&amp;</td>
162 	 * <td>&amp;amp;</td>
163 	 * </tr>
164 	 * <tr>
165 	 * <td>&lt;</td>
166 	 * <td>&amp;lt;</td>
167 	 * </tr>
168 	 * <tr>
169 	 * <td>&gt;</td>
170 	 * <td>&amp;gt;</td>
171 	 * </tr>
172 	 * <tr>
173 	 * <td>&quot;</td>
174 	 * <td>&amp;quot;</td>
175 	 * </tr>
176 	 * <tr>
177 	 * <td>&#39</td>
178 	 * <td>&amp;#39</td>
179 	 * </tr>
180 	 * </tbody>
181 	 * </table>
182 	 * </p>
183 	 * 
184 	 * @param str
185 	 * @return エスケープされた文字列
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, "&", "&amp;");
193 		text = StringUtils.replace(text, "<", "&lt;");
194 		text = StringUtils.replace(text, ">", "&gt;");
195 		text = StringUtils.replace(text, "\"", "&quot;");
196 		text = StringUtils.replace(text, "'", "&#39;");
197 		return text;
198 	}
199 
200 }