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