1 16 package org.apache.cocoon.components.jsp; 17 18 import java.io.IOException ; 19 20 import javax.servlet.RequestDispatcher ; 21 import javax.servlet.ServletContext ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.avalon.framework.logger.AbstractLogEnabled; 27 import org.apache.avalon.framework.parameters.Parameterizable; 28 import org.apache.avalon.framework.parameters.Parameters; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 31 43 public class JSPEngineImplNamedDispatcherInclude extends AbstractLogEnabled 44 implements JSPEngine, Parameterizable, ThreadSafe { 45 46 50 public static final String CONFIG_SERVLET_NAME = "servlet-name"; 51 52 57 public static final String CONFIG_FORWARD = "forward"; 58 59 64 public static final String DEFAULT_SERVLET_NAME = "*.jsp"; 65 66 70 public static final boolean DEFAULT_FORWARD = false; 71 72 75 private String servletName = DEFAULT_SERVLET_NAME; 76 77 81 private boolean forward = DEFAULT_FORWARD; 82 83 94 public void parameterize(Parameters params) { 95 this.servletName = params.getParameter(CONFIG_SERVLET_NAME, DEFAULT_SERVLET_NAME); 96 this.forward = params.getParameterAsBoolean(CONFIG_FORWARD, DEFAULT_FORWARD); 97 } 98 99 102 public byte[] executeJSP(String url, 103 HttpServletRequest servletRequest, 104 HttpServletResponse servletResponse, 105 ServletContext servletContext) 106 throws IOException , ServletException , Exception { 107 108 JSPEngineServletOutputStream output = new JSPEngineServletOutputStream(); 109 JSPEngineServletRequest request = new JSPEngineServletRequest(servletRequest,url); 110 JSPEngineServletResponse response = new JSPEngineServletResponse(servletResponse,output); 111 112 byte[] bytes = null; 113 114 RequestDispatcher rd = servletContext.getNamedDispatcher(servletName); 116 if (rd != null) { 117 if (forward) { 118 rd.forward(request,response); 119 } 120 else { 121 rd.include(request,response); 122 } 123 response.flushBuffer(); 124 bytes = output.toByteArray(); 125 } else { 126 throw new Exception ("No RequestDispatcher found. Specify a correct '" 127 + CONFIG_SERVLET_NAME + "': " + servletName); 128 } 129 return bytes; 130 } 131 } 132 133 | Popular Tags |