| Home >> All >> org >> infohazard >> maverick >> [ view Javadoc ] |
Source code: org/infohazard/maverick/view/DispatchedViewFactory.java
1 /* 2 * $Id: DispatchedViewFactory.java,v 1.7 2004/06/07 20:38:17 eelco12 Exp $ 3 * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/view/DispatchedViewFactory.java,v $ 4 */ 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 /** 28 * <p>This factory creates Views which use the RequestDispatcher to obtain 29 * content. It is used by the DocumentViewFactory and not intended to be 30 * designated as a normal Maverick view type.</p> 31 */ 32 public class DispatchedViewFactory implements ViewFactory 33 { 34 /** 35 * Logger. 36 */ 37 private static Log log = LogFactory.getLog(DispatchedViewFactory.class); 38 39 /** 40 * Initialize. 41 * @see org.infohazard.maverick.flow.ViewFactory#init(org.jdom.Element, javax.servlet.ServletConfig) 42 */ 43 public void init(Element factoryNode, ServletConfig servletCfg) throws ConfigException 44 { 45 } 46 47 /** 48 * @see org.infohazard.maverick.flow.ViewFactory#createView(org.jdom.Element) 49 */ 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 /** 61 * The view is rendered as a RequestDispatcher.forward() or include(). 62 */ 63 protected static class DispatchedView implements View 64 { 65 /** 66 * Source url of the document. 67 */ 68 protected String path; 69 70 /** 71 * @param path is the URL of the document to render. 72 */ 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 /** 84 * Renders the url associated with this view directly to the response. 85 * 86 * @see View#go 87 */ 88 public void go(ViewContext vctx) throws IOException, ServletException 89 { 90 // Put any params in the request attributes 91 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 }