View Javadoc

1   /*
2    * Copyright 2004-2009 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  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 	 * 指定された文字列をHTMLとしてエスケープします。
151 	 * <p>
152 	 * <table>
153 	 * <thead>
154 	 * <tr>
155 	 * <th>変換前</th>
156 	 * <th>変換後</th>
157 	 * </tr>
158 	 * </thead> <tbody>
159 	 * <tr>
160 	 * <td>&amp;</td>
161 	 * <td>&amp;amp;</td>
162 	 * </tr>
163 	 * <tr>
164 	 * <td>&lt;</td>
165 	 * <td>&amp;lt;</td>
166 	 * </tr>
167 	 * <tr>
168 	 * <td>&gt;</td>
169 	 * <td>&amp;gt;</td>
170 	 * </tr>
171 	 * <tr>
172 	 * <td>&quot;</td>
173 	 * <td>&amp;quot;</td>
174 	 * </tr>
175 	 * <tr>
176 	 * <td>&#39</td>
177 	 * <td>&amp;#39</td>
178 	 * </tr>
179 	 * </tbody>
180 	 * </table>
181 	 * </p>
182 	 * 
183 	 * @param str
184 	 * @return エスケープされた文字列
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, "&", "&amp;");
192 		text = StringUtils.replace(text, "<", "&lt;");
193 		text = StringUtils.replace(text, ">", "&gt;");
194 		text = StringUtils.replace(text, "\"", "&quot;");
195 		text = StringUtils.replace(text, "'", "&#39;");
196 		return text;
197 	}
198 
199 }