Source code: org/hsqldb/Servlet.java
1 /* Copyrights and Licenses
2 *
3 * This product includes Hypersonic SQL.
4 * Originally developed by Thomas Mueller and the Hypersonic SQL Group.
5 *
6 * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice, this list of conditions
10 * and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice, this list of
12 * conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * - All advertising materials mentioning features or use of this software must display the
15 * following acknowledgment: "This product includes Hypersonic SQL."
16 * - Products derived from this software may not be called "Hypersonic SQL" nor may
17 * "Hypersonic SQL" appear in their names without prior written permission of the
18 * Hypersonic SQL Group.
19 * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
20 * product includes Hypersonic SQL."
21 * This software is provided "as is" and any expressed or implied warranties, including, but
22 * not limited to, the implied warranties of merchantability and fitness for a particular purpose are
23 * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any
24 * direct, indirect, incidental, special, exemplary, or consequential damages (including, but
25 * not limited to, procurement of substitute goods or services; loss of use, data, or profits;
26 * or business interruption). However caused any on any theory of liability, whether in contract,
27 * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
28 * software, even if advised of the possibility of such damage.
29 * This software consists of voluntary contributions made by many individuals on behalf of the
30 * Hypersonic SQL Group.
31 *
32 *
33 * For work added by the HSQL Development Group:
34 *
35 * Copyright (c) 2001-2002, The HSQL Development Group
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
40 *
41 * Redistributions of source code must retain the above copyright notice, this
42 * list of conditions and the following disclaimer, including earlier
43 * license statements (above) and comply with all above license conditions.
44 *
45 * Redistributions in binary form must reproduce the above copyright notice,
46 * this list of conditions and the following disclaimer in the documentation
47 * and/or other materials provided with the distribution, including earlier
48 * license statements (above) and comply with all above license conditions.
49 *
50 * Neither the name of the HSQL Development Group nor the names of its
51 * contributors may be used to endorse or promote products derived from this
52 * software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
55 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
58 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 */
66
67
68 package org.hsqldb;
69
70 import javax.servlet.*;
71 import javax.servlet.http.*;
72 import java.io.*;
73 import java.util.*;
74 import java.sql.SQLException;
75
76 // fredt@users 20020130 - patch 475586 by wreissen@users
77 // fredt@users 20020328 - patch 1.7.0 by fredt - error trapping
78
79 /**
80 * <font color="#009900">
81 * Servlet acts as a interface between the applet and the database for the
82 * the client / server mode of HSQL Database Engine. It is not required if
83 * the included HSQL Database Engine WebServer is used, but if another
84 * HTTP server is used. The HTTP Server must support the Servlet API.
85 * <br>
86 * This class should not be used directly by the application. It will be
87 * called by the HTTP Server. The applet / application should use the
88 * jdbc* classes.
89 * <br>
90 * The database name is taken from the servlet engine (extranal webserver)
91 * property hsqldb.server.database (fredt@users)
92 * <br>
93 * </font>
94 * @version 1.7.0
95 */
96 public class Servlet extends javax.servlet.http.HttpServlet {
97
98 private String sError;
99 private Database dDatabase;
100 private String sDatabase;
101
102 /**
103 * Method declaration
104 *
105 *
106 * @param database
107 */
108 public void init(ServletConfig config) {
109
110 try {
111 super.init(config);
112 } catch (ServletException exp) {
113 log(exp.getMessage());
114 }
115
116 sDatabase = getInitParameter("hsqldb.server.database");
117
118 if (sDatabase == null) {
119 sDatabase = ".";
120 }
121
122 log("Database filename = " + sDatabase);
123
124 try {
125 dDatabase = new Database(sDatabase);
126 } catch (SQLException e) {
127 sError = e.getMessage();
128
129 log(sError);
130 }
131
132 log("Initialization completed.");
133 }
134
135 private static long lModified = 0;
136
137 /**
138 * Method declaration
139 *
140 *
141 * @param req
142 *
143 * @return
144 */
145 protected long getLastModified(HttpServletRequest req) {
146
147 // this is made so that the cache of the http server is not used
148 // maybe there is some other way
149 return lModified++;
150 }
151
152 /**
153 * Method declaration
154 *
155 *
156 * @param request
157 * @param response
158 *
159 * @throws IOException
160 * @throws ServletException
161 */
162 public void doGet(HttpServletRequest request,
163 HttpServletResponse response)
164 throws IOException, ServletException {
165
166 String query = request.getQueryString();
167
168 if ((query == null) || (query.length() == 0)) {
169 response.setContentType("text/html");
170
171 // fredt@users 20020130 - patch 1.7.0 by fredt
172 // to avoid caching on the browser
173 response.setHeader("Pragma", "no-cache");
174
175 PrintWriter out = response.getWriter();
176
177 out.println(
178 "<html><head><title>HSQL Database Engine Servlet</title>");
179 out.println("</head><body><h1>HSQL Database Engine Servlet</h1>");
180 out.println("The servlet is running.<P>");
181
182 if (dDatabase != null) {
183 out.println("The database is also running.<P>");
184 out.println("Database name: " + sDatabase + "<P>");
185 out.println("Queries processed: " + iQueries + "<P>");
186 } else {
187 out.println("<h2>The database is not running!</h2>");
188 out.println("The error message is:<P>");
189 out.println(sError);
190 }
191
192 out.println("</body></html>");
193 }
194 }
195
196 /**
197 * Method declaration
198 *
199 *
200 * @param request
201 * @param response
202 *
203 * @throws IOException
204 * @throws ServletException
205 */
206 public void doPost(HttpServletRequest request,
207 HttpServletResponse response)
208 throws IOException, ServletException {
209
210 ServletInputStream input = request.getInputStream();
211 int len = request.getContentLength();
212 byte b[] = new byte[len];
213
214 input.read(b, 0, len);
215
216 String s = new String(b);
217 int p = s.indexOf('+');
218 int q = s.indexOf('+', p + 1);
219
220 if ((p == -1) || (q == -1)) {
221 doGet(request, response);
222 }
223
224 String user = s.substring(0, p);
225 String password = s.substring(p + 1, q);
226
227 s = s.substring(q + 1);
228
229 try {
230 user = StringConverter.hexStringToUnicode(user);
231 password = StringConverter.hexStringToUnicode(password);
232 s = StringConverter.hexStringToUnicode(s);
233 } catch (SQLException e) {
234 throw new ServletException();
235 }
236
237 response.setContentType("application/octet-stream");
238
239 ServletOutputStream out = response.getOutputStream();
240 byte result[] = dDatabase.execute(user, password, s);
241
242 response.setContentLength(result.length);
243 out.write(result);
244 out.flush();
245 out.close();
246
247 iQueries++;
248
249 // System.out.print("Queries processed: "+iQueries+" \n");
250 }
251
252 static private int iQueries;
253 }