Coverage Report - org.seasar.cubby.admin.servlet.CubbyAdminServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
CubbyAdminServlet
0%
0/47
0%
0/10
0
 
 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.ByteArrayOutputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.io.PrintWriter;
 23  
 
 24  
 import javax.servlet.ServletConfig;
 25  
 import javax.servlet.ServletException;
 26  
 import javax.servlet.http.HttpServlet;
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 import javax.servlet.http.HttpServletResponse;
 29  
 
 30  
 /**
 31  
  * Cubbyの管理コンソールを表示するサーブレットです。
 32  
  * <ul>
 33  
  * <li>ローカルホストのみから利用できます。</li>
 34  
  * <li>「http://localhost/(コンテキストパス)/cubby-admin」でアクセスできます。</li>
 35  
  * <li>web.xmlに以下の設定を追加してください。</li>
 36  
  * </ul>
 37  
  * <code>
 38  
  *   <servlet>
 39  
  *     <servlet-name>cubbyAdminServlet</servlet-name>
 40  
  *     <servlet-class>org.seasar.cubby.admin.servlet.CubbyAdminServlet</servlet-class>
 41  
  *   </servlet>
 42  
  *   ...
 43  
  *   <servlet-mapping>
 44  
  *     <servlet-name>cubbyAdminServlet</servlet-name>
 45  
  *     <url-pattern>/cubby-admin</url-pattern>
 46  
  *   </servlet-mapping>
 47  
  * </code>
 48  
  * 
 49  
  * @author agata
 50  
  * @since 1.1.0
 51  
  */
 52  0
 public class CubbyAdminServlet extends HttpServlet {
 53  
 
 54  
         private static final long serialVersionUID = 1L;
 55  
 
 56  
         private String css;
 57  
 
 58  
         @Override
 59  
         public void init(final ServletConfig config) throws ServletException {
 60  0
                 super.init(config);
 61  0
                 css = getResource("cubby-admin.css");
 62  0
         }
 63  
 
 64  
         @Override
 65  
         protected void doGet(final HttpServletRequest request,
 66  
                         final HttpServletResponse response) throws ServletException,
 67  
                         IOException {
 68  0
                 final String remoteAddr = request.getRemoteAddr();
 69  0
                 if (!isLoopBackAddress(remoteAddr)) {
 70  0
                         return;
 71  
                 }
 72  0
                 request.setCharacterEncoding("UTF-8");
 73  0
                 response.setCharacterEncoding("UTF-8");
 74  0
                 render(request, response);
 75  0
         }
 76  
 
 77  
         private boolean isLoopBackAddress(final String remoteAddr) {
 78  
                 // NOTE:
 79  
                 // MacOSX 10.5 + Tomcat 6ではループバックアドレスが
 80  
                 // 「0:0:0:0:0:0:0:1%0」となるため、startWithを使い判定を行う
 81  0
                 return remoteAddr.equals("127.0.0.1")
 82  
                                 || remoteAddr.startsWith("0:0:0:0:0:0:0:1");
 83  
         }
 84  
 
 85  
         private void render(final HttpServletRequest request,
 86  
                         final HttpServletResponse response) throws IOException {
 87  0
                 final PrintWriter out = response.getWriter();
 88  
 
 89  0
                 out.println("<html>");
 90  0
                 out.println("<head>");
 91  0
                 out
 92  
                                 .println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
 93  0
                 out.println("<title>Cubby Admin Tool</title>");
 94  0
                 out.println("<style>");
 95  0
                 out.println(css);
 96  0
                 out.println("</style>");
 97  0
                 out.println("</head>");
 98  
 
 99  0
                 out.println("<body>");
 100  0
                 out.println("<div id=\"banner\">");
 101  0
                 out.println("<h1>Cubby Admin Tool</h1>");
 102  0
                 out.println("</div>");
 103  
 
 104  0
                 out.println("<div id=\"main\">");
 105  0
                 Section routingSection = new RoutingSection(request, response);
 106  0
                 routingSection.print();
 107  0
                 out.println("</div>");
 108  0
                 out.println("</body>");
 109  0
                 out.println("</html>");
 110  0
                 out.flush();
 111  0
                 out.close();
 112  0
         }
 113  
 
 114  
         private String getResource(final String resourceName)
 115  
                         throws ServletException {
 116  
                 try {
 117  0
                         return new String(getBytes(getClass().getResourceAsStream(
 118  
                                         resourceName)), "UTF-8");
 119  0
                 } catch (final IOException e) {
 120  0
                         throw new ServletException("Can't read resouce file : "
 121  
                                         + resourceName, e);
 122  
                 }
 123  
         }
 124  
 
 125  
         /**
 126  
          * {@link InputStream}からbyteの配列を取得します。
 127  
          * 
 128  
          * @param is
 129  
          * @return byteの配列
 130  
          * @throws IORuntimeException
 131  
          *             {@link IOException}が発生した場合
 132  
          */
 133  
         public static final byte[] getBytes(InputStream is) throws IOException {
 134  0
                 byte[] bytes = null;
 135  0
                 byte[] buf = new byte[8192];
 136  
                 try {
 137  0
                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 138  0
                         int n = 0;
 139  0
                         while ((n = is.read(buf, 0, buf.length)) != -1) {
 140  0
                                 baos.write(buf, 0, n);
 141  
                         }
 142  0
                         bytes = baos.toByteArray();
 143  
                 } finally {
 144  0
                         if (is != null) {
 145  0
                                 is.close();
 146  
                         }
 147  
                 }
 148  0
                 return bytes;
 149  
         }
 150  
 
 151  
 }