001: package dinamica;
002:
003: import java.io.BufferedInputStream;
004: import java.io.IOException;
005: import java.sql.*;
006:
007: import javax.servlet.ServletOutputStream;
008: import javax.sql.DataSource;
009:
010: /**
011: * Generic output module to print blob contents,
012: * like images, pdfs and other types of documents
013: * saved in database columns of type BLOB or equivalent.
014: * <br>
015: * Creation date: 6-jan-2004<br>
016: * Last Update: 20-may-2004<br>
017: * (c) 2003 Martin Cordova<br>
018: * This code is released under the LGPL license<br>
019: * @author Martin Cordova
020: * */
021: public class BlobOutput extends GenericOutput {
022:
023: /* (non-Javadoc)
024: * @see dinamica.GenericOutput#print(dinamica.GenericTransaction)
025: */
026: public void print(GenericTransaction data) throws Throwable {
027:
028: final int BUFFER_SIZE = 8192;
029:
030: //get datasource object
031: String jndiPrefix = getContext()
032: .getInitParameter("jndi-prefix");
033: String dataSourceName = getContext().getInitParameter(
034: "def-datasource");
035:
036: /* PATCH 2005-03-10 read datasource name from config.xml if available */
037: if (getConfig().transDataSource != null)
038: dataSourceName = getConfig().transDataSource;
039:
040: if (jndiPrefix == null)
041: jndiPrefix = "";
042:
043: DataSource ds = Jndi.getDataSource(jndiPrefix + dataSourceName);
044:
045: Connection conn = null;
046: Statement s = null;
047: ResultSet rs = null;
048:
049: BufferedInputStream buf = null;
050: ServletOutputStream out = null;
051:
052: try {
053: //connect to database
054: conn = ds.getConnection();
055: s = conn.createStatement();
056:
057: //get recordset with blob metadata
058: Recordset info = data.getRecordset("blobinfo");
059:
060: //get sql to retrieve blob
061: String sql = info.getString("sql");
062:
063: //set BLOB content-type
064: getResponse().setContentType(info.getString("format"));
065:
066: //attach?
067: String fileName = info.getString("filename");
068: if (fileName != null) {
069: getResponse().setHeader("Content-Disposition",
070: "attachment; filename=\"" + fileName + "\";");
071: }
072:
073: //get servlet output stream
074: out = getResponse().getOutputStream();
075:
076: //execute query and retrieve blob
077: rs = s.executeQuery(sql);
078: if (rs.next()) {
079: //get reference to BLOB column - must be the only one retrieved!
080: Blob blob = rs.getBlob(1);
081:
082: //set content length
083: int size = (int) blob.length();
084: getResponse().setContentLength(size);
085:
086: int bytes = 0;
087: byte buffer[] = new byte[BUFFER_SIZE];
088: buf = new BufferedInputStream(blob.getBinaryStream());
089:
090: while (bytes != -1) {
091: bytes = buf.read(buffer);
092: if (bytes > 0)
093: out.write(buffer, 0, bytes);
094: }
095: }
096: } catch (Throwable e) {
097: throw e;
098: } finally {
099:
100: try {
101: if (buf != null)
102: buf.close();
103: } catch (IOException e1) {
104: e1.printStackTrace();
105: }
106:
107: try {
108: if (rs != null)
109: rs.close();
110: } catch (SQLException e2) {
111: e2.printStackTrace();
112: }
113:
114: try {
115: if (s != null)
116: s.close();
117: } catch (SQLException e3) {
118: e3.printStackTrace();
119: }
120:
121: try {
122: if (conn != null)
123: conn.close();
124: } catch (SQLException e4) {
125: e4.printStackTrace();
126: }
127:
128: try {
129: if (out != null) {
130: out.close();
131: }
132: } catch (IOException e5) {
133: e5.printStackTrace();
134: }
135:
136: }
137:
138: }
139:
140: }
|