1 26 package org.ofbiz.webapp.region; 27 28 import javax.servlet.ServletContext ; 29 import javax.servlet.ServletException ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.jsp.JspException ; 33 import javax.servlet.jsp.PageContext ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.GeneralException; 37 import org.ofbiz.base.util.UtilJ2eeCompat; 38 import org.ofbiz.webapp.control.RequestHandler; 39 import org.ofbiz.webapp.view.JPublishWrapper; 40 import org.ofbiz.webapp.view.ViewHandler; 41 import org.ofbiz.webapp.view.ViewHandlerException; 42 43 57 public class Section extends Content { 58 59 protected final String name; 60 protected final String info; 61 protected RegionManager regionManager; 62 63 public final static String module = Section.class.getName(); 64 65 public Section(String name, String info, String content, String type, RegionManager regionManager) { 66 super(content, type); 67 this.name = name; 68 this.info = info; 69 this.regionManager = regionManager; 70 } 71 72 public String getName() { 73 return name; 74 } 75 76 public void render(PageContext pageContext) throws JspException { 77 try { 78 if (UtilJ2eeCompat.doFlushOnRender(pageContext.getServletContext())) { 79 pageContext.getOut().flush(); 80 } 81 render((HttpServletRequest ) pageContext.getRequest(), (HttpServletResponse ) pageContext.getResponse()); 82 } catch (java.io.IOException e) { 83 Debug.logError(e, "Error rendering section: ", module); 84 if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) 85 throw new JspException (e); 86 else 87 throw new JspException (e.toString()); 88 } catch (ServletException e) { 89 Throwable throwable = e.getRootCause() != null ? e.getRootCause() : e; 90 91 Debug.logError(throwable, "Error rendering section: ", module); 92 if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) 93 throw new JspException (throwable); 94 else 95 throw new JspException (throwable.toString()); 96 } 97 } 98 99 public void render(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException , ServletException { 100 ServletContext context = (ServletContext ) request.getAttribute("servletContext"); 101 boolean verboseOn = Debug.verboseOn(); 102 103 if (verboseOn) Debug.logVerbose("Rendering " + this.toString(), module); 104 105 if (content != null) { 107 if ("direct".equals(type)) { 108 if (UtilJ2eeCompat.useOutputStreamNotWriter(context)) { 109 response.getOutputStream().print(content); 110 } else { 111 response.getWriter().print(content); 112 } 113 } else if ("default".equals(type) || "region".equals(type) || "resource".equals(type) || "jpublish".equals(type)) { 114 116 Region region = null; 118 119 if ("default".equals(type) || "region".equals(type)) { 120 region = regionManager.getRegion(content); 121 } 122 123 if ("region".equals(type) || region != null) { 124 if (region == null) { 125 throw new IllegalArgumentException ("No region definition found with name: " + content); 126 } 127 RegionStack.push(request, region); 129 region.render(request, response); 130 RegionStack.pop(request); 131 } else if ("jpublish".equals(type)) { 132 ServletContext sctx = (ServletContext ) request.getAttribute("servletContext"); 134 if (sctx != null) { 135 JPublishWrapper jp = this.getJPublishWrapper(sctx); 136 if (jp != null) { 137 String contentStr = "<!-- " + content + " Not Processed -->"; 138 try { 139 contentStr = jp.render(content, request, response); 140 } catch (GeneralException e) { 141 Debug.logError(e, "Problems rendering view from JPublish", module); 142 } 143 if (UtilJ2eeCompat.useOutputStreamNotWriter(context)) { 144 response.getOutputStream().print(contentStr); 145 } else { 146 response.getWriter().print(contentStr); 147 } 148 } else { 149 throw new IllegalStateException ("No jpublishWrapper found in ServletContext"); 150 } 151 } else { 152 throw new IllegalStateException ("No servletContext found in request"); 153 } 154 } else { 155 viewHandlerRender("default", request, response); 157 } 158 } else { 159 viewHandlerRender(type, request, response); 160 } 161 } 162 if (verboseOn) Debug.logVerbose("DONE Rendering " + this.toString(), module); 163 } 164 165 protected void viewHandlerRender(String typeToUse, HttpServletRequest request, HttpServletResponse response) throws ServletException { 166 ServletContext context = (ServletContext ) request.getAttribute("servletContext"); 167 RequestHandler requestHandler = (RequestHandler) context.getAttribute("_REQUEST_HANDLER_"); 168 169 try { 171 if (Debug.verboseOn()) Debug.logVerbose("Rendering view [" + content + "] of type [" + typeToUse + "]", module); 172 ViewHandler vh = requestHandler.getViewFactory().getViewHandler(typeToUse); 173 vh.render(name, content, info, null, null, request, response); 175 } catch (ViewHandlerException e) { 176 throw new ServletException (e.getNonNestedMessage(), e.getNested()); 177 } 178 } 179 180 protected JPublishWrapper getJPublishWrapper(ServletContext ctx) { 181 JPublishWrapper wrapper = (JPublishWrapper) ctx.getAttribute("jpublishWrapper"); 182 if (wrapper == null) { 183 wrapper = new JPublishWrapper(ctx); 184 } 185 return wrapper; 186 } 187 188 public String toString() { 189 return "Section: " + name + ", info=" + info + ", content=" + content + ", type=" + type; 190 } 191 } 192 | Popular Tags |