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.MultipleValidationException; 32 import org.dbforms.config.Table; 33 34 import org.dbforms.event.DatabaseEvent; 35 import org.dbforms.event.EventEngine; 36 import org.dbforms.event.WebEvent; 37 38 import org.dbforms.util.MessageResources; 39 import org.dbforms.util.MultipartRequest; 40 import org.dbforms.util.ParseUtil; 41 import org.dbforms.util.SqlUtil; 42 import org.dbforms.util.Util; 43 44 import org.dbforms.validation.ValidatorConstants; 45 46 import java.io.IOException ; 47 import java.io.PrintWriter ; 48 49 import java.sql.Connection ; 50 import java.sql.SQLException ; 51 52 import java.util.Enumeration ; 53 import java.util.Hashtable ; 54 import java.util.Locale ; 55 import java.util.Vector ; 56 57 import javax.servlet.ServletException ; 58 import javax.servlet.http.HttpServlet ; 59 import javax.servlet.http.HttpServletRequest ; 60 import javax.servlet.http.HttpServletResponse ; 61 import javax.servlet.http.HttpSession ; 62 63 64 65 72 public class Controller extends HttpServlet { 73 74 private static Log logCat = LogFactory.getLog(Controller.class.getName()); 75 76 77 private int maxUploadSize = 102400; 78 79 88 public void doGet(HttpServletRequest request, 89 HttpServletResponse response) 90 throws IOException , ServletException { 91 process(request, response); 92 } 93 94 95 104 public void doPost(HttpServletRequest request, 105 HttpServletResponse response) 106 throws IOException , ServletException { 107 process(request, response); 108 } 109 110 111 116 public void init() throws ServletException { 117 String maxUploadSizeStr = this.getServletConfig() 120 .getInitParameter("maxUploadSize"); 121 122 if (maxUploadSizeStr != null) { 123 try { 124 this.maxUploadSize = Integer.parseInt(maxUploadSizeStr); 125 } catch (NumberFormatException nfe) { 126 logCat.error("maxUploadSize not a valid number => using default."); 127 } 128 } 129 } 130 131 132 142 private Connection getConnection(DbFormsConfig config, 143 HttpServletRequest request, 144 int tableId, 145 Hashtable connectionsTable) 146 throws SQLException { 147 String connectionName = null; 148 Connection con = null; 149 150 if (tableId != -1) { 152 connectionName = ParseUtil.getParameter(request, "invname_" + tableId); 153 } 154 155 connectionName = Util.isNull(connectionName) ? "default" 156 : connectionName; 157 158 if ((con = (Connection ) connectionsTable.get(connectionName)) == null) { 159 con = config.getConnection(connectionName); 160 connectionsTable.put(connectionName, con); 161 } 162 163 return con; 164 } 165 166 167 178 private void cleanUpConnectionAfterException(Connection con) { 179 try { 180 if ((con != null) && (!con.getAutoCommit())) { 182 con.rollback(); 183 con.setAutoCommit(true); 184 } 185 } catch (java.sql.SQLException e) { 186 SqlUtil.logSqlException(e); 187 } 188 } 189 190 191 196 private void closeConnections(Hashtable connectionsTable) { 197 Enumeration cons = connectionsTable.keys(); 198 199 while (cons.hasMoreElements()) { 200 String dbConnectionName = (String ) cons.nextElement(); 201 Connection con = (Connection ) connectionsTable.get(dbConnectionName); 202 203 try { 204 if ((con != null) && (!con.getAutoCommit())) { 206 con.commit(); 207 con.setAutoCommit(true); 208 } 209 } catch (java.sql.SQLException e) { 210 SqlUtil.logSqlException(e); 211 } 212 213 SqlUtil.closeConnection(con); 214 } 215 } 216 217 218 227 private void process(HttpServletRequest request, 228 HttpServletResponse response) 229 throws IOException , ServletException { 230 HttpSession session = request.getSession(true); 231 logCat.debug("session timeout: " + session.getMaxInactiveInterval()); 232 233 DbFormsConfig config = null; 236 237 try { 238 config = DbFormsConfigRegistry.instance() 239 .lookup(); 240 } catch (Exception e) { 241 logCat.error(e); 242 throw new ServletException (e); 243 } 244 245 Hashtable connections = new Hashtable (); 251 String contentType = request.getContentType(); 252 253 processLocale(request); 258 259 if ((contentType != null) && contentType.startsWith("multipart")) { 260 try { 261 MultipartRequest multipartRequest = new MultipartRequest(request, 262 maxUploadSize); 263 request.setAttribute("multipartRequest", multipartRequest); 264 } catch (IOException ioe) { 265 logCat.error("::process - check if uploaded file(s) exceeded allowed size", 266 ioe); 267 sendErrorMessage("Check if uploaded file(s) exceeded allowed size.", 268 response); 269 270 return; 271 } 272 } 273 274 Connection con = null; 275 WebEvent e = null; 276 Vector errors = new Vector (); 277 278 try { 279 request.setAttribute("errors", errors); 280 281 EventEngine engine = new EventEngine(request, config); 282 e = engine.generatePrimaryEvent(); 283 284 if (e != null) { 286 request.setAttribute("webEvent", e); 287 } 288 289 try { 290 con = getConnection(config, request, 291 ((e == null) || (e.getTable() == null)) ? (-1) 292 : e.getTable().getId(), 293 connections); 294 295 if (e instanceof DatabaseEvent) { 297 try { 298 String formValidatorName = request.getParameter(ValidatorConstants.FORM_VALIDATOR_NAME 301 + "_" 302 + e.getTable().getId()); 303 304 if (formValidatorName != null) { 305 ((DatabaseEvent) e).doValidation(formValidatorName, 306 getServletContext()); 307 } 308 309 ((DatabaseEvent) e).processEvent(con); 310 } catch (SQLException sqle) { 311 logCat.error("::process - SQLException:", sqle); 312 errors.addElement(sqle); 313 cleanUpConnectionAfterException(con); 314 } catch (MultipleValidationException mve) { 315 processMultipleValidationException(con, errors, mve); 316 } 317 } 318 319 if (engine.getInvolvedTables() != null) { 330 processInvolvedTables(config, request, connections, e, errors, 331 engine); 332 } 333 } catch (SQLException exc) { 334 throw new ServletException (exc); 335 } 336 } finally { 337 closeConnections(connections); 339 340 if (e != null) { 341 String followUp = null; 343 344 if (errors.size() != 0) { 345 followUp = e.getFollowUpOnError(); 346 } 347 348 if (Util.isNull(followUp)) { 349 followUp = e.getFollowUp(); 350 } 351 352 logCat.info("*** e = " + e + "*** e.getFollowUp() = " 353 + e.getFollowUp()); 354 355 if (!Util.isNull(followUp)) { 356 request.getRequestDispatcher(followUp) 357 .forward(request, response); 358 } 359 } 360 } 361 } 362 363 364 373 384 private void processInvolvedTables(DbFormsConfig config, 385 HttpServletRequest request, 386 Hashtable connections, 387 WebEvent e, 388 Vector errors, 389 EventEngine engine) 390 throws SQLException { 391 Connection con; 392 393 Enumeration tableEnum = engine.getInvolvedTables() 395 .elements(); 396 397 while (tableEnum.hasMoreElements()) { 398 Table t = (Table) tableEnum.nextElement(); 399 Enumeration eventEnum = engine.generateSecundaryEvents(t, e); 400 401 while (eventEnum.hasMoreElements()) { 403 DatabaseEvent dbE = (DatabaseEvent) eventEnum.nextElement(); 404 405 if (t.getId() == dbE.getTable() 408 .getId()) { 409 con = getConnection(config, request, dbE.getTable().getId(), 410 connections); 411 412 String formValidatorName = request.getParameter(ValidatorConstants.FORM_VALIDATOR_NAME 414 + "_" 415 + dbE.getTable().getId()); 416 417 try { 418 if (formValidatorName != null) { 421 dbE.doValidation(formValidatorName, getServletContext()); 422 } 423 424 dbE.processEvent(con); 425 } catch (SQLException sqle2) { 426 SqlUtil.logSqlException(sqle2, 427 "::process - exception while process secundary events"); 428 errors.addElement(sqle2); 429 cleanUpConnectionAfterException(con); 430 } catch (MultipleValidationException mve) { 431 processMultipleValidationException(con, errors, mve); 432 } 433 } 434 } 435 } 436 } 437 438 439 444 private void processLocale(HttpServletRequest request) { 445 String lang = ParseUtil.getParameter(request, "lang"); 446 String country = ParseUtil.getParameter(request, "country", ""); 447 Locale locale = null; 448 449 if (!Util.isNull(lang)) { 450 locale = new Locale (lang, country); 451 } else if (MessageResources.getLocale(request) == null) { 452 MessageResources.setLocale(request, request.getLocale()); 453 } 454 455 if (locale != null) { 456 MessageResources.setLocale(request, locale); 457 } 458 } 459 460 461 468 private void processMultipleValidationException(Connection con, 469 Vector errors, 470 MultipleValidationException mve) { 471 java.util.Vector v = null; 472 473 logCat.error("::processMultipleValidationException - exception", mve); 474 475 if ((v = mve.getMessages()) != null) { 476 Enumeration e = v.elements(); 477 478 while (e.hasMoreElements()) { 479 errors.addElement(e.nextElement()); 480 } 481 } 482 483 cleanUpConnectionAfterException(con); 484 } 485 486 487 493 private void sendErrorMessage(String message, 494 HttpServletResponse response) { 495 try { 496 PrintWriter out = response.getWriter(); 497 498 response.setContentType("text/html"); 499 out.println("<html><body><h1>ERROR:</h1><p>"); 500 out.println(message); 501 out.println("</p></body></html>"); 502 } catch (IOException ioe) { 503 logCat.error("::sendErrorMessage - senderror message crashed", ioe); 504 } 505 } 506 } 507 | Popular Tags |