KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > BlobOutput


1 package dinamica;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.sql.*;
6
7 import javax.servlet.ServletOutputStream JavaDoc;
8 import javax.sql.DataSource JavaDoc;
9
10 /**
11  * Generic output module to print blob contents,
12  * like images, pdfs and other types of documents
13  * saved in database columns of type BLOB or equivalent.
14  * <br>
15  * Creation date: 6-jan-2004<br>
16  * Last Update: 20-may-2004<br>
17  * (c) 2003 Martin Cordova<br>
18  * This code is released under the LGPL license<br>
19  * @author Martin Cordova
20  * */

21 public class BlobOutput extends GenericOutput
22 {
23
24     /* (non-Javadoc)
25      * @see dinamica.GenericOutput#print(dinamica.GenericTransaction)
26      */

27     public void print(GenericTransaction data) throws Throwable JavaDoc
28     {
29         
30         final int BUFFER_SIZE = 8192;
31         
32         //get datasource object
33
String JavaDoc jndiPrefix = getContext().getInitParameter("jndi-prefix");
34         String JavaDoc dataSourceName = getContext().getInitParameter("def-datasource");
35                 
36         /* PATCH 2005-03-10 read datasource name from config.xml if available */
37         if (getConfig().transDataSource!=null)
38             dataSourceName = getConfig().transDataSource;
39         
40         if (jndiPrefix==null)
41             jndiPrefix="";
42         
43         DataSource JavaDoc ds = Jndi.getDataSource(jndiPrefix + dataSourceName);
44         
45         Connection conn = null;
46         Statement s = null;
47         ResultSet rs = null;
48         
49         BufferedInputStream JavaDoc buf = null;
50         ServletOutputStream JavaDoc out = null;
51         
52         try
53         {
54             //connect to database
55
conn = ds.getConnection();
56             s = conn.createStatement();
57
58             //get recordset with blob metadata
59
Recordset info = data.getRecordset("blobinfo");
60             
61             //get sql to retrieve blob
62
String JavaDoc sql = info.getString("sql");
63
64             //set BLOB content-type
65
getResponse().setContentType(info.getString("format"));
66
67             //attach?
68
String JavaDoc fileName = info.getString("filename");
69             if (fileName!=null)
70             {
71                 getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
72             }
73
74             //get servlet output stream
75
out = getResponse().getOutputStream();
76
77             //execute query and retrieve blob
78
rs = s.executeQuery(sql);
79             if (rs.next())
80             {
81                 //get reference to BLOB column - must be the only one retrieved!
82
Blob blob = rs.getBlob(1);
83
84                 //set content length
85
int size = (int)blob.length();
86                 getResponse().setContentLength(size);
87
88                 int bytes = 0;
89                 byte buffer[] = new byte[BUFFER_SIZE];
90                 buf = new BufferedInputStream JavaDoc( blob.getBinaryStream() );
91                 
92                 while( bytes != -1 )
93                 {
94                     bytes = buf.read(buffer);
95                     if (bytes>0)
96                         out.write(buffer,0,bytes);
97                 }
98             }
99         }
100         catch (Throwable JavaDoc e)
101         {
102             throw e;
103         }
104         finally
105         {
106
107             try
108             {
109                 if (buf!=null) buf.close();
110             }
111             catch (IOException JavaDoc e1)
112             {
113                 e1.printStackTrace();
114             }
115
116             try
117             {
118                 if (rs!=null) rs.close();
119             }
120             catch (SQLException e2)
121             {
122                 e2.printStackTrace();
123             }
124
125             try
126             {
127                 if (s!=null) s.close();
128             }
129             catch (SQLException e3)
130             {
131                 e3.printStackTrace();
132             }
133
134             try
135             {
136                 if (conn!=null) conn.close();
137             }
138             catch (SQLException e4)
139             {
140                 e4.printStackTrace();
141             }
142
143             try
144             {
145                 if (out!=null)
146                 {
147                     out.close();
148                 }
149             }
150             catch (IOException JavaDoc e5)
151             {
152                 e5.printStackTrace();
153             }
154
155         }
156         
157     }
158
159 }
160
Popular Tags