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.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  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  		super.init(config);
61  		css = getResource("cubby-admin.css");
62  	}
63  
64  	@Override
65  	protected void doGet(final HttpServletRequest request,
66  			final HttpServletResponse response) throws ServletException,
67  			IOException {
68  		final String remoteAddr = request.getRemoteAddr();
69  		if (!isLoopBackAddress(remoteAddr)) {
70  			return;
71  		}
72  		request.setCharacterEncoding("UTF-8");
73  		response.setCharacterEncoding("UTF-8");
74  		render(request, response);
75  	}
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  		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  		final PrintWriter out = response.getWriter();
88  
89  		out.println("<html>");
90  		out.println("<head>");
91  		out
92  				.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
93  		out.println("<title>Cubby Admin Tool</title>");
94  		out.println("<style>");
95  		out.println(css);
96  		out.println("</style>");
97  		out.println("</head>");
98  
99  		out.println("<body>");
100 		out.println("<div id=\"banner\">");
101 		out.println("<h1>Cubby Admin Tool</h1>");
102 		out.println("</div>");
103 
104 		out.println("<div id=\"main\">");
105 		Section routingSection = new RoutingSection(request, response);
106 		routingSection.print();
107 		out.println("</div>");
108 		out.println("</body>");
109 		out.println("</html>");
110 		out.flush();
111 		out.close();
112 	}
113 
114 	private String getResource(final String resourceName)
115 			throws ServletException {
116 		try {
117 			return new String(getBytes(getClass().getResourceAsStream(
118 					resourceName)), "UTF-8");
119 		} catch (final IOException e) {
120 			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 		byte[] bytes = null;
135 		byte[] buf = new byte[8192];
136 		try {
137 			ByteArrayOutputStream baos = new ByteArrayOutputStream();
138 			int n = 0;
139 			while ((n = is.read(buf, 0, buf.length)) != -1) {
140 				baos.write(buf, 0, n);
141 			}
142 			bytes = baos.toByteArray();
143 		} finally {
144 			if (is != null) {
145 				is.close();
146 			}
147 		}
148 		return bytes;
149 	}
150 
151 }