1 14 package org.wings.session; 15 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.wings.*; 20 import org.wings.event.ExitVetoException; 21 import org.wings.event.SRequestEvent; 22 import org.wings.externalizer.ExternalizeManager; 23 import org.wings.externalizer.ExternalizedResource; 24 import org.wings.io.Device; 25 import org.wings.io.DeviceFactory; 26 import org.wings.io.ServletDevice; 27 import org.wings.resource.DynamicCodeResource; 28 29 import javax.servlet.ServletConfig ; 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.ServletOutputStream ; 33 import javax.servlet.http.*; 34 import java.io.IOException ; 35 import java.io.PrintWriter ; 36 import java.io.StringWriter ; 37 import java.util.Enumeration ; 38 import java.util.Locale ; 39 import java.util.Arrays ; 40 41 58 final class SessionServlet 59 extends HttpServlet 60 implements HttpSessionBindingListener { 61 private final transient static Log log = LogFactory.getLog(SessionServlet.class); 62 63 66 protected transient HttpServlet parent = this; 67 68 71 protected String errorTemplateFile; 72 73 76 private Session session; 77 78 private boolean firstRequest = true; 79 80 83 protected SessionServlet() { 84 } 85 86 90 protected final void setParent(HttpServlet p) { 91 if (p != null) parent = p; 92 } 93 94 public final Session getSession() { 95 return session; 96 } 97 98 103 public final void setLocaleFromHeader(String [] args) { 104 if (args == null) 105 return; 106 107 for (int i = 0; i < args.length; i++) { 108 try { 109 getSession().setLocaleFromHeader(Boolean.valueOf(args[i]).booleanValue()); 110 } catch (Exception e) { 111 log.error("setLocaleFromHeader", e); 112 } 113 } 114 } 115 116 124 protected final void handleLocale(HttpServletRequest req) { 125 setLocaleFromHeader(req.getParameterValues("LocaleFromHeader")); 126 127 if (getSession().getLocaleFromHeader()) { 128 for (Enumeration en = req.getLocales(); en.hasMoreElements();) { 129 Locale locale = (Locale ) en.nextElement(); 130 try { 131 getSession().setLocale(locale); 132 return; 133 } catch (Exception ex) { 134 log.warn("locale not supported " + locale); 135 } } } 138 } 139 140 143 144 public ServletContext getServletContext() { 145 if (parent != this) 146 return parent.getServletContext(); 147 else 148 return super.getServletContext(); 149 } 150 151 152 public String getInitParameter(String name) { 153 if (parent != this) 154 return parent.getInitParameter(name); 155 else 156 return super.getInitParameter(name); 157 } 158 159 160 public Enumeration getInitParameterNames() { 161 if (parent != this) 162 return parent.getInitParameterNames(); 163 else 164 return super.getInitParameterNames(); 165 } 166 167 173 public void log(String msg) { 174 if (parent != this) 175 parent.log(msg); 176 else 177 super.log(msg); 178 } 179 180 181 public String getServletInfo() { 182 if (parent != this) 183 return parent.getServletInfo(); 184 else 185 return super.getServletInfo(); 186 } 187 188 189 public ServletConfig getServletConfig() { 190 if (parent != this) 191 return parent.getServletConfig(); 192 else 193 return super.getServletConfig(); 194 } 195 196 198 204 protected void initErrorTemplate(ServletConfig config) throws ServletException { 205 if (errorTemplateFile == null) { 206 errorTemplateFile = config.getInitParameter("wings.error.template"); 207 } 208 } 209 210 213 public final void init(ServletConfig config, 214 HttpServletRequest request, 215 HttpServletResponse response) throws ServletException { 216 try { 217 initErrorTemplate(config); 218 219 session = new Session(); 220 SessionManager.setSession(session); 221 222 if (request.isRequestedSessionIdValid()) { 224 ((PropertyService) session).setProperty("request.url", 226 new RequestURL("", getSessionEncoding(response))); 227 } 228 229 session.init(config, request); 230 231 232 try { 233 String mainClassName = config.getInitParameter("wings.mainclass"); 234 Class mainClass = null; 235 try { 236 mainClass = Class.forName(mainClassName, true, 237 Thread.currentThread() 238 .getContextClassLoader()); 239 } catch (ClassNotFoundException e) { 240 mainClass = Class.forName(mainClassName); 243 } 244 Object main = mainClass.newInstance(); 245 } catch (Exception ex) { 246 log.fatal("could not load wings.mainclass: " + 247 config.getInitParameter("wings.mainclass"), ex); 248 throw new ServletException (ex); 249 } 250 } finally { 251 SessionManager.removeSession(); 255 } 256 } 257 258 262 public final void doPost(HttpServletRequest req, HttpServletResponse res) 263 throws ServletException , IOException { 264 if (req.getContentLength() > getSession().getMaxContentLength() * 1024) { 266 res.setContentType("text/html"); 267 ServletOutputStream out = res.getOutputStream(); 268 out.println("<html><head><title>Too big</title></head>"); 269 out.println("<body><h1>Error - content length > " + 270 getSession().getMaxContentLength() + "k"); 271 out.println("</h1></body></html>"); 272 } else { 273 doGet(req, res); 274 } 275 } 279 280 281 293 public final synchronized void doGet(HttpServletRequest req, 294 HttpServletResponse response) 295 throws ServletException , IOException { 296 SessionManager.setSession(session); 297 session.setServletRequest(req); 298 session.setServletResponse(response); 299 300 session.fireRequestEvent(SRequestEvent.REQUEST_START); 301 302 SForm.clearArmedComponents(); 304 305 Device outputDevice = null; 306 307 try { 308 328 RequestURL requestURL = null; 329 if (req.isRequestedSessionIdValid()) { 330 requestURL = new RequestURL("", getSessionEncoding(response)); 331 ((PropertyService) session).setProperty("request.url", 333 requestURL); 334 } 335 336 if (log.isDebugEnabled()) { 337 log.debug("RequestURL: " + requestURL); 338 log.debug("\nHEADER:"); 339 for (Enumeration en = req.getHeaderNames(); en.hasMoreElements();) { 340 String header = (String ) en.nextElement(); 341 log.debug(" " + header + ": " + req.getHeader(header)); 342 } 343 } 344 handleLocale(req); 345 346 Enumeration en = req.getParameterNames(); 347 Cookie[] cookies = req.getCookies(); 348 349 if (en.hasMoreElements()) { 351 session.fireRequestEvent(SRequestEvent.DISPATCH_START); 353 354 for (int i = 0; i < cookies.length; i++) { 355 Cookie cookie = cookies[i]; 356 String paramName = (String ) cookie.getName(); 357 String value = cookie.getValue(); 358 359 if (log.isDebugEnabled()) 360 log.debug("dispatching cookie " + paramName + " = " + value); 361 362 session.getDispatcher().dispatch(paramName, new String [] { value }); 363 } 364 while (en.hasMoreElements()) { 365 String paramName = (String ) en.nextElement(); 366 String [] value = req.getParameterValues(paramName); 367 368 if (log.isDebugEnabled()) 369 log.debug("dispatching " + paramName + " = " + Arrays.asList(value)); 370 371 session.getDispatcher().dispatch(paramName, value); 372 } 373 374 SForm.fireEvents(); 375 376 session.fireRequestEvent(SRequestEvent.DISPATCH_DONE); 378 } 379 380 session.fireRequestEvent(SRequestEvent.PROCESS_REQUEST); 381 382 383 396 if (session.getExitAddress() != null) { 397 398 try { 399 session.firePrepareExit(); 400 401 String redirectAddress; 402 if (session.getExitAddress().length() > 0) { 403 redirectAddress = session.getExitAddress(); 405 } else { 406 redirectAddress = req.getRequestURL().toString(); 408 } 409 req.getSession().invalidate(); response.sendRedirect(redirectAddress); 411 412 return; 413 } catch (ExitVetoException ex) { 414 session.exit(null); 415 } } 417 418 if (session.getRedirectAddress() != null) { 419 response.sendRedirect(session.getRedirectAddress()); 421 session.setRedirectAddress(null); 422 return; 423 } 424 425 getSession().getReloadManager().invalidateResources(); 427 428 ExternalizeManager extManager = getSession().getExternalizeManager(); 431 String pathInfo = req.getPathInfo().substring(1); 432 log.debug("pathInfo: " + pathInfo); 433 434 439 String externalizeIdentifier = null; 440 if (pathInfo == null 441 || pathInfo.length() == 0 442 || "_".equals(pathInfo) 443 || firstRequest) { 444 log.debug("delivering default frame"); 445 446 if (session.getFrames().size() == 0) 447 throw new ServletException ("no frame visible"); 448 449 SFrame defaultFrame = (SFrame) session.getFrames().iterator().next(); 454 while (defaultFrame.getParent() != null) 455 defaultFrame = (SFrame) defaultFrame.getParent(); 456 457 Resource resource = defaultFrame.getDynamicResource(DynamicCodeResource.class); 458 externalizeIdentifier = resource.getId(); 459 460 firstRequest = false; 461 } else { 462 externalizeIdentifier = pathInfo; 463 } 464 465 ExternalizedResource extInfo = extManager 467 .getExternalizedResource(externalizeIdentifier); 468 if (extInfo != null) { 469 outputDevice = DeviceFactory.createDevice(extInfo); 470 472 session.fireRequestEvent(SRequestEvent.DELIVER_START, extInfo); 473 474 extManager.deliver(extInfo, response, outputDevice); 475 476 session.fireRequestEvent(SRequestEvent.DELIVER_DONE, extInfo); 477 } else { 478 handleUnknownResourceRequested(req, response); 479 } 480 481 } catch (Throwable e) { 482 log.fatal("exception: ", e); 483 handleException(req, response, e); 484 } finally { 485 if (session != null) { 486 session.fireRequestEvent(SRequestEvent.REQUEST_END); 487 } 488 489 if (outputDevice != null) { 490 try { 491 outputDevice.close(); 492 } catch (Exception e) { 493 } 494 } 495 496 499 if (session != null) { 500 session.getReloadManager().clear(); 501 session.setServletRequest(null); 502 session.setServletResponse(null); 503 } 504 505 506 SessionManager.removeSession(); 509 SForm.clearArmedComponents(); 510 } 511 } 512 513 532 protected Device createOutputDevice(HttpServletRequest request, 533 HttpServletResponse response, 534 ExternalizedResource extInfo) 535 throws IOException { 536 return new ServletDevice(response.getOutputStream()); 537 } 538 539 540 541 543 private SFrame errorFrame; 544 545 private SLabel errorStackTraceLabel; 546 private SLabel errorMessageLabel; 547 548 protected void handleException(HttpServletRequest req, 549 HttpServletResponse res, Throwable e) { 550 try { 551 if (errorFrame == null) { 552 errorFrame = new SFrame(); 553 558 errorFrame.getContentPane(). 559 setLayout(new STemplateLayout(errorTemplateFile)); 560 561 errorStackTraceLabel = new SLabel(); 562 errorFrame.getContentPane().add(errorStackTraceLabel, 563 "EXCEPTION_STACK_TRACE"); 564 565 errorMessageLabel = new SLabel(); 566 errorFrame.getContentPane().add(errorMessageLabel, 567 "EXCEPTION_MESSAGE"); 568 } 569 570 res.setContentType("text/html"); 571 ServletOutputStream out = res.getOutputStream(); 572 errorStackTraceLabel.setText(getStackTraceString(e)); 573 errorMessageLabel.setText(e.getMessage()); 574 errorFrame.write(new ServletDevice(out)); 575 } catch (Exception ex) { 576 } 577 } 578 579 586 protected void handleUnknownResourceRequested(HttpServletRequest req, 587 HttpServletResponse res) 588 throws IOException { 589 res.setStatus(HttpServletResponse.SC_NOT_FOUND); 590 res.setContentType("text/html"); 591 res.getOutputStream().println("<h1>404 Not Found</h1>Unknown Resource Requested"); 592 } 593 594 597 598 599 public void valueBound(HttpSessionBindingEvent event) { 600 } 601 602 603 public void valueUnbound(HttpSessionBindingEvent event) { 604 destroy(); 605 } 606 607 612 public static String getSessionEncoding(HttpServletResponse response) { 613 if (response == null) return ""; 614 String enc = response.encodeURL("foo").substring(3); 616 return enc; 617 } 618 619 public void destroy() { 620 log.info("destroy called"); 621 622 SessionManager.setSession(session); 624 try { 625 setParent(null); 627 session.destroy(); 628 session = null; 629 errorFrame = null; 630 errorStackTraceLabel = null; 631 errorMessageLabel = null; 632 } catch (Exception ex) { 633 log.error("destroy", ex); 634 } finally { 635 SessionManager.removeSession(); 636 } 637 } 638 639 private String getStackTraceString(Throwable e) { 640 StringWriter stringWriter = new StringWriter (); 641 PrintWriter printWriter = new PrintWriter (stringWriter); 642 stringWriter.getBuffer().setLength(0); 643 e.printStackTrace(printWriter); 644 return stringWriter.toString(); 645 } 646 } 647 648 649 | Popular Tags |