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