1 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 49 public class DbBlobContentTag extends DbBaseHandlerTag implements 50 javax.servlet.jsp.tagext.TryCatchFinally { 51 private static Log logCat = LogFactory.getLog(DbBlobContentTag.class); 52 53 private String dbConnectionName; 54 55 60 public void setDbConnectionName(String string) { 61 dbConnectionName = string; 62 } 63 64 69 public String getDbConnectionName() { 70 return dbConnectionName; 71 } 72 73 76 public void doCatch(Throwable t) throws Throwable { 77 throw t; 78 } 79 80 92 public int doEndTag() throws javax.servlet.jsp.JspException { 93 try { 94 if (getParentForm().isFooterReached()) { 95 return EVAL_PAGE; } 97 98 StringBuffer queryBuf = new StringBuffer (); 99 queryBuf.append("SELECT "); 100 queryBuf.append(getField().getName()); 101 queryBuf.append(" FROM "); 102 queryBuf.append(getParentForm().getTable().getName()); 103 queryBuf.append(" WHERE "); 104 queryBuf.append(getParentForm().getTable() 105 .getWhereClauseForKeyFields()); 106 logCat.info("blobcontent query- " + queryBuf.toString()); 107 108 StringBuffer contentBuf = new StringBuffer (); 109 110 try { 111 Connection con = getConfig().getConnection(dbConnectionName); 112 PreparedStatement ps = con 113 .prepareStatement(queryBuf.toString()); 114 getParentForm().getTable().populateWhereClauseWithKeyFields( 115 getKeyVal(), ps, 1); 116 117 ResultSet rs = ps.executeQuery(); 118 119 if (rs.next()) { 120 InputStream is = null; 121 String fileName = null; 122 123 if (getField().getType() == FieldTypes.DISKBLOB) { 124 fileName = rs.getString(1); 125 is = SqlUtil.readDiskBlob(fileName, getField() 126 .getDirectory(), null); 127 } else if (getField().getTable().getBlobHandlingStrategy() == Table.BLOB_CLASSIC) { 128 FileHolder fh = SqlUtil.readFileHolderBlob(rs); 129 is = fh.getInputStreamFromBuffer(); 130 } else { 131 is = SqlUtil.readDbFieldBlob(rs); 132 } 133 if (is != null) { 134 try { 135 BufferedReader br = new BufferedReader( 136 new InputStreamReader(is)); 137 char[] c = new char[1024]; 138 int read; 139 140 while ((read = br.read(c)) != -1) { 141 contentBuf.append(c, 0, read); 142 } 143 } finally { 144 is.close(); 145 } 146 } 147 } else { 148 logCat.info("fs- we have got no result" + queryBuf); 149 } 150 151 SqlUtil.closeConnection(con); 152 } catch (SQLException sqle) { 153 sqle.printStackTrace(); 154 } 155 156 pageContext.getOut().write(escapeHTML(contentBuf.toString())); 157 } catch (java.io.IOException ioe) { 158 throw new JspException("IO Error: " + ioe.getMessage()); 159 } 160 161 return EVAL_PAGE; 162 } 163 164 167 public void doFinally() { 168 dbConnectionName = null; 169 super.doFinally(); 170 } 171 172 175 180 private String getKeyVal() { 181 return getParentForm().getTable().getKeyPositionString( 182 getParentForm().getResultSetVector()); 183 } 184 } 185 | Popular Tags |