1 32 33 package com.knowgate.http; 34 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 import java.io.FileNotFoundException ; 38 39 import java.util.Properties ; 40 41 import javax.servlet.*; 42 import javax.servlet.http.*; 43 44 import java.lang.System ; 45 import java.util.StringTokenizer ; 46 47 import java.sql.DriverManager ; 48 import java.sql.SQLException ; 49 import java.sql.Connection ; 50 import java.sql.PreparedStatement ; 51 import java.sql.ResultSet ; 52 53 import com.knowgate.debug.DebugFile; 54 import com.knowgate.misc.Environment; 55 import com.knowgate.hipergate.QueryByForm; 56 57 63 64 public class HttpQueryServlet extends HttpServlet { 65 66 68 private boolean isVoid(String sParam) { 69 if (null==sParam) 70 return true; 71 else 72 return (sParam.length()==0); 73 } 74 75 86 public void init() throws ServletException { 87 88 ServletConfig config = getServletConfig(); 89 jdbcDriverClassName = config.getInitParameter("jdbcDriverClassName"); 90 jdbcURL = config.getInitParameter("jdbcURL"); 91 dbUserName = config.getInitParameter("dbUserName"); 92 dbUserPassword = config.getInitParameter("dbUserPassword"); 93 94 if (isVoid(jdbcDriverClassName) || isVoid(jdbcURL) || isVoid(dbUserName) || isVoid(dbUserPassword)) { 95 Properties env = Environment.getProfile("hipergate"); 96 97 if (isVoid(jdbcDriverClassName)) 98 jdbcDriverClassName = env.getProperty("driver"); 99 100 if (isVoid(jdbcURL)) 101 jdbcURL = env.getProperty("dburl"); 102 103 if (isVoid(dbUserName)) 104 dbUserName = env.getProperty("dbuser"); 105 106 if (isVoid(dbUserPassword)) 107 dbUserPassword = env.getProperty("dbpassword"); 108 } 109 110 if (jdbcDriverClassName == null || jdbcURL == null) 111 throw new UnavailableException("Init params missing"); 112 } 114 115 117 128 public void doGet(HttpServletRequest request, HttpServletResponse response) 129 throws IOException , FileNotFoundException , ServletException 130 { 131 Class oDriver; 132 Connection oConn = null; 133 ServletOutputStream oOut = response.getOutputStream(); 134 QueryByForm oQBF; 135 String sQuerySpec; 136 String sColumnList; 137 String sWhere; 138 String sOrderBy; 139 String sShowAs; 140 String sStorage; 141 142 if (DebugFile.trace) { 143 DebugFile.writeln("Begin HttpQueryServlet.doGet(...)"); 144 DebugFile.incIdent(); 145 } 146 147 sStorage = Environment.getProfileVar("hipergate", "storage"); 148 149 if (DebugFile.trace) DebugFile.writeln("storage=" + sStorage); 150 151 try { 152 oDriver = Class.forName(jdbcDriverClassName); 153 } 154 catch (ClassNotFoundException ignore) { 155 oDriver = null; 156 if (DebugFile.trace) DebugFile.writeln("Class.forName(" + jdbcDriverClassName + ") : " + ignore.getMessage()); 157 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database driver not found"); 158 } 159 160 if (null==oDriver) return; 161 162 try { 163 if (DebugFile.trace) DebugFile.writeln("DriverManager.getConnection(" + jdbcURL + ",...)"); 164 165 oConn = DriverManager.getConnection(jdbcURL,dbUserName,dbUserPassword); 166 167 sQuerySpec = request.getParameter("queryspec"); 168 sColumnList = request.getParameter("columnlist"); 169 if (null==sColumnList) sColumnList = "*"; 170 sWhere = request.getParameter("where"); 171 if (null==sWhere) sWhere = "1=1"; 172 sOrderBy = request.getParameter("orderby"); 173 if (null==sOrderBy) sOrderBy = ""; 174 sShowAs = request.getParameter("showas"); 175 if (null==sShowAs) sShowAs = "CSV"; 176 177 if (DebugFile.trace) DebugFile.writeln("queryspec=" + sQuerySpec!=null ? sQuerySpec : "null"); 178 if (DebugFile.trace) DebugFile.writeln("where=" + sWhere); 179 if (DebugFile.trace) DebugFile.writeln("orderby=" + sOrderBy); 180 181 oQBF = new QueryByForm("file://" + sStorage + "/qbf/" + sQuerySpec + ".xml"); 182 183 if (sShowAs.equalsIgnoreCase("XLS")) { 185 response.setContentType("application/x-msexcel"); 186 response.setHeader("Content-Disposition", "inline; filename=\"" + oQBF.getTitle(request.getLocale().getLanguage())+ " 1.csv\""); 187 } 188 else if (sShowAs.equalsIgnoreCase("CSV")) { 189 response.setContentType("text/plain"); 190 response.setHeader("Content-Disposition","attachment; filename=\"" + oQBF.getTitle(request.getLocale().getLanguage())+ " 1.csv\""); 191 } 192 else if (sShowAs.equalsIgnoreCase("TSV")) { 193 response.setContentType("text/tab-separated-values"); 194 response.setHeader("Content-Disposition","attachment; filename=\"" + oQBF.getTitle(request.getLocale().getLanguage())+ " 1.tsv\""); 195 } 196 else { 197 response.setContentType("text/plain"); 198 response.setHeader("Content-Disposition", "inline; filename=\"" + oQBF.getTitle(request.getLocale().getLanguage())+ " 1.txt\""); 199 } 200 201 if (0==sOrderBy.length()) 202 oQBF.queryToStream(oConn, sColumnList, oQBF.getBaseFilter(request) + " " + sWhere, oOut, sShowAs); 203 else 204 oQBF.queryToStream(oConn, sColumnList, oQBF.getBaseFilter(request) + " " + sWhere + " ORDER BY " + sOrderBy, oOut, sShowAs); 205 206 oConn.close(); 207 oConn = null; 208 209 oOut.flush(); 210 } 211 catch (SQLException e) { 212 if (DebugFile.trace) DebugFile.writeln("SQLException " + e.getMessage()); 213 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 214 } 215 catch (ClassNotFoundException e) { 216 if (DebugFile.trace) DebugFile.writeln("ClassNotFoundException " + e.getMessage()); 217 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 218 } 219 catch (IllegalAccessException e) { 220 if (DebugFile.trace) DebugFile.writeln("IllegalAccessException " + e.getMessage()); 221 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 222 } 223 catch (Exception e) { 224 if (DebugFile.trace) DebugFile.writeln("Exception " + e.getMessage()); 225 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 226 } 227 finally { 228 try { if(null!=oConn) if(!oConn.isClosed()) oConn.close(); } 229 catch (SQLException e) { if (DebugFile.trace) DebugFile.writeln("SQLException " + e.getMessage()); } 230 } 231 232 if (DebugFile.trace) { 233 DebugFile.decIdent(); 234 DebugFile.writeln("End HttpQueryServlet.doGet()"); 235 } 236 } 238 241 private String jdbcDriverClassName; 242 private String jdbcURL; 243 private String dbUserName; 244 private String dbUserPassword; 245 246 } | Popular Tags |