1 16 package org.apache.cocoon.components.jsp; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.avalon.framework.parameters.ParameterException; 20 import org.apache.avalon.framework.parameters.Parameters; 21 import org.apache.avalon.framework.parameters.Parameterizable; 22 import org.apache.avalon.framework.thread.ThreadSafe; 23 24 import javax.servlet.RequestDispatcher ; 25 import javax.servlet.ServletContext ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.Cookie ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.IOException ; 32 import java.util.Locale ; 33 34 45 public class JSPEngineImplWLS extends AbstractLogEnabled 46 implements JSPEngine, Parameterizable, ThreadSafe { 47 48 49 public static final String INC_SERVLET_PATH = "javax.servlet.include.servlet_path"; 50 51 54 public static final String CONFIG_SERVLET_NAME = "servlet-name"; 55 58 public static final String DEFAULT_SERVLET_NAME = "*.jsp"; 59 61 62 String servletName = DEFAULT_SERVLET_NAME; 63 64 69 public void parameterize(Parameters params) throws ParameterException { 70 this.servletName = params.getParameter( CONFIG_SERVLET_NAME, DEFAULT_SERVLET_NAME); 71 } 72 73 84 public byte[] executeJSP(String url, 85 HttpServletRequest servletRequest, 86 HttpServletResponse servletResponse, 87 ServletContext servletContext) 88 throws IOException , ServletException , Exception { 89 90 byte[] bytes = null; 91 92 HttpServletRequest request = servletRequest; 93 String inc_servlet_path_was = (String ) servletRequest.getAttribute(INC_SERVLET_PATH); 94 request.setAttribute(INC_SERVLET_PATH, url); 95 MyWLSResponse response = new MyWLSResponse( servletResponse, 96 (weblogic.servlet.internal.ServletContextImpl) servletContext); 97 98 RequestDispatcher rd = servletContext.getNamedDispatcher(servletName); 100 if (rd != null) { 101 rd.include(request,response); 102 response.flushBuffer(); 103 104 if (getLogger().isDebugEnabled()) { 105 getLogger().debug("JSP response: " + response.getResponseContentAsString()); 106 } 107 108 bytes = response.getResponseContentAsByteArray(); 109 110 if (inc_servlet_path_was != null) { 111 servletRequest.setAttribute( INC_SERVLET_PATH, inc_servlet_path_was ); 112 } 113 } else { 114 getLogger().error( "Specify a correct " + CONFIG_SERVLET_NAME + " " + servletName ); 115 } 116 117 return bytes; 118 } 119 120 132 static class MyWLSResponse extends weblogic.servlet.internal.ServletResponseImpl { 133 136 HttpServletResponse response; 137 138 ByteArrayOutputStream baos; 139 weblogic.servlet.internal.ServletOutputStreamImpl wlsOutputStream; 140 141 public MyWLSResponse( HttpServletResponse response, 142 weblogic.servlet.internal.ServletContextImpl servlet_context ) { 143 144 super( servlet_context ); 145 this.response = response; 146 147 baos = new ByteArrayOutputStream (); 148 149 wlsOutputStream = 150 new weblogic.servlet.internal.ServletOutputStreamImpl( baos ); 151 this.setOutputStream( wlsOutputStream ); 152 wlsOutputStream.setImpl( this ); 153 } 154 155 157 public void flushBuffer() throws IOException { 158 super.flushBuffer(); 159 baos.flush(); 160 } 161 162 170 public byte[] getResponseContentAsByteArray() { 171 byte[] baos_arr = baos.toByteArray(); 172 173 int baos_arr_length = baos_arr.length; 174 int i = 0; 175 boolean matched = false; 176 final int I_MAX = 8192; 178 final byte MATCH_0d = (byte)'\r'; 179 final byte MATCH_0a = (byte)'\n'; 180 181 final byte MATCH_FIRST = (byte)'<'; 182 final byte MATCH_SECOND = (byte)'?'; 183 final byte MATCH_THIRD = (byte)'x'; 184 final byte MATCH_FOURTH = (byte)'m'; 185 final byte MATCH_FIFTH = (byte)'l'; 186 187 final int MATCH_COUNT = 5; 188 189 while (i + MATCH_COUNT < baos_arr_length && i < I_MAX && !matched) { 190 191 matched = (baos_arr[i] == MATCH_FIRST && baos_arr[i+1] == MATCH_SECOND && 192 baos_arr[i+2] == MATCH_THIRD && baos_arr[i+3] == MATCH_FOURTH && 193 baos_arr[i+4] == MATCH_FIFTH); 194 if (matched) break; 195 196 matched = (baos_arr[i] == MATCH_0d && baos_arr[i+1] == MATCH_0a && 197 baos_arr[i+2] == MATCH_0d && baos_arr[i+3] == MATCH_0a && 198 baos_arr[i+4] == MATCH_FIRST); 199 if (matched) { 200 i += 4; break; 202 } 203 i += 2; 204 } 205 if (matched && i > 0) { 206 int baos_arr_new_length = baos_arr_length - i; 207 208 byte []new_baos_arr = new byte[baos_arr_new_length]; 209 System.arraycopy( baos_arr, i, new_baos_arr, 0, baos_arr_new_length ); 210 baos_arr = new_baos_arr; 211 } 212 return baos_arr; 213 } 214 215 public String getResponseContentAsString() { 216 String s = new String ( getResponseContentAsByteArray() ); 217 return s; 218 } 219 220 222 public String getCharacterEncoding() { return this.response.getCharacterEncoding();} 223 public Locale getLocale(){ return this.response.getLocale();} 224 public void addCookie(Cookie cookie){ response.addCookie(cookie); } 225 public boolean containsHeader(String s){ return response.containsHeader(s); } 226 227 public String encodeUrl(String s){ return response.encodeUrl(s); } 228 public String encodeURL(String s){ return response.encodeURL(s); } 229 230 public String encodeRedirectUrl(String s){ return response.encodeRedirectUrl(s); } 231 public String encodeRedirectURL(String s){ return response.encodeRedirectURL(s); } 232 public void sendError(int i, String s) 233 throws IOException {response.sendError(i,s); } 234 public void sendError(int i) 235 throws IOException {response.sendError(i); } 236 public void sendRedirect(String s) 237 throws IOException {response.sendRedirect(s); } 238 public void setDateHeader(String s, long l){response.setDateHeader(s, l); } 239 public void addDateHeader(String s, long l){response.addDateHeader(s, l); } 240 public void setHeader(String s, String s1){response.setHeader(s, s1); } 241 public void addHeader(String s, String s1){response.addHeader(s, s1); } 242 public void setIntHeader(String s, int i){response.setIntHeader(s, i); } 243 public void addIntHeader(String s, int i){response.addIntHeader(s, i); } 244 public void setStatus(int i){response.setStatus(i); } 245 246 public void setStatus(int i, String s){response.setStatus(i, s); } 247 } 248 } 249 | Popular Tags |