Coverage Report - org.seasar.cubby.admin.servlet.RoutingSection
 
Classes in this File Line Coverage Branch Coverage Complexity
RoutingSection
0%
0/78
0%
0/16
2.8
 
 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  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  
          * 指定された文字列を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  0
                 if (str == null) {
 188  0
                         return "";
 189  
                 }
 190  0
                 String text = str.toString();
 191  0
                 text = StringUtils.replace(text, "&", "&amp;");
 192  0
                 text = StringUtils.replace(text, "<", "&lt;");
 193  0
                 text = StringUtils.replace(text, ">", "&gt;");
 194  0
                 text = StringUtils.replace(text, "\"", "&quot;");
 195  0
                 text = StringUtils.replace(text, "'", "&#39;");
 196  0
                 return text;
 197  
         }
 198  
 
 199  
 }