File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\server\web\WebServlet.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.server.web;
8
9 import java.io.IOException;
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.ArrayList;
13 import java.util.Enumeration;
14 import java.util.Properties;
15 import javax.servlet.ServletConfig;
16 import javax.servlet.ServletOutputStream;
17 import javax.servlet.http.HttpServlet;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20 import org.h2.util.New;
21 import org.h2.util.StringUtils;
22
23 /**
24 * This servlet lets the H2 Console be used in a standard servlet container
25 * such as Tomcat or Jetty.
26 */
27 public class WebServlet extends HttpServlet {
28
29 private static final long serialVersionUID = 1L;
30 private transient WebServer server;
31
32 public void init() {
33 ServletConfig config = getServletConfig();
34 Enumeration<?> en = config.getInitParameterNames();
35 ArrayList<String> list = New.arrayList();
36 while (en.hasMoreElements()) {
37 String name = en.nextElement().toString();
38 String value = config.getInitParameter(name);
39 if (!name.startsWith("-")) {
40 name = "-" + name;
41 }
42 list.add(name);
43 if (value.length() > 0) {
44 list.add(value);
45 }
46 }
47 String[] args = new String[list.size()];
48 list.toArray(args);
49 server = new WebServer();
50 server.setAllowChunked(false);
51 server.init(args);
52 }
53
54 public void destroy() {
55 server.stop();
56 }
57
58 private boolean allow(HttpServletRequest req) {
59 if (server.getAllowOthers()) {
60 return true;
61 }
62 String addr = req.getRemoteAddr();
63 try {
64 InetAddress address = InetAddress.getByName(addr);
65 return address.isLoopbackAddress();
66 } catch (UnknownHostException e) {
67 return false;
68 } catch (NoClassDefFoundError e) {
69 // Google App Engine does not allow java.net.InetAddress
70 return false;
71 }
72 }
73
74 private String getAllowedFile(HttpServletRequest req, String requestedFile) {
75 if (!allow(req)) {
76 return "notAllowed.jsp";
77 }
78 if (requestedFile.length() == 0) {
79 return "index.do";
80 }
81 return requestedFile;
82 }
83
84 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
85 req.setCharacterEncoding("utf-8");
86 String file = req.getPathInfo();
87 if (file == null) {
88 resp.sendRedirect(req.getRequestURI() + "/");
89 return;
90 } else if (file.startsWith("/")) {
91 file = file.substring(1);
92 }
93 file = getAllowedFile(req, file);
94 byte[] bytes = null;
95 Properties attributes = new Properties();
96 Enumeration<?> en = req.getAttributeNames();
97 while (en.hasMoreElements()) {
98 String name = en.nextElement().toString();
99 String value = req.getAttribute(name).toString();
100 attributes.put(name, value);
101 }
102 en = req.getParameterNames();
103 while (en.hasMoreElements()) {
104 String name = en.nextElement().toString();
105 String value = req.getParameter(name);
106 attributes.put(name, value);
107 }
108 WebSession session = null;
109 String sessionId = attributes.getProperty("jsessionid");
110 if (sessionId != null) {
111 session = server.getSession(sessionId);
112 }
113 WebApp app = new WebApp(server);
114 app.setSession(session, attributes);
115 String ifModifiedSince = req.getHeader("if-modified-since");
116
117 String hostAddr = req.getRemoteAddr();
118 file = app.processRequest(file, hostAddr);
119 session = app.getSession();
120
121 String mimeType = app.getMimeType();
122 boolean cache = app.getCache();
123
124 if (cache && server.getStartDateTime().equals(ifModifiedSince)) {
125 resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
126 return;
127 }
128 bytes = server.getFile(file);
129 if (bytes == null) {
130 resp.sendError(HttpServletResponse.SC_NOT_FOUND);
131 bytes = StringUtils.utf8Encode("File not found: " + file);
132 } else {
133 if (session != null && file.endsWith(".jsp")) {
134 String page = StringUtils.utf8Decode(bytes);
135 page = PageParser.parse(page, session.map);
136 bytes = StringUtils.utf8Encode(page);
137 }
138 resp.setContentType(mimeType);
139 if (!cache) {
140 resp.setHeader("Cache-Control", "no-cache");
141 } else {
142 resp.setHeader("Cache-Control", "max-age=10");
143 resp.setHeader("Last-Modified", server.getStartDateTime());
144 }
145 }
146 if (bytes != null) {
147 ServletOutputStream out = resp.getOutputStream();
148 out.write(bytes);
149 }
150 }
151
152 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
153 doGet(req, resp);
154 }
155 }
156