1 32 33 package com.knowgate.http; 34 35 import javax.servlet.*; 36 import javax.servlet.http.*; 37 38 import java.sql.DriverManager ; 39 import java.sql.SQLException ; 40 import java.sql.Connection ; 41 42 import com.knowgate.debug.DebugFile; 43 import com.knowgate.misc.Environment; 44 import com.knowgate.addrbook.Fellow; 45 46 51 52 public class HttpVCardServlet extends HttpServlet { 53 54 56 private boolean isVoid(String sParam) { 57 if (null==sParam) 58 return true; 59 else 60 return (sParam.length()==0); 61 } 62 63 74 75 public void init() throws ServletException { 76 ServletConfig config = getServletConfig(); 77 78 jdbcDriverClassName = config.getInitParameter("jdbcDriverClassName"); 79 jdbcURL = config.getInitParameter("jdbcURL"); 80 dbUserName = config.getInitParameter("dbUserName"); 81 dbUserPassword = config.getInitParameter("dbUserPassword"); 82 83 if (isVoid(jdbcDriverClassName) || isVoid(jdbcURL) || isVoid(dbUserName) || isVoid(dbUserPassword)) { 84 java.util.Properties env = Environment.getProfile("hipergate"); 85 86 if (isVoid(jdbcDriverClassName)) 87 jdbcDriverClassName = env.getProperty("driver"); 88 89 if (isVoid(jdbcURL)) 90 jdbcURL = env.getProperty("dburl"); 91 92 if (isVoid(dbUserName)) 93 dbUserName = env.getProperty("dbuser"); 94 95 if (isVoid(dbUserPassword)) 96 dbUserPassword = env.getProperty("dbpassword"); 97 } 98 99 if (jdbcDriverClassName == null || jdbcURL == null) { 100 throw new UnavailableException("Init params missing"); 101 } 102 } 104 106 111 public void doGet(HttpServletRequest request, HttpServletResponse response) 112 throws java.io.IOException , ServletException 113 { 114 boolean bFound; 115 Class oDriver; 116 Connection oConn = null; 117 Fellow oFlw = new Fellow(); 118 String sPKFld, sPKVal, vCard, sNick = null; 119 120 if (DebugFile.trace) { 121 DebugFile.writeln("Begin HttpVCardServlet.doGet"); 122 DebugFile.incIdent(); 123 } 124 125 try { 126 oDriver = Class.forName(jdbcDriverClassName); 127 } 128 catch (ClassNotFoundException ignore) { 129 oDriver = null; 130 if (DebugFile.trace) DebugFile.writeln("Class.forName(" + jdbcDriverClassName + ") : " + ignore.getMessage()); 131 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database driver not found"); 132 } 133 134 if (null==oDriver) return; 135 136 ServletOutputStream oOut = response.getOutputStream(); 137 138 try { 139 if (DebugFile.trace) DebugFile.writeln("DriverManager.getConnection(" + jdbcURL + ",...)"); 140 141 oConn = DriverManager.getConnection(jdbcURL,dbUserName,dbUserPassword); 142 143 sPKFld = (request.getParameter("pk_field")!=null ? request.getParameter("pk_field") : "null"); 144 sPKVal = (request.getParameter("pk_value")!=null ? request.getParameter("pk_value") : "null"); 145 146 if (DebugFile.trace) DebugFile.writeln("pk_field = " + sPKFld); 147 148 if (sPKFld.equalsIgnoreCase("gu_fellow")) { 149 bFound = oFlw.load(oConn, new Object []{sPKVal}); 150 151 if (!oFlw.isNull("tx_nickname")) 152 sNick = oFlw.getString("tx_nickname"); 153 else 154 sNick = sPKVal; 155 } 156 else 157 bFound = false; 158 159 if (bFound) { 160 161 if (DebugFile.trace) { 162 DebugFile.writeln("response.setContentType(\"application/directory\")"); 163 DebugFile.writeln("response.setHeader(\"Content-Disposition\", \"attachment; filename=\"" + sNick + ".vcf\""); 164 } 165 166 response.setContentType("application/directory"); 168 response.setHeader("Content-Disposition", "attachment; filename=\"" + sNick + ".vcf\""); 169 170 oOut.print(oFlw.vCard(oConn)); 171 172 oOut.flush(); 173 } 175 if (!bFound) { 176 if (DebugFile.trace) DebugFile.writeln("SQLException: Cannot find requested document"); 177 178 response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot find requested document"); 179 } 180 181 oConn.close(); 182 oConn = null; 183 } 184 catch (SQLException e) { 185 bFound = false; 186 187 if (DebugFile.trace) DebugFile.writeln("SQLException: " + e.getMessage()); 188 189 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); 190 } 191 try { if(null!=oConn) if(!oConn.isClosed()) oConn.close(); } catch (SQLException e) { } 192 193 if (DebugFile.trace) { 194 DebugFile.decIdent(); 195 DebugFile.writeln("End HttpVCardServlet().doGet()"); 196 } 197 } 199 202 private String jdbcDriverClassName; 203 private String jdbcURL; 204 private String dbUserName; 205 private String dbUserPassword; 206 } 207 | Popular Tags |