1 23 24 package org.dbforms.servlets; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.config.DbFormsConfig; 30 import org.dbforms.config.DbFormsConfigRegistry; 31 import org.dbforms.config.Field; 32 import org.dbforms.config.FieldTypes; 33 import org.dbforms.config.Table; 34 35 import org.dbforms.util.SqlUtil; 36 import org.dbforms.util.StringUtil; 37 import org.dbforms.util.Util; 38 import org.dbforms.util.FileHolder; 39 40 import java.io.IOException ; 41 import java.io.InputStream ; 42 43 import java.sql.Connection ; 44 import java.sql.PreparedStatement ; 45 import java.sql.ResultSet ; 46 import java.sql.SQLException ; 47 48 import javax.servlet.ServletException ; 49 import javax.servlet.ServletOutputStream ; 50 import javax.servlet.http.HttpServlet ; 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.http.HttpServletResponse ; 53 54 59 public class FileServlet extends HttpServlet { 60 private static Log logCat = LogFactory.getLog(FileServlet.class.getName()); 62 63 76 public void doGet(HttpServletRequest request, HttpServletResponse response) 77 throws ServletException , IOException { 78 79 DbFormsConfig config = null; 83 try { 84 config = DbFormsConfigRegistry.instance().lookup(); 85 } catch (Exception e) { 86 logCat.error(e); 87 throw new ServletException (e); 88 } 89 90 try { 91 String tf = request.getParameter("tf"); 92 String keyValuesStr = request.getParameter("keyval"); 93 if (!Util.isNull(keyValuesStr) && !("null".equals(keyValuesStr))) { 94 95 int tableId = Integer.parseInt(StringUtil.getEmbeddedString(tf, 96 0, '_')); 97 Table table = config.getTable(tableId); 98 int fieldId = Integer.parseInt(StringUtil.getEmbeddedString(tf, 99 1, '_')); 100 Field field = table.getField(fieldId); 101 102 StringBuffer queryBuf = new StringBuffer (); 103 String dbConnectionName = request.getParameter("invname_" 104 + tableId); 105 Connection con = config.getConnection(dbConnectionName); 106 107 String nameField = request.getParameter("nf"); 109 110 InputStream is = null; 111 String fileName = null; 112 113 queryBuf.append("SELECT "); 114 queryBuf.append(field.getName()); 115 116 if (nameField != null) { 117 queryBuf.append(", "); 118 queryBuf.append(nameField); 119 } 120 121 queryBuf.append(" FROM "); 122 queryBuf.append(table.getName()); 123 queryBuf.append(" WHERE "); 124 queryBuf.append(table.getWhereClauseForKeyFields()); 125 126 logCat.info("::doGet - query is [" + queryBuf + "]"); 129 130 PreparedStatement ps = con 131 .prepareStatement(queryBuf.toString()); 132 table.populateWhereClauseWithKeyFields(keyValuesStr, ps, 1); 133 134 ResultSet rs = ps.executeQuery(); 135 136 if (rs.next()) { 137 if (field.getType() == FieldTypes.DISKBLOB) { 139 fileName = rs.getString(1); 140 is = SqlUtil.readDiskBlob(fileName, field 141 .getDirectory(), request 142 .getParameter("defaultValue")); 143 } 144 else if (field.getType() == FieldTypes.BLOB) { 146 if (nameField != null) { 148 fileName = rs.getString(2); 149 is = SqlUtil.readDbFieldBlob(rs); 150 } else { 151 FileHolder fh = SqlUtil.readFileHolderBlob(rs); 152 is = fh.getInputStreamFromBuffer(); 153 fileName = fh.getFileName(); 154 } 155 } 156 } else { 157 logCat.info("::doGet - we have got no result using query " 158 + queryBuf); 159 } 160 161 if (is != null) { 162 writeToClient(request, response, fileName, is); 163 } 164 165 SqlUtil.closeConnection(con); 166 } 167 } catch (SQLException sqle) { 168 logCat.error("::doGet - SQL exception", sqle); 169 } 170 } 171 172 185 public void doPost(HttpServletRequest request, HttpServletResponse response) 186 throws ServletException , IOException { 187 doGet(request, response); 188 } 189 190 205 private void writeToClient(HttpServletRequest request, 206 HttpServletResponse response, String fileName, InputStream is) 207 throws IOException { 208 String contentType = request.getSession().getServletContext() 209 .getMimeType(fileName); 210 logCat.info("::writeToClient- writing to client:" + fileName + " ct=" 211 + contentType); 212 213 if (!Util.isNull(contentType)) { 214 response.setContentType(contentType); 215 } 216 217 response.setHeader("Cache-control", "private"); 219 response.setHeader("Content-Disposition", "attachment; fileName=\"" 221 + fileName + "\""); 222 223 ServletOutputStream out = response.getOutputStream(); 224 byte[] b = new byte[1024]; 225 int read; 226 227 while ((read = is.read(b)) != -1) 228 out.write(b, 0, read); 229 230 out.close(); 231 } 232 } 233 | Popular Tags |