Source code: org/hsqldb/HypersonicDBServlet.java
1 package org.hsqldb;
2
3 /*
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.<p>
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.<p>
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
17 * http://www.gnu.org/copyleft/lesser.html
18 */
19
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.ServletConfig;
22 import javax.servlet.ServletException;
23 import java.io.StringWriter;
24 import java.io.PrintWriter;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27 import java.io.IOException;
28 import org.hsqldb.Database;
29 import java.sql.SQLException;
30 import javax.servlet.ServletInputStream;
31 import javax.servlet.ServletOutputStream;
32
33 // Configuration Management Information:
34 // -------------------------------------
35 // $Id: HypersonicDBServlet.java,v 1.5 2001/10/27 17:15:37 wreissen Exp $
36 //
37 // Version History:
38 // ----------------
39 // $Log: HypersonicDBServlet.java,v $
40 // Revision 1.5 2001/10/27 17:15:37 wreissen
41 // javadoc improved
42 //
43 // Revision 1.4 2001/10/14 18:45:47 wreissen
44 // the stack trace is printed preformatted.
45 //
46 // Revision 1.3 2001/07/01 06:46:31 wreissen
47 // fixed errors in DOS/Unix-translation.
48 //
49 // Revision 1.1 2001/05/29 18:23:56 wreissen
50 // updated to version 1.60 of Hypersonic SQL
51 //
52 // Revision 1.1 2001/02/15 07:43:51 wreissen
53 // initial version, copied from WebGlossary.
54 //
55 // Revision 1.1 2000/11/13 13:53:25 wreissen
56 // Rebuilt of org.hsql.Servlet due to design weaknesses in the
57 // original source.
58 //
59 //
60 // ***********************************************************************************
61 /**
62 * <p>Database Servlet handling the access to the
63 * <a href="http://sourceforge.net/projects/hsqldb/">HyperSonic SQL</a>
64 * database. Unfortunately, in the current version 1.6.0 of
65 * org.hsqldb.Serlvet, the database filename is not configurable, but it
66 * has to be set fixed in the constructor. This does not fit to the
67 * servlet API. Hence, we import the sourcecode of
68 * <code>org.hsqldb.Serlvet</code> in the hope, that this will be
69 * fixed in the future.</p>
70 *
71 *
72 * Created: Sun Nov 12 07:41:51 2000
73 *
74 * @author Wolfgang Reissenberger
75 * @version $Revision: 1.5 $
76 */
77
78 public class HypersonicDBServlet extends HttpServlet {
79
80
81 private Database database;
82 private String filename;
83
84 static private String sError = null;
85 static private long iQueries = 0;
86
87
88
89 /**
90 * Initializes the servlet. The following parameters are
91 * recognized:
92 * <ul>
93 * <li> <b>filename</b>: local filename of the database.
94 * </ul>
95 *
96 * @param config servlet configuration
97 */
98 public void init(ServletConfig config) {
99 try {
100 super.init(config);
101 } catch (ServletException exp) {
102 logException(exp);
103 }
104
105 filename = getInitParameter("filename");
106 if (filename == null) filename = ".";
107
108 log("Database filename = " + filename);
109
110 try {
111 database = new Database(filename);
112 } catch (SQLException e) {
113 sError = showStackTrace(e);
114 log(sError);
115 }
116
117
118 log("Initialization completed.");
119 }
120
121
122 /**
123 * Delivers the database status. This method is a 1:1 copy from
124 * <code>org.hsqldb.Serlvet</code>.
125 *
126 * @param request HTTP request
127 * @param response HTTP response
128 * @exception ServletException if an error occurs
129 * @exception IOException if an error occurs
130 */
131 public void doGet(HttpServletRequest request, HttpServletResponse response)
132 throws IOException, ServletException {
133 String query=request.getQueryString();
134 if(query=="" || query==null) {
135 response.setContentType("text/html");
136 PrintWriter out=response.getWriter();
137 out.println("<html><head><title>Hypersonic SQL Servlet</title>");
138 out.println("</head><body><h1>Hypersonic SQL Servlet</h1>");
139 out.println("The servlet is running.<P>");
140 if(database!=null) {
141 out.println("The database is also running.<P>");
142 out.println("Database name: "+filename+"<P>");
143 out.println("Queries processed: "+iQueries+"<P>");
144 } else {
145 out.println("<h2>The database is not running!</h2>");
146 out.println("The error message is:<P>");
147 out.println("<pre>\n" + sError + "\n</pre>");
148 }
149 out.println("</body></html>");
150 }
151 }
152
153
154
155 /**
156 * Handles the database access. The request should be of the form: <p>
157 *
158 * <user>+<password>+<SQL command><p>
159 *
160 * This method is a 1:1 copy from <code>org.hsqldb.Serlvet</code>.
161 *
162 * @param request HTTP request
163 * @param response HTTP response
164 * @exception ServletException if an error occurs
165 * @exception IOException if an error occurs
166 */
167 public void doPost(HttpServletRequest request,
168 HttpServletResponse response)
169 throws IOException, ServletException {
170 ServletInputStream input=request.getInputStream();
171 int len=request.getContentLength();
172 byte b[]=new byte[len];
173 input.read(b,0,len);
174 String s=new String(b);
175 int p=s.indexOf('+');
176 int q=s.indexOf('+',p+1);
177 if(p==-1 || q==-1) {
178 doGet(request,response);
179 }
180 String user=s.substring(0,p);
181 user=StringConverter.hexStringToUnicode(user);
182 String password=s.substring(p+1,q);
183 password=StringConverter.hexStringToUnicode(password);
184 s=s.substring(q+1);
185 s=StringConverter.hexStringToUnicode(s);
186 response.setContentType("application/octet-stream");
187 ServletOutputStream out=response.getOutputStream();
188 byte result[]=database.execute(user,password,s);
189 response.setContentLength(result.length);
190 out.write(result);
191 out.flush();
192 out.close();
193 iQueries++;
194 }
195
196
197 private static long lModified=0;
198 /**
199 * Always delivers a new integer value. This is made so that the
200 * cache of the http server is not used maybe there is some other way.
201 *
202 * @param req servlet request
203 * @return a new integer value.
204 */
205 protected long getLastModified(HttpServletRequest req) {
206 // this is made so that the cache of the http server is not used
207 // maybe there is some other way
208 return lModified++;
209 }
210
211 /**
212 * Write the exception stacktrace to the log file of the
213 * servlet engine.
214 *
215 * @param e the exception to be logged.
216 */
217 protected void logException(Exception e) {
218 log(showStackTrace(e));
219 }
220
221
222 /**
223 * Converts the stack trace of the exception to a single
224 * String.
225 *
226 * @param e the exception
227 */
228 private String showStackTrace(Exception e) {
229 StringWriter writer = new StringWriter();
230 e.printStackTrace(new PrintWriter(writer));
231
232 return(writer.toString());
233 }
234 } // HypersonicDBServlet