KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > BlobOutputPGSQL


1 package dinamica;
2
3 import java.sql.*;
4 import javax.servlet.ServletOutputStream JavaDoc;
5 import javax.sql.DataSource JavaDoc;
6
7 /**
8  * Output module to print blob contents stored in PostgreSQL database (v8.0.3),
9  * like images, pdfs and other types of documents
10  * saved in columns of type "bytea". This class was required
11  * because PostgreSQL does implement the standard JDBC BLOB API for
12  * its "bytea" data type, so the default dinamica.BlobOutput cannot be used
13  * with PostgreSQL databases in the current stable versions (7.4 and 8.0.3).
14  * <br>
15  * Creation date: 2-sept-2005<br>
16  * Last Update: 2-sept-2005<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 BlobOutputPGSQL 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         //get datasource object
31
String JavaDoc jndiPrefix = getContext().getInitParameter("jndi-prefix");
32         String JavaDoc dataSourceName = getContext().getInitParameter("def-datasource");
33                 
34         /* PATCH 2005-03-10 read datasource name from config.xml if available */
35         if (getConfig().transDataSource!=null)
36             dataSourceName = getConfig().transDataSource;
37         
38         if (jndiPrefix==null)
39             jndiPrefix="";
40         
41         DataSource JavaDoc ds = Jndi.getDataSource(jndiPrefix + dataSourceName);
42         
43         Connection conn = null;
44         Statement s = null;
45         ResultSet rs = null;
46         
47         ServletOutputStream JavaDoc out = null;
48         
49         try
50         {
51             //connect to database
52
conn = ds.getConnection();
53             s = conn.createStatement();
54
55             //get recordset with blob metadata
56
Recordset info = data.getRecordset("blobinfo");
57             
58             //get sql to retrieve blob
59
String JavaDoc sql = info.getString("sql");
60
61             //set BLOB content-type
62
getResponse().setContentType(info.getString("format"));
63
64             //attach?
65
String JavaDoc fileName = info.getString("filename");
66             if (fileName!=null)
67             {
68                 getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
69             }
70
71             //get servlet output stream
72
out = getResponse().getOutputStream();
73
74             //execute query and retrieve blob
75
rs = s.executeQuery(sql);
76             if (rs.next())
77             {
78                 //read blob
79
byte[] blob = rs.getBytes(1);
80
81                 //set content length
82
int size = (int)blob.length;
83                 getResponse().setContentLength(size);
84                 out.write(blob);
85             }
86
87         }
88         catch (Throwable JavaDoc e)
89         {
90             throw e;
91         }
92         finally
93         {
94                 if (rs!=null) rs.close();
95                 if (s!=null) s.close();
96                 if (conn!=null) conn.close();
97                 if (out!=null) out.close();
98         }
99         
100     }
101
102 }
103
Popular Tags