1 32 33 package com.knowgate.http; 34 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 import java.io.FileNotFoundException ; 38 import javax.servlet.*; 39 import javax.servlet.http.*; 40 41 import java.lang.System ; 42 import java.util.StringTokenizer ; 43 import java.util.Properties ; 44 45 import java.sql.DriverManager ; 46 import java.sql.SQLException ; 47 import java.sql.Connection ; 48 import java.sql.PreparedStatement ; 49 import java.sql.ResultSet ; 50 51 import com.knowgate.debug.DebugFile; 52 import com.knowgate.misc.Environment; 53 54 59 60 public class HttpBLOBServlet extends HttpServlet { 61 62 64 private boolean isVoid(String sParam) { 65 if (null==sParam) 66 return true; 67 else 68 return (sParam.length()==0); 69 } 70 71 82 83 public void init() throws ServletException { 84 ServletConfig config = getServletConfig(); 85 86 jdbcDriverClassName = config.getInitParameter("jdbcDriverClassName"); 87 jdbcURL = config.getInitParameter("jdbcURL"); 88 dbUserName = config.getInitParameter("dbUserName"); 89 dbUserPassword = config.getInitParameter("dbUserPassword"); 90 91 if (isVoid(jdbcDriverClassName) || isVoid(jdbcURL) || isVoid(dbUserName) || isVoid(dbUserPassword)) { 92 Properties env = Environment.getProfile("hipergate"); 93 94 if (isVoid(jdbcDriverClassName)) 95 jdbcDriverClassName = env.getProperty("driver"); 96 97 if (isVoid(jdbcURL)) 98 jdbcURL = env.getProperty("dburl"); 99 100 if (isVoid(dbUserName)) 101 dbUserName = env.getProperty("dbuser"); 102 103 if (isVoid(dbUserPassword)) 104 dbUserPassword = env.getProperty("dbpassword"); 105 } 106 107 if (jdbcDriverClassName == null || jdbcURL == null) { 108 throw new UnavailableException("Init params missing"); 109 } 110 } 112 114 123 public void doGet(HttpServletRequest request, HttpServletResponse response) 124 throws IOException , FileNotFoundException , ServletException 125 { 126 boolean bFound; 127 String sSQL; 128 Class oDriver; 129 Connection oConn = null; 130 PreparedStatement oStmt; 131 ResultSet oRSet; 132 InputStream oBlob = null; 133 int iOffset; 134 int iReaded; 135 int iPar; 136 StringTokenizer oStrTok; 137 138 if (DebugFile.trace) { 139 DebugFile.writeln("Begin HttpBLOBServlet().doGet"); 140 DebugFile.incIdent(); 141 } 142 143 try { 144 oDriver = Class.forName(jdbcDriverClassName); 145 } 146 catch (ClassNotFoundException ignore) { 147 oDriver = null; 148 if (DebugFile.trace) DebugFile.writeln("Class.forName(" + jdbcDriverClassName + ") : " + ignore.getMessage()); 149 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database driver not found"); 150 } 151 152 if (null==oDriver) return; 153 154 ServletOutputStream oOut = response.getOutputStream(); 155 156 byte oBuffer[] = new byte[4004]; 157 158 try { 159 if (DebugFile.trace) DebugFile.writeln("DriverManager.getConnection(" + jdbcURL + ",...)"); 160 161 oConn = DriverManager.getConnection(jdbcURL,dbUserName,dbUserPassword); 162 163 if (DebugFile.trace) DebugFile.writeln("pk_field = " + (request.getParameter("pk_field")!=null ? request.getParameter("pk_field") : "null")); 164 165 oStrTok = new StringTokenizer (request.getParameter("pk_field"), ","); 166 167 sSQL = ""; 168 while (oStrTok.hasMoreTokens()) { 169 sSQL += (sSQL.length()==0 ? " WHERE " : " AND "); 170 sSQL += oStrTok.nextToken() + "=?"; 171 } sSQL = "SELECT " + request.getParameter("nm_field") + "," + request.getParameter("bin_field") + " FROM " + request.getParameter("nm_table") + sSQL; 173 174 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")"); 175 176 oStmt = oConn.prepareStatement (sSQL); 177 178 iPar = 0; 179 oStrTok = new StringTokenizer (request.getParameter("pk_value"), ","); 180 while (oStrTok.hasMoreTokens()) { 181 oStmt.setString(++iPar, oStrTok.nextToken()); 182 } 183 184 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()"); 185 186 oRSet = oStmt.executeQuery(); 187 bFound = oRSet.next(); 188 189 String sFileName = null; 190 191 if (bFound) { 192 193 sFileName = oRSet.getString(1); 194 195 if (DebugFile.trace) { 196 DebugFile.writeln("response.setContentType(\"application/octet-stream\")"); 197 DebugFile.writeln("response.setHeader(\"Content-Disposition\", \"inline; filename=\"" + sFileName + "\""); 198 } 199 200 response.setContentType("application/octet-stream"); 202 response.setHeader("Content-Disposition", "inline; filename=\"" + sFileName + "\""); 203 204 if (DebugFile.trace) 205 DebugFile.writeln("ResultSet.getBinaryStream(2)"); 206 207 oBlob = oRSet.getBinaryStream(2); 208 iOffset = 0; 209 do { 210 iReaded = oBlob.read(oBuffer, 0, 4000); 211 if (iReaded>0) 212 oOut.write(oBuffer, 0, iReaded); 213 iOffset += iReaded; 214 } while (4000==iReaded); 215 216 if (DebugFile.trace) 217 DebugFile.writeln("response.getOutputStream().flush()"); 218 219 oOut.flush(); 220 oBlob.close(); 221 oBlob = null; 222 } 224 oRSet.close(); 225 226 if (!bFound) { 227 if (DebugFile.trace) DebugFile.writeln("FileNotFoundException: Cannot find requested document"); 228 229 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot find requested document"); 230 } 231 232 oConn.close(); 233 oConn = null; 234 } 235 catch (SQLException e) { 236 bFound = false; 237 if (oBlob!=null) oBlob.close(); 238 239 if (DebugFile.trace) DebugFile.writeln("SQLException: " + e.getMessage()); 240 241 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 242 } 243 try { if(null!=oConn) if(!oConn.isClosed()) oConn.close(); } catch (SQLException e) { } 244 245 if (DebugFile.trace) { 246 DebugFile.decIdent(); 247 DebugFile.writeln("End HttpBLOBServlet().doGet()"); 248 } 249 } 251 254 private String jdbcDriverClassName; 255 private String jdbcURL; 256 private String dbUserName; 257 private String dbUserPassword; 258 } | Popular Tags |