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