1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.taglib;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.FieldTypes;
30 import org.dbforms.config.Table;
31
32 import org.dbforms.util.FileHolder;
33 import org.dbforms.util.SqlUtil;
34
35 import java.io.*;
36
37 import java.sql.Connection;
38 import java.sql.PreparedStatement;
39 import java.sql.ResultSet;
40 import java.sql.SQLException;
41
42 import javax.servlet.jsp.*;
43
44 /***
45 * #fixme docu to come
46 *
47 * @author Joe Peer
48 */
49 public class DbBlobContentTag extends AbstractDbBaseHandlerTag implements
50 javax.servlet.jsp.tagext.TryCatchFinally {
51 private static Log logCat = LogFactory.getLog(DbBlobContentTag.class);
52
53 private String dbConnectionName;
54
55 /***
56 * DOCUMENT ME!
57 *
58 * @param string
59 */
60 public void setDbConnectionName(String string) {
61 dbConnectionName = string;
62 }
63
64 /***
65 * DOCUMENT ME!
66 *
67 * @return
68 */
69 public String getDbConnectionName() {
70 return dbConnectionName;
71 }
72
73
74 /***
75 * DOCUMENT ME!
76 *
77 * @return DOCUMENT ME!
78 *
79 * @throws javax.servlet.jsp.JspException
80 * DOCUMENT ME!
81 * @throws IllegalArgumentException
82 * DOCUMENT ME!
83 * @throws JspException
84 * DOCUMENT ME!
85 */
86 public int doEndTag() throws javax.servlet.jsp.JspException {
87 try {
88 if (getParentForm().isFooterReached()) {
89 return EVAL_PAGE;
90 }
91
92 String keyVal = getKeyVal();
93 StringBuffer queryBuf = new StringBuffer();
94 queryBuf.append("SELECT ");
95 queryBuf.append(getField().getName());
96 queryBuf.append(" FROM ");
97 queryBuf.append(getParentForm().getTable().getQueryFrom());
98 queryBuf.append(" WHERE ");
99 queryBuf.append(getParentForm().getTable()
100 .getWhereClauseForKeyFields(keyVal));
101 logCat.info("blobcontent query- " + queryBuf.toString());
102
103 StringBuffer contentBuf = new StringBuffer();
104
105 try {
106 Connection con = getConfig().getConnection(dbConnectionName);
107 PreparedStatement ps = con
108 .prepareStatement(queryBuf.toString());
109 getParentForm().getTable().populateWhereClauseWithKeyFields(
110 keyVal, ps, 1);
111
112 ResultSet rs = ps.executeQuery();
113
114 if (rs.next()) {
115 InputStream is = null;
116 String fileName = null;
117
118 if (getField().getType() == FieldTypes.DISKBLOB) {
119 fileName = rs.getString(1);
120 is = SqlUtil.readDiskBlob(fileName, getField()
121 .getDirectory(), null);
122 } else if (getField().getTable().getBlobHandlingStrategy() == Table.BLOB_CLASSIC) {
123 FileHolder fh = SqlUtil.readFileHolderBlob(rs);
124 is = fh.getInputStreamFromBuffer();
125 } else {
126 is = SqlUtil.readDbFieldBlob(rs);
127 }
128 if (is != null) {
129 try {
130 BufferedReader br = new BufferedReader(
131 new InputStreamReader(is));
132 char[] c = new char[1024];
133 int read;
134
135 while ((read = br.read(c)) != -1) {
136 contentBuf.append(c, 0, read);
137 }
138 } finally {
139 is.close();
140 }
141 }
142 } else {
143 logCat.info("fs- we have got no result" + queryBuf);
144 }
145
146 SqlUtil.closeConnection(con);
147 } catch (SQLException sqle) {
148 sqle.printStackTrace();
149 }
150
151 pageContext.getOut().write(escapeHTML(contentBuf.toString()));
152 } catch (java.io.IOException ioe) {
153 throw new JspException("IO Error: " + ioe.getMessage());
154 }
155
156 return EVAL_PAGE;
157 }
158
159 /***
160 * DOCUMENT ME!
161 */
162 public void doFinally() {
163 dbConnectionName = null;
164 super.doFinally();
165 }
166
167
168
169
170 /***
171 * DOCUMENT ME!
172 *
173 * @return DOCUMENT ME!
174 */
175 private String getKeyVal() {
176 return getParentForm().getTable().getKeyPositionString(
177 getParentForm().getResultSetVector());
178 }
179 }