001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.server.web;
007:
008: import java.io.IOException;
009: import java.net.InetAddress;
010: import java.net.UnknownHostException;
011: import java.sql.SQLException;
012: import java.util.ArrayList;
013: import java.util.Enumeration;
014: import java.util.Properties;
015:
016: import javax.servlet.ServletConfig;
017: import javax.servlet.ServletException;
018: import javax.servlet.ServletOutputStream;
019: import javax.servlet.http.HttpServlet;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.h2.util.StringUtils;
024:
025: /**
026: * This servlet lets the H2 Console be used in a standard servlet container
027: * such as Tomcat or Jetty.
028: */
029: public class WebServlet extends HttpServlet {
030:
031: private static final long serialVersionUID = 9171446624885086692L;
032: private WebServer server;
033:
034: public void init() throws ServletException {
035: ServletConfig config = getServletConfig();
036: Enumeration en = config.getInitParameterNames();
037: ArrayList list = new ArrayList();
038: while (en.hasMoreElements()) {
039: String name = en.nextElement().toString();
040: String value = config.getInitParameter(name);
041: if (!name.startsWith("-")) {
042: name = "-" + name;
043: }
044: list.add(name);
045: list.add(value);
046: }
047: String[] args = new String[list.size()];
048: list.toArray(args);
049: server = new WebServer();
050: try {
051: server.init(args);
052: } catch (Exception e) {
053: throw new ServletException("Init failed", e);
054: }
055: }
056:
057: public void destroy() {
058: }
059:
060: private boolean allow(HttpServletRequest req) {
061: if (server.getAllowOthers()) {
062: return true;
063: }
064: String addr = req.getRemoteAddr();
065: InetAddress address;
066: try {
067: address = InetAddress.getByName(addr);
068: } catch (UnknownHostException e) {
069: return false;
070: }
071: return address.isLoopbackAddress();
072: }
073:
074: private String getAllowedFile(HttpServletRequest req,
075: String requestedFile) {
076: if (!allow(req)) {
077: return "notAllowed.jsp";
078: }
079: if (requestedFile.length() == 0) {
080: return "index.do";
081: }
082: return requestedFile;
083: }
084:
085: protected void doGet(HttpServletRequest req,
086: HttpServletResponse resp) throws IOException {
087: String file = req.getPathInfo();
088: if (file == null) {
089: resp.sendRedirect(req.getRequestURI() + "/");
090: return;
091: } else if (file.startsWith("/")) {
092: file = file.substring(1);
093: }
094: file = getAllowedFile(req, file);
095: byte[] bytes = null;
096: Properties attributes = new Properties();
097: Enumeration en = req.getAttributeNames();
098: while (en.hasMoreElements()) {
099: String name = en.nextElement().toString();
100: String value = req.getAttribute(name).toString();
101: attributes.put(name, value);
102: }
103: en = req.getParameterNames();
104: while (en.hasMoreElements()) {
105: String name = en.nextElement().toString();
106: String value = req.getParameter(name);
107: attributes.put(name, value);
108: }
109: WebSession session = null;
110: String sessionId = attributes.getProperty("jsessionid");
111: if (sessionId != null) {
112: session = server.getSession(sessionId);
113: }
114: WebThread app = new WebThread(null, server);
115: app.setSession(session, attributes);
116: String ifModifiedSince = req.getHeader("if-modified-since");
117:
118: String hostAddr = req.getRemoteAddr();
119: file = app.processRequest(file, hostAddr);
120: session = app.getSession();
121:
122: String mimeType = app.getMimeType();
123: boolean cache = app.getCache();
124:
125: if (cache && server.getStartDateTime().equals(ifModifiedSince)) {
126: resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
127: return;
128: } else {
129: bytes = server.getFile(file);
130: }
131: if (bytes == null) {
132: resp.sendError(HttpServletResponse.SC_NOT_FOUND);
133: try {
134: bytes = StringUtils.utf8Encode("File not found: "
135: + file);
136: } catch (SQLException e) {
137: server.traceError(e);
138: }
139: } else {
140: if (session != null && file.endsWith(".jsp")) {
141: String page = StringUtils.utf8Decode(bytes);
142: page = PageParser.parse(server, page, session.map);
143: try {
144: bytes = StringUtils.utf8Encode(page);
145: } catch (SQLException e) {
146: server.traceError(e);
147: }
148: }
149: resp.setContentType(mimeType);
150: if (!cache) {
151: resp.setHeader("Cache-Control", "no-cache");
152: } else {
153: resp.setHeader("Cache-Control", "max-age=10");
154: resp.setHeader("Last-Modified", server
155: .getStartDateTime());
156: }
157: }
158: if (bytes != null) {
159: ServletOutputStream out = resp.getOutputStream();
160: out.write(bytes);
161: }
162: }
163:
164: protected void doPost(HttpServletRequest req,
165: HttpServletResponse resp) throws IOException {
166: doGet(req, resp);
167: }
168: }
|