Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 88088 invoked by uid 500); 19 Jul 2001 17:29:20 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 88075 invoked by uid 500); 19 Jul 2001 17:29:19 -0000 Delivered-To: apmail-xml-cocoon2-cvs@apache.org Date: 19 Jul 2001 17:27:52 -0000 Message-ID: <20010719172752.96676.qmail@icarus.apache.org> From: dims@apache.org To: xml-cocoon2-cvs@apache.org Subject: cvs commit: xml-cocoon2/webapp cocoon.xconf X-Spam-Rating: h31.sny.collab.net 1.6.2 0/1000/N dims 01/07/19 10:27:52 Modified: webapp cocoon.xconf Added: src/org/apache/cocoon/components/jsp JSPEngineImplNamedDispatcherInclude.java JSPEngineImplWLS.java Log: Contribution for JspEngineImplWLS and JSPEngineImplNamedDispatcherInclude from "Bernhard Huber" Revision Changes Path 1.1 xml-cocoon2/src/org/apache/cocoon/components/jsp/JSPEngineImplNamedDispatcherInclude.java Index: JSPEngineImplNamedDispatcherInclude.java =================================================================== /***************************************************************************** * Copyright (C) The Apache Software Foundation. All rights reserved. * * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.jsp; import java.io.BufferedReader; import java.io.IOException; import java.io.OutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import java.security.Principal; import java.util.Enumeration; import java.util.Locale; import java.util.Map; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; import javax.servlet.http.HttpSession; import javax.servlet.Servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.component.Component; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.ContextException; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.logger.AbstractLoggable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.components.parser.Parser; import org.apache.cocoon.environment.http.HttpEnvironment; import org.apache.cocoon.xml.AbstractXMLProducer; import org.apache.cocoon.xml.XMLProducer; import org.apache.avalon.excalibur.pool.Recyclable; import org.apache.log.Logger; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * Allows JSP to be used as a generator. Builds upon the JSP servlet * functionality - overrides the output method and returns the byte(s). * This implementation includes via ServletContext.getNamedDispatcher() the * jsp-response. This a generic implementation. * * @author Davanum Srinivas * @author Bernhard Huber * @version CVS $Revision: 1.1 $ $Date: 2001/07/19 17:27:52 $ */ public class JSPEngineImplNamedDispatcherInclude extends AbstractLoggable implements JSPEngine, Contextualizable, Composable, Configurable, ThreadSafe, Disposable { /** The Servlet Include Path */ public static final String INC_SERVLET_PATH = "javax.servlet.include.servlet_path"; /** config-parameter name for specifying the jsp servlet-name. ie. servlet-name */ public static final String CONFIG_SERVLET_NAME = "servlet-name"; /** default value of CONFIG_SERVLET_NAME. ie. *jsp, this is the WLS JSP servlet default name */ public static final String DEFAULT_SERVLET_NAME = "*.jsp"; /** the configured name of the jsp servlet */ String servletName = DEFAULT_SERVLET_NAME; /** The component manager */ protected ComponentManager manager = null; /** Contextualize this class */ public void contextualize(Context context) throws ContextException { } /** * Set the sitemap-provided configuration. * @param conf The configuration information * @exception ConfigurationException */ public void configure(Configuration conf) throws ConfigurationException { Parameters params = Parameters.fromConfiguration(conf); this.servletName = params.getParameter(CONFIG_SERVLET_NAME, DEFAULT_SERVLET_NAME); } /** * Set the global component manager. This metod also sets the * ComponentSelector used as language factory for both markup and programming languages. * @param manager The global component manager */ public void compose(ComponentManager manager) throws ComponentException { if ((this.manager == null) && (manager != null)) { this.manager = manager; } } /** * execute the JSP and return the output * * @param context The Servlet Context * @exception IOException * @exception ServletException * @exception SAXException * @exception Exception */ public byte[] executeJSP(String url, HttpServletRequest httpRequest, HttpServletResponse httpResponse, ServletContext context) throws IOException, ServletException, SAXException, Exception { Parser parser = null; byte[] bytes = null; MyServletRequest request = new MyServletRequest(httpRequest, url); MyServletResponse response = new MyServletResponse(httpResponse); // start JSPServlet. javax.servlet.RequestDispatcher rd = context.getNamedDispatcher( servletName ); if (rd != null) { rd.include( request, response ); response.flushBuffer(); bytes = response.toByteArray(); ByteArrayInputStream input = new ByteArrayInputStream( bytes ); } else { getLogger().error( "Specify a correct " + CONFIG_SERVLET_NAME + " " + servletName ); } return bytes; } /** * dispose */ public void dispose() { } /** * Stub implementation of HttpServletRequest */ class MyServletRequest implements HttpServletRequest { HttpServletRequest request; String jspFile; public MyServletRequest(HttpServletRequest request, String jspFile) { this.request = request; this.jspFile = jspFile; } public String getAuthType(){ return request.getAuthType(); } public Cookie[] getCookies(){ return request.getCookies(); } public long getDateHeader(String s){ return request.getDateHeader(s); } public String getHeader(String s){ return request.getHeader(s); } public Enumeration getHeaders(String s){ return request.getHeaders(s); } public Enumeration getHeaderNames(){ return request.getHeaderNames(); } public int getIntHeader(String s){ return request.getIntHeader(s); } public String getMethod(){ return request.getMethod(); } public String getPathInfo(){ return request.getPathInfo(); } public String getPathTranslated(){ return request.getPathTranslated(); } public String getContextPath(){ return request.getContextPath(); } public String getQueryString(){ return request.getQueryString(); } public String getRemoteUser(){ return request.getRemoteUser(); } public boolean isUserInRole(String s){ return request.isUserInRole(s); } public Principal getUserPrincipal(){ return request.getUserPrincipal(); } public String getRequestedSessionId(){ return request.getRequestedSessionId(); } public String getRequestURI(){ return request.getRequestURI(); } public String getServletPath(){ return request.getServletPath(); } public HttpSession getSession(boolean flag){ return request.getSession(flag); } public HttpSession getSession(){ return request.getSession(); } public boolean isRequestedSessionIdValid(){ return request.isRequestedSessionIdValid(); } public boolean isRequestedSessionIdFromCookie(){ return request.isRequestedSessionIdFromCookie(); } public boolean isRequestedSessionIdFromURL(){ return request.isRequestedSessionIdFromURL(); } public boolean isRequestedSessionIdFromUrl(){ return request.isRequestedSessionIdFromUrl(); } public Object getAttribute(String s){ if(s != null && s.equals(INC_SERVLET_PATH)) return jspFile; return request.getAttribute(s); } public Enumeration getAttributeNames(){ return request.getAttributeNames(); } public String getCharacterEncoding(){ return request.getCharacterEncoding(); } public int getContentLength(){ return request.getContentLength(); } public String getContentType(){ return request.getContentType(); } public ServletInputStream getInputStream() throws IOException{ return request.getInputStream(); } public String getParameter(String s){ return request.getParameter(s); } public Enumeration getParameterNames(){ return request.getParameterNames(); } public String[] getParameterValues(String s){ return request.getParameterValues(s); } public String getProtocol(){ return request.getProtocol(); } public String getScheme(){ return request.getScheme(); } public String getServerName(){ return request.getServerName(); } public int getServerPort(){ return request.getServerPort(); } public BufferedReader getReader() throws IOException{ return request.getReader(); } public String getRemoteAddr(){ return request.getRemoteAddr(); } public String getRemoteHost(){ return request.getRemoteHost(); } public void setAttribute(String s, Object obj){ request.setAttribute(s,obj); } public void removeAttribute(String s){ request.removeAttribute(s); } public Locale getLocale(){ return request.getLocale(); } public Enumeration getLocales(){ return request.getLocales(); } public boolean isSecure(){ return request.isSecure(); } public RequestDispatcher getRequestDispatcher(String s){ return request.getRequestDispatcher(s); } public String getRealPath(String s){ return request.getRealPath(s); } public java.lang.StringBuffer getRequestURL() { return null; } public java.util.Map getParameterMap() { return null; } public void setCharacterEncoding(java.lang.String $1) { } } /** * Stub implementation of HttpServletResponse */ class MyServletResponse implements HttpServletResponse { HttpServletResponse response; MyServletOutputStream output; public MyServletResponse(HttpServletResponse response){ this.response = response; this.output = new MyServletOutputStream(); } public void flushBuffer() throws IOException { } public int getBufferSize() { return 1024; } public String getCharacterEncoding() { return this.response.getCharacterEncoding();} public Locale getLocale(){ return this.response.getLocale();} public PrintWriter getWriter() { return this.output.getWriter(); } public boolean isCommitted() { return false; } public void reset() {} public void setBufferSize(int size) {} public void setContentLength(int len) {} public void setContentType(java.lang.String type) {} public void setLocale(java.util.Locale loc) {} public ServletOutputStream getOutputStream() { return this.output; } public void addCookie(Cookie cookie){ response.addCookie(cookie); } public boolean containsHeader(String s){ return response.containsHeader(s); } public String encodeURL(String s){ return response.encodeURL(s); } public String encodeRedirectURL(String s){ return response.encodeRedirectURL(s); } public String encodeUrl(String s){ return response.encodeUrl(s); } public String encodeRedirectUrl(String s){ return response.encodeRedirectUrl(s); } public void sendError(int i, String s) throws IOException{response.sendError(i,s); } public void sendError(int i) throws IOException{response.sendError(i); } public void sendRedirect(String s) throws IOException{response.sendRedirect(s); } public void setDateHeader(String s, long l){response.setDateHeader(s, l); } public void addDateHeader(String s, long l){response.addDateHeader(s, l); } public void setHeader(String s, String s1){response.setHeader(s, s1); } public void addHeader(String s, String s1){response.addHeader(s, s1); } public void setIntHeader(String s, int i){response.setIntHeader(s, i); } public void addIntHeader(String s, int i){response.addIntHeader(s, i); } public void setStatus(int i){response.setStatus(i); } public void setStatus(int i, String s){response.setStatus(i, s); } public void resetBuffer(){} public byte[] toByteArray() { return output.toByteArray(); } } /** * Stub implementation of ServletOutputStream */ class MyServletOutputStream extends ServletOutputStream { ByteArrayOutputStream output; PrintWriter writer; public MyServletOutputStream() { this.output = new ByteArrayOutputStream(); this.writer = new PrintWriter(output, true); } public PrintWriter getWriter() { return this.writer; } public void write(byte[] b) throws java.io.IOException { output.write(b); } public void write(byte[] b, int off, int len) throws java.io.IOException { output.write(b,off,len); } public void write(int b) throws java.io.IOException { output.write(b); } public byte[] toByteArray() { this.writer.flush(); byte[] bytes = output.toByteArray(); return bytes; } } } 1.1 xml-cocoon2/src/org/apache/cocoon/components/jsp/JSPEngineImplWLS.java Index: JSPEngineImplWLS.java =================================================================== /***************************************************************************** * Copyright (C) The Apache Software Foundation. All rights reserved. * * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.jsp; import java.io.BufferedReader; import java.io.IOException; import java.io.OutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import java.security.Principal; import java.util.Enumeration; import java.util.Locale; import java.util.Map; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; import javax.servlet.http.HttpSession; import javax.servlet.Servlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.avalon.framework.activity.Disposable; import org.apache.avalon.framework.component.Component; import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; import org.apache.avalon.framework.context.Context; import org.apache.avalon.framework.context.ContextException; import org.apache.avalon.framework.context.Contextualizable; import org.apache.avalon.framework.logger.AbstractLoggable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.framework.thread.ThreadSafe; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.components.parser.Parser; import org.apache.cocoon.environment.http.HttpEnvironment; import org.apache.cocoon.xml.AbstractXMLProducer; import org.apache.cocoon.xml.XMLProducer; import org.apache.avalon.excalibur.pool.Recyclable; import org.apache.log.Logger; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * Allows WLS JSP to be used as a generator. Builds upon the JSP servlet * functionality - overrides the output method and returns the byte(s). * This implementation includes via ServletContext.getNamedDispatcher() the * jsp-response. This a WLS-specific implementation. * This code contain WLS 5.1 specific classes, and uses WLS internal classes. * * @author Davanum Srinivas * @author Bernhard Huber * @version CVS $Revision: 1.1 $ $Date: 2001/07/19 17:27:52 $ */ public class JSPEngineImplWLS extends AbstractLoggable implements JSPEngine, Contextualizable, Composable, Configurable, ThreadSafe, Disposable { /** The Servlet Include Path */ public static final String INC_SERVLET_PATH = "javax.servlet.include.servlet_path"; /** config-parameter name for specifying the jsp servlet-name. ie. servlet-name */ public static final String CONFIG_SERVLET_NAME = "servlet-name"; /** default value of CONFIG_SERVLET_NAME. ie. *jsp, this is the WLS JSP servlet default name */ public static final String DEFAULT_SERVLET_NAME = "*.jsp"; /** the configured name of the jsp servlet */ String servletName = DEFAULT_SERVLET_NAME; /** The component manager */ protected ComponentManager manager = null; /** Contextualize this class */ public void contextualize(Context context) throws ContextException { } /** * Set the sitemap-provided configuration. * @param conf The configuration information * @exception ConfigurationException */ public void configure(Configuration conf) throws ConfigurationException { Parameters params = Parameters.fromConfiguration(conf); this.servletName = params.getParameter( CONFIG_SERVLET_NAME, DEFAULT_SERVLET_NAME); } /** * Set the global component manager. This metod also sets the * ComponentSelector used as language factory for both markup and programming languages. * @param manager The global component manager */ public void compose(ComponentManager manager) throws ComponentException { if ((this.manager == null) && (manager != null)) { this.manager = manager; } } /** * execute the JSP and return the output * * @param context The Servlet Context * @exception IOException * @exception ServletException * @exception SAXException * @exception Exception */ public byte[] executeJSP(String url, HttpServletRequest httpRequest, HttpServletResponse httpResponse, ServletContext context) throws IOException, ServletException, SAXException, Exception { Parser parser = null; byte[] bytes = null; HttpServletRequest request = httpRequest; String inc_servlet_path_was = (String)httpRequest.getAttribute( INC_SERVLET_PATH ); request.setAttribute( INC_SERVLET_PATH, url ); MyWLSResponse response = new MyWLSResponse( httpResponse, (weblogic.servlet.internal.ServletContextImpl)context ); // start JSPServlet. javax.servlet.RequestDispatcher rd = context.getNamedDispatcher( servletName ); if (rd != null) { rd.include( request, response ); response.flushBuffer(); getLogger().debug( "JSP response: " + response.getResponseContentAsString() ); bytes = response.getResponseContentAsByteArray(); if (inc_servlet_path_was != null) { httpRequest.setAttribute( INC_SERVLET_PATH, inc_servlet_path_was ); } } else { getLogger().error( "Specify a correct " + CONFIG_SERVLET_NAME + " " + servletName ); } return bytes; } /** * dispose */ public void dispose() { } /** WLS jsp servlet hack.

Here WLS specific classes are used.

The weblogic.servlet.JSPServlet, and weblogic.servlet.internal.RequesDispatcherImpl expects objects weblogic.servlet.internal.ServletOutputStreamImpl, and weblogic.servlet.internal.ServletResponseImpl. Thus we have to use exactly these classes!

*/ class MyWLSResponse extends weblogic.servlet.internal.ServletResponseImpl { /* the cocoon2 response. Let's use this response to forward headers , cookies, etc generated inside the jsp-response */ HttpServletResponse response; ByteArrayOutputStream baos; weblogic.servlet.internal.ServletOutputStreamImpl wlsOutputStream; public MyWLSResponse( HttpServletResponse response, weblogic.servlet.internal.ServletContextImpl servlet_context ) { super( servlet_context ); this.response = response; baos = new ByteArrayOutputStream(); wlsOutputStream = new weblogic.servlet.internal.ServletOutputStreamImpl( baos ); this.setOutputStream( wlsOutputStream ); wlsOutputStream.setImpl( this ); } /** flush response content. */ public void flushBuffer() throws IOException { super.flushBuffer(); baos.flush(); } /** return response as byte array.

Note: http-headers are skipped. More exactly all chars until first '<?xml', or '\r\n\r\n< sequence. This may be a bit heuristic.

Note: we are expecting the xml prolog, without the xml prolog http -headers are passed further, and the xml parser will surly complain!

*/ public byte[] getResponseContentAsByteArray() { byte[] baos_arr = baos.toByteArray(); int baos_arr_length = baos_arr.length; int i = 0; boolean matched = false; final int I_MAX = 8192; // check only header final byte MATCH_0d = (byte)'\r'; final byte MATCH_0a = (byte)'\n'; final byte MATCH_FIRST = (byte)'<'; final byte MATCH_SECOND = (byte)'?'; final byte MATCH_THIRD = (byte)'x'; final byte MATCH_FOURTH = (byte)'m'; final byte MATCH_FIFTH = (byte)'l'; final int MATCH_COUNT = 5; while (i + MATCH_COUNT < baos_arr_length && i < I_MAX && !matched) { matched = (baos_arr[i] == MATCH_FIRST && baos_arr[i+1] == MATCH_SECOND && baos_arr[i+2] == MATCH_THIRD && baos_arr[i+3] == MATCH_FOURTH && baos_arr[i+4] == MATCH_FIFTH); if (matched) break; matched = (baos_arr[i] == MATCH_0d && baos_arr[i+1] == MATCH_0a && baos_arr[i+2] == MATCH_0d && baos_arr[i+3] == MATCH_0a && baos_arr[i+4] == MATCH_FIRST); if (matched) { i += 4; // skip leading \r\n\r\n, too break; } i += 2; } if (matched && i > 0) { int baos_arr_new_length = baos_arr_length - i; byte []new_baos_arr = new byte[baos_arr_new_length]; System.arraycopy( baos_arr, i, new_baos_arr, 0, baos_arr_new_length ); baos_arr = new_baos_arr; } return baos_arr; } public String getResponseContentAsString() { String s = new String( getResponseContentAsByteArray() ); return s; } // following methods forwarding from jsp-repsonse to cocoon2-repsonse public String getCharacterEncoding() { return this.response.getCharacterEncoding();} public Locale getLocale(){ return this.response.getLocale();} public void addCookie(Cookie cookie){ response.addCookie(cookie); } public boolean containsHeader(String s){ return response.containsHeader(s); } public String encodeURL(String s){ return response.encodeURL(s); } public String encodeRedirectURL(String s){ return response.encodeRedirectURL(s); } public String encodeUrl(String s){ return response.encodeUrl(s); } public String encodeRedirectUrl(String s){ return response.encodeRedirectUrl(s); } public void sendError(int i, String s) throws IOException{response.sendError(i,s); } public void sendError(int i) throws IOException{response.sendError(i); } public void sendRedirect(String s) throws IOException{response.sendRedirect(s); } public void setDateHeader(String s, long l){response.setDateHeader(s, l); } public void addDateHeader(String s, long l){response.addDateHeader(s, l); } public void setHeader(String s, String s1){response.setHeader(s, s1); } public void addHeader(String s, String s1){response.addHeader(s, s1); } public void setIntHeader(String s, int i){response.setIntHeader(s, i); } public void addIntHeader(String s, int i){response.addIntHeader(s, i); } public void setStatus(int i){response.setStatus(i); } public void setStatus(int i, String s){response.setStatus(i, s); } } } 1.23 +1 -0 xml-cocoon2/webapp/cocoon.xconf Index: cocoon.xconf =================================================================== RCS file: /home/cvs/xml-cocoon2/webapp/cocoon.xconf,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- cocoon.xconf 2001/07/17 10:54:45 1.22 +++ cocoon.xconf 2001/07/19 17:27:52 1.23 @@ -64,6 +64,7 @@ + ---------------------------------------------------------------------- In case of troubles, e-mail: webmaster@xml.apache.org To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org For additional commands, e-mail: cocoon-cvs-help@xml.apache.org