001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.http;
034:
035: import java.io.InputStream;
036: import java.io.IOException;
037: import java.io.FileNotFoundException;
038:
039: import java.util.Properties;
040:
041: import javax.servlet.*;
042: import javax.servlet.http.*;
043:
044: import java.lang.System;
045: import java.util.StringTokenizer;
046:
047: import java.sql.DriverManager;
048: import java.sql.SQLException;
049: import java.sql.Connection;
050: import java.sql.PreparedStatement;
051: import java.sql.ResultSet;
052:
053: import com.knowgate.debug.DebugFile;
054: import com.knowgate.misc.Environment;
055: import com.knowgate.hipergate.QueryByForm;
056:
057: /**
058: * <p>Get Query By Form results</p>
059: * @author Sergio Montoro Ten
060: * @version 1.0
061: * @see com.knowgate.hipergate.QueryByForm
062: */
063:
064: public class HttpQueryServlet extends HttpServlet {
065:
066: // -----------------------------------------------------------
067:
068: private boolean isVoid(String sParam) {
069: if (null == sParam)
070: return true;
071: else
072: return (sParam.length() == 0);
073: }
074:
075: /**
076: * <p>Initialize Servlet Parameters</p>
077: * Take Database Driver, Conenction URL and User from /WEB-INF/web.xml.<br>
078: * If any parameter is not found then look it up at hipergate.cnf Properties
079: * file using Environment singleton.
080: * @throws ServletException
081: * @throws UnavailableException If jdbcDriverClassName parameter is not found
082: * and driver property at hipergate.cnf is not found or if jdbcURL parameter
083: * is not found and dburl property at hipergate.cnf is not found.
084: * @see com.knowgate.misc.Environment
085: */
086: public void init() throws ServletException {
087:
088: ServletConfig config = getServletConfig();
089: jdbcDriverClassName = config
090: .getInitParameter("jdbcDriverClassName");
091: jdbcURL = config.getInitParameter("jdbcURL");
092: dbUserName = config.getInitParameter("dbUserName");
093: dbUserPassword = config.getInitParameter("dbUserPassword");
094:
095: if (isVoid(jdbcDriverClassName) || isVoid(jdbcURL)
096: || isVoid(dbUserName) || isVoid(dbUserPassword)) {
097: Properties env = Environment.getProfile("hipergate");
098:
099: if (isVoid(jdbcDriverClassName))
100: jdbcDriverClassName = env.getProperty("driver");
101:
102: if (isVoid(jdbcURL))
103: jdbcURL = env.getProperty("dburl");
104:
105: if (isVoid(dbUserName))
106: dbUserName = env.getProperty("dbuser");
107:
108: if (isVoid(dbUserPassword))
109: dbUserPassword = env.getProperty("dbpassword");
110: }
111:
112: if (jdbcDriverClassName == null || jdbcURL == null)
113: throw new UnavailableException("Init params missing");
114: } // init()
115:
116: // -----------------------------------------------------------
117:
118: /**
119: * <p>Get Query By Form results throught response output stream.</p>
120: * @param queryspec Name of XML file containing the Query Specification
121: * @param columnlist List of comma separated column names to retrieve
122: * @param where SQL WHERE clause to apply
123: * @param order by SQL ORDER BY clause to apply
124: * @param showas Output format. One of { "CSV" <i>(comma separated)</i>, "TSV" <i>(tabbed separated)</i>, "XLS" <i>(Excel)</i> }
125: * @throws IOException
126: * @throws FileNotFoundException
127: * @throws ServletException
128: */
129: public void doGet(HttpServletRequest request,
130: HttpServletResponse response) throws IOException,
131: FileNotFoundException, ServletException {
132: Class oDriver;
133: Connection oConn = null;
134: ServletOutputStream oOut = response.getOutputStream();
135: QueryByForm oQBF;
136: String sQuerySpec;
137: String sColumnList;
138: String sWhere;
139: String sOrderBy;
140: String sShowAs;
141: String sStorage;
142:
143: if (DebugFile.trace) {
144: DebugFile.writeln("Begin HttpQueryServlet.doGet(...)");
145: DebugFile.incIdent();
146: }
147:
148: sStorage = Environment.getProfileVar("hipergate", "storage");
149:
150: if (DebugFile.trace)
151: DebugFile.writeln("storage=" + sStorage);
152:
153: try {
154: oDriver = Class.forName(jdbcDriverClassName);
155: } catch (ClassNotFoundException ignore) {
156: oDriver = null;
157: if (DebugFile.trace)
158: DebugFile.writeln("Class.forName("
159: + jdbcDriverClassName + ") : "
160: + ignore.getMessage());
161: response.sendError(
162: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
163: "Database driver not found");
164: }
165:
166: if (null == oDriver)
167: return;
168:
169: try {
170: if (DebugFile.trace)
171: DebugFile.writeln("DriverManager.getConnection("
172: + jdbcURL + ",...)");
173:
174: oConn = DriverManager.getConnection(jdbcURL, dbUserName,
175: dbUserPassword);
176:
177: sQuerySpec = request.getParameter("queryspec");
178: sColumnList = request.getParameter("columnlist");
179: if (null == sColumnList)
180: sColumnList = "*";
181: sWhere = request.getParameter("where");
182: if (null == sWhere)
183: sWhere = "1=1";
184: sOrderBy = request.getParameter("orderby");
185: if (null == sOrderBy)
186: sOrderBy = "";
187: sShowAs = request.getParameter("showas");
188: if (null == sShowAs)
189: sShowAs = "CSV";
190:
191: if (DebugFile.trace)
192: DebugFile
193: .writeln("queryspec=" + sQuerySpec != null ? sQuerySpec
194: : "null");
195: if (DebugFile.trace)
196: DebugFile.writeln("where=" + sWhere);
197: if (DebugFile.trace)
198: DebugFile.writeln("orderby=" + sOrderBy);
199:
200: oQBF = new QueryByForm("file://" + sStorage + "/qbf/"
201: + sQuerySpec + ".xml");
202:
203: // Send some basic http headers to support binary d/l.
204: if (sShowAs.equalsIgnoreCase("XLS")) {
205: response.setContentType("application/x-msexcel");
206: response.setHeader("Content-Disposition",
207: "inline; filename=\""
208: + oQBF.getTitle(request.getLocale()
209: .getLanguage()) + " 1.csv\"");
210: } else if (sShowAs.equalsIgnoreCase("CSV")) {
211: response.setContentType("text/plain");
212: response.setHeader("Content-Disposition",
213: "attachment; filename=\""
214: + oQBF.getTitle(request.getLocale()
215: .getLanguage()) + " 1.csv\"");
216: } else if (sShowAs.equalsIgnoreCase("TSV")) {
217: response.setContentType("text/tab-separated-values");
218: response.setHeader("Content-Disposition",
219: "attachment; filename=\""
220: + oQBF.getTitle(request.getLocale()
221: .getLanguage()) + " 1.tsv\"");
222: } else {
223: response.setContentType("text/plain");
224: response.setHeader("Content-Disposition",
225: "inline; filename=\""
226: + oQBF.getTitle(request.getLocale()
227: .getLanguage()) + " 1.txt\"");
228: }
229:
230: if (0 == sOrderBy.length())
231: oQBF.queryToStream(oConn, sColumnList, oQBF
232: .getBaseFilter(request)
233: + " " + sWhere, oOut, sShowAs);
234: else
235: oQBF.queryToStream(oConn, sColumnList, oQBF
236: .getBaseFilter(request)
237: + " " + sWhere + " ORDER BY " + sOrderBy, oOut,
238: sShowAs);
239:
240: oConn.close();
241: oConn = null;
242:
243: oOut.flush();
244: } catch (SQLException e) {
245: if (DebugFile.trace)
246: DebugFile.writeln("SQLException " + e.getMessage());
247: response.sendError(
248: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
249: .getMessage());
250: } catch (ClassNotFoundException e) {
251: if (DebugFile.trace)
252: DebugFile.writeln("ClassNotFoundException "
253: + e.getMessage());
254: response.sendError(
255: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
256: .getMessage());
257: } catch (IllegalAccessException e) {
258: if (DebugFile.trace)
259: DebugFile.writeln("IllegalAccessException "
260: + e.getMessage());
261: response.sendError(
262: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
263: .getMessage());
264: } catch (Exception e) {
265: if (DebugFile.trace)
266: DebugFile.writeln("Exception " + e.getMessage());
267: response.sendError(
268: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
269: .getMessage());
270: } finally {
271: try {
272: if (null != oConn)
273: if (!oConn.isClosed())
274: oConn.close();
275: } catch (SQLException e) {
276: if (DebugFile.trace)
277: DebugFile.writeln("SQLException " + e.getMessage());
278: }
279: }
280:
281: if (DebugFile.trace) {
282: DebugFile.decIdent();
283: DebugFile.writeln("End HttpQueryServlet.doGet()");
284: }
285: } // doGet()
286:
287: // **********************************************************
288: // * Private Variables
289:
290: private String jdbcDriverClassName;
291: private String jdbcURL;
292: private String dbUserName;
293: private String dbUserPassword;
294:
295: }
|