1 5 6 package org.infohazard.maverick.view; 7 8 import java.io.IOException ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import javax.servlet.RequestDispatcher ; 13 import javax.servlet.ServletConfig ; 14 import javax.servlet.ServletException ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.infohazard.maverick.flow.ConfigException; 19 import org.infohazard.maverick.flow.TransformStep; 20 import org.infohazard.maverick.flow.View; 21 import org.infohazard.maverick.flow.ViewContext; 22 import org.infohazard.maverick.flow.ViewFactory; 23 import org.infohazard.maverick.util.XML; 24 import org.jdom.Element; 25 26 27 32 public class DispatchedViewFactory implements ViewFactory 33 { 34 37 private static Log log = LogFactory.getLog(DispatchedViewFactory.class); 38 39 43 public void init(Element factoryNode, ServletConfig servletCfg) throws ConfigException 44 { 45 } 46 47 50 public View createView(Element viewNode) throws ConfigException 51 { 52 String path = XML.getValue(viewNode, "path"); 53 54 if (path == null) 55 throw new ConfigException("View node must have a path: " + XML.toString(viewNode)); 56 57 return new DispatchedView(path); 58 } 59 60 63 protected static class DispatchedView implements View 64 { 65 68 protected String path; 69 70 73 public DispatchedView(String path) 74 { 75 if (path.startsWith("/")) 76 this.path = path; 77 else 78 this.path = "/" + path; 79 80 log.debug("Creating DispatchedView with path: " + path); 81 } 82 83 88 public void go(ViewContext vctx) throws IOException , ServletException 89 { 90 if (vctx.getViewParams() != null) 92 { 93 if (log.isDebugEnabled()) 94 log.debug("Setting " + vctx.getViewParams().size() + " params"); 95 96 Iterator entryIt = vctx.getViewParams().entrySet().iterator(); 97 while (entryIt.hasNext()) 98 { 99 Map.Entry entry = (Map.Entry )entryIt.next(); 100 vctx.getRequest().setAttribute((String )entry.getKey(), entry.getValue()); 101 } 102 } 103 104 RequestDispatcher disp = vctx.getRequest().getRequestDispatcher(this.path); 105 if (null == disp) 106 throw new ServletException ("RequestDispatcher could not be created for " + this.path); 107 108 TransformStep next = vctx.getNextStep(); 109 110 if (log.isDebugEnabled()) 111 log.debug((next.isLast() ? "Forwarding to " : "Including ") + this.path); 112 113 if (next.isLast()) 114 disp.forward(vctx.getRequest(), next.getResponse()); 115 else 116 disp.include(vctx.getRequest(), next.getResponse()); 117 118 next.done(); 119 } 120 } 121 } | Popular Tags |