1 20 package org.enhydra.barracuda.core.comp.helper; 21 22 import java.io.*; 23 import java.util.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 import org.apache.log4j.*; 28 import org.w3c.dom.*; 29 30 import org.enhydra.barracuda.core.comp.*; 31 import org.enhydra.barracuda.core.event.*; 32 import org.enhydra.barracuda.core.helper.servlet.*; 33 import org.enhydra.barracuda.core.util.dom.*; 34 import org.enhydra.barracuda.core.view.*; 35 import org.enhydra.barracuda.plankton.data.*; 36 import org.enhydra.barracuda.plankton.http.URLRewriter; 37 import org.enhydra.barracuda.plankton.exceptions.*; 38 39 43 public abstract class ComponentGateway extends HttpServlet { 44 45 protected static final Logger logger = Logger.getLogger(ComponentGateway.class.getName()); 47 public boolean recycleChildren = false; 50 51 68 public abstract Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, RenderException, EventException; 69 70 85 public void handleEventException (EventException e, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 86 Exception rootException = NestableException.getRootException(e); 89 if (rootException instanceof ServletException) throw (ServletException) rootException; 90 if (rootException instanceof IOException) throw (IOException) rootException; 91 92 resp.setHeader("Cache-Control","max-age=0"); 95 resp.setDateHeader("Last-Modified", System.currentTimeMillis()); 96 97 resp.setContentType("text/html"); 99 PrintWriter out = resp.getWriter(); 100 ExceptionUtil.logExceptionAsHTML(out, e, req); 101 logger.warn("Unexpected event exception: ", e); 102 if (rootException!=e) { 103 logger.warn("Root Exception: ", rootException); 104 } 105 } 106 107 120 public void handleRenderException (RenderException e, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 121 Exception rootException = NestableException.getRootException(e); 124 if (rootException instanceof ServletException) throw (ServletException) rootException; 125 if (rootException instanceof IOException) throw (IOException) rootException; 126 127 resp.setHeader("Cache-Control","max-age=0"); 130 resp.setDateHeader("Last-Modified", System.currentTimeMillis()); 131 132 resp.setContentType("text/html"); 134 PrintWriter out = resp.getWriter(); 135 ExceptionUtil.logExceptionAsHTML(out, e, req); 136 logger.warn("Unexpected event exception: ", e); 137 if (rootException!=e) { 138 logger.warn("Root Exception:", rootException); 139 } 140 } 141 142 148 public DOMWriter getDOMWriter() { 149 return new DefaultDOMWriter(); 157 } 158 159 160 161 162 171 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 172 boolean handled = ScriptDetector.checkClientReq(req, resp); 175 if (!handled) handleDefault(new DefaultServletRequestWrapper(req), new DefaultServletResponseWrapper(resp)); } 179 180 188 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 189 handleDefault(new DefaultServletRequestWrapper(req), new DefaultServletResponseWrapper(resp)); } 192 193 201 protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 202 handleDefault(new DefaultServletRequestWrapper(req), new DefaultServletResponseWrapper(resp)); } 205 206 214 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 215 handleDefault(new DefaultServletRequestWrapper(req), new DefaultServletResponseWrapper(resp)); } 218 219 227 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 228 handleDefault(new DefaultServletRequestWrapper(req), new DefaultServletResponseWrapper(resp)); } 231 232 240 protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 241 handleDefault(new DefaultServletRequestWrapper(req), new DefaultServletResponseWrapper(resp)); } 244 245 protected void handleDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 246 247 long bmillis = 0; 248 long smillis = 0; 249 long emillis = 0; 250 long elapsed1 = 0; 251 long elapsed2 = 0; 252 if (logger.isInfoEnabled()) bmillis = System.currentTimeMillis(); 253 if (logger.isInfoEnabled()) logger.info("Handling incoming HTTP request in "+this); 254 255 ViewContext vc = null; 256 try { 257 ObjectRepository.setupSessionRepository(req); 260 if (logger.isDebugEnabled()) logger.debug("Create the ViewContext"); 262 vc = new DefaultViewContext(new ViewCapabilities(req, resp), req, resp); 265 if (logger.isDebugEnabled()) logger.debug("Create component root"); 267 BComponent broot = new BComponent(); 268 broot.setName("Root"); 269 270 if (logger.isDebugEnabled()) logger.debug("Handling default"); 272 Document doc = handleDefault(broot, vc, req, resp); 273 274 if (logger.isDebugEnabled()) logger.debug("Invoking initCycle on component hierarchy"); 276 broot.initCycle(); 277 278 if (logger.isDebugEnabled()) logger.debug("Rendering component hierarchy"); 280 if (logger.isDebugEnabled()) smillis = System.currentTimeMillis(); 281 broot.render(vc); 282 if (logger.isDebugEnabled()) elapsed1 = System.currentTimeMillis()-smillis; 283 284 if (recycleChildren) { 287 if (logger.isDebugEnabled()) logger.debug("Recycling child components"); 288 List children = broot.getChildren(); 289 if (children!=null) { 290 for (int i=children.size()-1; i>=0; i--) { 291 broot.removeChild(i); 292 } 293 } 294 } 295 296 if (logger.isDebugEnabled()) logger.debug("Invoking destroyCycle on component hierarchy"); 298 broot.destroyCycle(); 299 300 ScriptDetector.prepareClientResp(doc, vc); 304 305 if (logger.isDebugEnabled()) logger.debug("Rendering the DOM"); 307 if (logger.isInfoEnabled()) smillis = System.currentTimeMillis(); 308 this.getDOMWriter().write(doc, resp); 309 if (logger.isInfoEnabled()) { 310 elapsed2 = System.currentTimeMillis()-smillis; 311 emillis = System.currentTimeMillis(); 312 } 313 314 if (logger.isInfoEnabled()) logger.info("Dispatching complete! (rendered in "+(elapsed1)+"/written in "+(elapsed2)+" of "+(emillis-bmillis)+" millis)"); 315 316 } catch (ClientSideRedirectException re) { 317 327 332 String url = URLRewriter.encodeRedirectURL(req, resp, re.getRedirectURL()); 333 url = ScriptDetector.prepareRedirectURL(url, vc.getViewCapabilities()); if (logger.isInfoEnabled()) logger.info("ClientSideRedirectException...redirecting to "+url); 335 resp.sendRedirect(url); 336 337 } catch (RenderException e) { 338 if (logger.isInfoEnabled()) logger.info("Handling RenderException:"+e); 340 handleRenderException(e, vc, req, resp); 341 342 } catch (EventException e) { 343 if (logger.isInfoEnabled()) logger.info("Handling EventException:"+e); 345 handleEventException(e, vc, req, resp); 346 347 } finally { 348 ObjectRepository.removeSessionRepository(); ObjectRepository.removeLocalRepository(); } 352 } 353 } 354 | Popular Tags |