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: import javax.servlet.*;
039: import javax.servlet.http.*;
040:
041: import java.lang.System;
042: import java.util.StringTokenizer;
043: import java.util.Properties;
044:
045: import java.sql.DriverManager;
046: import java.sql.SQLException;
047: import java.sql.Connection;
048: import java.sql.PreparedStatement;
049: import java.sql.ResultSet;
050:
051: import com.knowgate.debug.DebugFile;
052: import com.knowgate.misc.Environment;
053:
054: /**
055: * <p>Send LONGVARBINARY database field to HttpServletResponse OutputStream</p>
056: * @author Sergio Montoro ten
057: * @version 2.0
058: */
059:
060: public class HttpBLOBServlet extends HttpServlet {
061:
062: // -----------------------------------------------------------
063:
064: private boolean isVoid(String sParam) {
065: if (null == sParam)
066: return true;
067: else
068: return (sParam.length() == 0);
069: }
070:
071: /**
072: * <p>Initialize Servlet Parameters</p>
073: * Take Database Driver, Conenction URL and User from /WEB-INF/web.xml.<br>
074: * If any parameter is not found then look it up at hipergate.cnf Properties
075: * file using Environment singleton.
076: * @throws ServletException
077: * @throws UnavailableException If jdbcDriverClassName parameter is not found
078: * and driver property at hipergate.cnf is not found or if jdbcURL parameter
079: * is not found and dburl property at hipergate.cnf is not found.
080: * @see com.knowgate.misc.Environment
081: */
082:
083: public void init() throws ServletException {
084: ServletConfig config = getServletConfig();
085:
086: jdbcDriverClassName = config
087: .getInitParameter("jdbcDriverClassName");
088: jdbcURL = config.getInitParameter("jdbcURL");
089: dbUserName = config.getInitParameter("dbUserName");
090: dbUserPassword = config.getInitParameter("dbUserPassword");
091:
092: if (isVoid(jdbcDriverClassName) || isVoid(jdbcURL)
093: || isVoid(dbUserName) || isVoid(dbUserPassword)) {
094: Properties env = Environment.getProfile("hipergate");
095:
096: if (isVoid(jdbcDriverClassName))
097: jdbcDriverClassName = env.getProperty("driver");
098:
099: if (isVoid(jdbcURL))
100: jdbcURL = env.getProperty("dburl");
101:
102: if (isVoid(dbUserName))
103: dbUserName = env.getProperty("dbuser");
104:
105: if (isVoid(dbUserPassword))
106: dbUserPassword = env.getProperty("dbpassword");
107: }
108:
109: if (jdbcDriverClassName == null || jdbcURL == null) {
110: throw new UnavailableException("Init params missing");
111: }
112: } // init()
113:
114: // -----------------------------------------------------------
115:
116: /**
117: * <p>Send LONGVARBINARY database field to HttpServletResponse OutputStream</p>
118: * @param nm_table Name of table holding longvarbinary field.
119: *
120: * @param response
121: * @throws IOException
122: * @throws FileNotFoundException
123: * @throws ServletException
124: */
125: public void doGet(HttpServletRequest request,
126: HttpServletResponse response) throws IOException,
127: FileNotFoundException, ServletException {
128: boolean bFound;
129: String sSQL;
130: Class oDriver;
131: Connection oConn = null;
132: PreparedStatement oStmt;
133: ResultSet oRSet;
134: InputStream oBlob = null;
135: int iOffset;
136: int iReaded;
137: int iPar;
138: StringTokenizer oStrTok;
139:
140: if (DebugFile.trace) {
141: DebugFile.writeln("Begin HttpBLOBServlet().doGet");
142: DebugFile.incIdent();
143: }
144:
145: try {
146: oDriver = Class.forName(jdbcDriverClassName);
147: } catch (ClassNotFoundException ignore) {
148: oDriver = null;
149: if (DebugFile.trace)
150: DebugFile.writeln("Class.forName("
151: + jdbcDriverClassName + ") : "
152: + ignore.getMessage());
153: response.sendError(
154: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
155: "Database driver not found");
156: }
157:
158: if (null == oDriver)
159: return;
160:
161: ServletOutputStream oOut = response.getOutputStream();
162:
163: byte oBuffer[] = new byte[4004];
164:
165: try {
166: if (DebugFile.trace)
167: DebugFile.writeln("DriverManager.getConnection("
168: + jdbcURL + ",...)");
169:
170: oConn = DriverManager.getConnection(jdbcURL, dbUserName,
171: dbUserPassword);
172:
173: if (DebugFile.trace)
174: DebugFile
175: .writeln("pk_field = "
176: + (request.getParameter("pk_field") != null ? request
177: .getParameter("pk_field")
178: : "null"));
179:
180: oStrTok = new StringTokenizer(request
181: .getParameter("pk_field"), ",");
182:
183: sSQL = "";
184: while (oStrTok.hasMoreTokens()) {
185: sSQL += (sSQL.length() == 0 ? " WHERE " : " AND ");
186: sSQL += oStrTok.nextToken() + "=?";
187: } // wend
188: sSQL = "SELECT " + request.getParameter("nm_field") + ","
189: + request.getParameter("bin_field") + " FROM "
190: + request.getParameter("nm_table") + sSQL;
191:
192: if (DebugFile.trace)
193: DebugFile.writeln("Connection.prepareStatement(" + sSQL
194: + ")");
195:
196: oStmt = oConn.prepareStatement(sSQL);
197:
198: iPar = 0;
199: oStrTok = new StringTokenizer(request
200: .getParameter("pk_value"), ",");
201: while (oStrTok.hasMoreTokens()) {
202: oStmt.setString(++iPar, oStrTok.nextToken());
203: }
204:
205: if (DebugFile.trace)
206: DebugFile.writeln("PreparedStatement.executeQuery()");
207:
208: oRSet = oStmt.executeQuery();
209: bFound = oRSet.next();
210:
211: String sFileName = null;
212:
213: if (bFound) {
214:
215: sFileName = oRSet.getString(1);
216:
217: if (DebugFile.trace) {
218: DebugFile
219: .writeln("response.setContentType(\"application/octet-stream\")");
220: DebugFile
221: .writeln("response.setHeader(\"Content-Disposition\", \"inline; filename=\""
222: + sFileName + "\"");
223: }
224:
225: // Send some basic http headers to support binary d/l.
226: response.setContentType("application/octet-stream");
227: response.setHeader("Content-Disposition",
228: "inline; filename=\"" + sFileName + "\"");
229:
230: if (DebugFile.trace)
231: DebugFile.writeln("ResultSet.getBinaryStream(2)");
232:
233: oBlob = oRSet.getBinaryStream(2);
234: iOffset = 0;
235: do {
236: iReaded = oBlob.read(oBuffer, 0, 4000);
237: if (iReaded > 0)
238: oOut.write(oBuffer, 0, iReaded);
239: iOffset += iReaded;
240: } while (4000 == iReaded);
241:
242: if (DebugFile.trace)
243: DebugFile
244: .writeln("response.getOutputStream().flush()");
245:
246: oOut.flush();
247: oBlob.close();
248: oBlob = null;
249: } // fi (bFound)
250:
251: oRSet.close();
252:
253: if (!bFound) {
254: if (DebugFile.trace)
255: DebugFile
256: .writeln("FileNotFoundException: Cannot find requested document");
257:
258: response.sendError(HttpServletResponse.SC_NOT_FOUND,
259: "Cannot find requested document");
260: }
261:
262: oConn.close();
263: oConn = null;
264: } catch (SQLException e) {
265: bFound = false;
266: if (oBlob != null)
267: oBlob.close();
268:
269: if (DebugFile.trace)
270: DebugFile.writeln("SQLException: " + e.getMessage());
271:
272: response.sendError(
273: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
274: .getMessage());
275: }
276: try {
277: if (null != oConn)
278: if (!oConn.isClosed())
279: oConn.close();
280: } catch (SQLException e) {
281: }
282:
283: if (DebugFile.trace) {
284: DebugFile.decIdent();
285: DebugFile.writeln("End HttpBLOBServlet().doGet()");
286: }
287: } // doGet()
288:
289: // **********************************************************
290: // * Variables privadas
291:
292: private String jdbcDriverClassName;
293: private String jdbcURL;
294: private String dbUserName;
295: private String dbUserPassword;
296: }
|