1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
79
80
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
127
128
129
130
131
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 }