dims 01/09/07 16:00:28 Modified: src/org/apache/cocoon/components/jsp JSPEngineImpl.java webapp sitemap.xmap webapp/docs/samples samples.xml Added: src/org/apache/cocoon/components/jsp JSPReader.java webapp/docs/samples/jsp welcome.jsp Log: "JSPReader & sample" from Piroumian, Konstantin Revision Changes Path 1.4 +16 -16 xml-cocoon2/src/org/apache/cocoon/components/jsp/JSPEngineImpl.java Index: JSPEngineImpl.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/jsp/JSPEngineImpl.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- JSPEngineImpl.java 2001/08/20 13:55:10 1.3 +++ JSPEngineImpl.java 2001/09/07 23:00:28 1.4 @@ -20,8 +20,8 @@ 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.components.parser.Parser; -import org.xml.sax.SAXException; +//import org.apache.cocoon.components.parser.Parser; +//import org.xml.sax.SAXException; import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; @@ -45,12 +45,12 @@ /** * Allows JSP to be used as a generator. Builds upon the JSP servlet - * functionality - overrides the output method and returns the byte(s). + * functionality - overrides the output method and returns the byte(s). * * @author <a href="mailto:[EMAIL PROTECTED]">Davanum Srinivas</a> - * @version CVS $Revision: 1.3 $ $Date: 2001/08/20 13:55:10 $ + * @version CVS $Revision: 1.4 $ $Date: 2001/09/07 23:00:28 $ */ -public class JSPEngineImpl extends AbstractLoggable +public class JSPEngineImpl extends AbstractLoggable implements JSPEngine, Contextualizable, Composable, Configurable, ThreadSafe, Disposable { /** The Servlet Include Path */ @@ -70,9 +70,9 @@ } /** - * Set the sitemap-provided configuration. + * Set the sitemap-provided configuration. * @param conf The configuration information - * @exception ConfigurationException + * @exception ConfigurationException */ public void configure(Configuration conf) throws ConfigurationException { Parameters params = Parameters.fromConfiguration(conf); @@ -94,15 +94,15 @@ * execute the JSP and return the output * * @param context The Servlet Context - * @exception IOException - * @exception ServletException - * @exception SAXException - * @exception Exception + * @exception IOException + * @exception ServletException + * @exception SAXException + * @exception Exception */ - public byte[] executeJSP(String url, HttpServletRequest httpRequest, HttpServletResponse httpResponse, ServletContext context) - throws IOException, ServletException, SAXException, Exception { + public byte[] executeJSP(String url, HttpServletRequest httpRequest, HttpServletResponse httpResponse, ServletContext context) + throws IOException, ServletException/*, SAXException*/, Exception { - Parser parser = null; +// Parser parser = null; byte[] bytes = null; MyServletRequest request = new MyServletRequest(httpRequest, url); @@ -115,7 +115,7 @@ jsp.service(request, response); bytes = response.toByteArray(); - ByteArrayInputStream input = new ByteArrayInputStream(bytes); +// ByteArrayInputStream input = new ByteArrayInputStream(bytes); // clean up jsp.destroy(); @@ -138,7 +138,7 @@ ServletContext c; public config(ServletContext c) {this.c = c; } - public String getServletName() { return "JspGenerator"; } + public String getServletName() { return "JSPEngineImpl"; } public Enumeration getInitParameterNames() { return c.getInitParameterNames(); } public ServletContext getServletContext() { return c; } 1.1 xml-cocoon2/src/org/apache/cocoon/components/jsp/JSPReader.java Index: JSPReader.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.reading; import org.apache.avalon.excalibur.pool.Poolable; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.component.Composable; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.environment.http.HttpEnvironment; import org.apache.cocoon.environment.Source; import org.apache.cocoon.environment.SourceResolver; import org.apache.cocoon.components.jsp.JSPEngine; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.MalformedURLException; /** * @author <a href="mailto:[EMAIL PROTECTED]">Konstantin Piroumian</a> * @version CVS $Revision: 1.1 $ $Date: 2001/09/07 23:00:28 $ * * The <code>JSPReader</code> component is used to serve JSP page output data * in a sitemap pipeline. */ public class JSPReader extends AbstractReader implements Composable, Poolable{ private ComponentManager manager; public void compose (ComponentManager manager) { this.manager = manager; } /** Contextualize this class public void contextualize(Context context) throws ContextException { if (this.contextDir == null) { org.apache.cocoon.environment.Context ctx = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); try { this.contextDir = new File(ctx.getRealPath("/")).toURL().toExternalForm(); getLogger().debug("JSPReader: Context directory is " + this.contextDir); } catch (MalformedURLException e) { getLogger().error("JSPReader: Could not get context directory", e); throw new ContextException("JSPReader: Could not get context directory", e); } } } */ /** * Generates the output from JSP page. */ public void generate() throws IOException, ProcessingException { // ensure that we are running in a servlet environment HttpServletResponse httpResponse = (HttpServletResponse)this.objectModel.get(HttpEnvironment.HTTP_RESPONSE_OBJECT); HttpServletRequest httpRequest = (HttpServletRequest)this.objectModel.get(HttpEnvironment.HTTP_REQUEST_OBJECT); ServletContext httpContext = (ServletContext)this.objectModel.get(HttpEnvironment.HTTP_SERVLET_CONTEXT); if (httpResponse == null || httpRequest == null || httpContext == null) { throw new ProcessingException("JSPReader can be used only in a Servlet/JSP environment"); } JSPEngine engine = null; Source src = null; try { // KP: A hacky way of source resolving. // Why context:// protocol returns not a string in URL format, // but a system-dependent path with 'file:' prefix? String contextDir = new File(httpContext.getRealPath("/")).toURL().toExternalForm(); src = this.resolver.resolve(this.source); String url = src.getSystemId(); if(url.startsWith(contextDir)) { // File is located under contextDir, using relative file name url = url.substring(contextDir.length()); } if (url.startsWith("file:")) { // we need a relative path url = url.substring(5); } getLogger().debug("JSPReader executing JSP:" + url); engine = (JSPEngine)this.manager.lookup(JSPEngine.ROLE); byte[] bytes = engine.executeJSP(url, httpRequest, httpResponse, httpContext); byte[] buffer = new byte[8192]; int length = -1; ByteArrayInputStream bais = new ByteArrayInputStream(bytes); while ((length = bais.read(buffer)) > -1) { out.write(buffer, 0, length); } bais.close(); bais = null; out.flush(); // } catch (ServletException e) { getLogger().debug("ServletException in JSPReader.generate()", e); getLogger().debug("Embedded ServletException JSPReader.generate()", e.getRootCause()); throw new ProcessingException("ServletException in JSPReader.generate()",e.getRootCause()); } catch (IOException e) { getLogger().debug("IOException in JSPReader.generate()", e); throw new ProcessingException("IOException JSPReader.generate()",e); } catch (ProcessingException e) { throw e; } catch (Exception e) { getLogger().debug("Exception in JSPReader.generate()", e); throw new ProcessingException("Exception JSPReader.generate()",e); } finally { if (src != null) src.recycle(); if (engine != null) this.manager.release(engine); } } } 1.50 +5 -0 xml-cocoon2/webapp/sitemap.xmap Index: sitemap.xmap =================================================================== RCS file: /home/cvs/xml-cocoon2/webapp/sitemap.xmap,v retrieving revision 1.49 retrieving revision 1.50 diff -u -r1.49 -r1.50 --- sitemap.xmap 2001/09/06 20:56:21 1.49 +++ sitemap.xmap 2001/09/07 23:00:28 1.50 @@ -44,6 +44,7 @@ <map:readers default="resource"> <map:reader name="resource" src="org.apache.cocoon.reading.ResourceReader"/> + <map:reader name="jsp" src="org.apache.cocoon.reading.JSPReader"/> </map:readers> <map:serializers default="html"> @@ -459,6 +460,10 @@ <map:generate type="script" src="docs/samples/scripts/{1}"/> <map:transform src="stylesheets/page/simple-page2html.xsl"/> <map:serialize type="html"/> + </map:match> + + <map:match pattern="jsp/*.htm"> + <map:read type="jsp" src="/docs/samples/jsp/{1}.jsp" mime-type="text/html" /> </map:match> <map:match pattern="jsp/*"> 1.14 +3 -0 xml-cocoon2/webapp/docs/samples/samples.xml Index: samples.xml =================================================================== RCS file: /home/cvs/xml-cocoon2/webapp/docs/samples/samples.xml,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- samples.xml 2001/09/06 20:56:21 1.13 +++ samples.xml 2001/09/07 23:00:28 1.14 @@ -125,6 +125,9 @@ <sample name="JSP Generator" href="jsp/hello"> An example of the JSPGenerator producing XML out of using JSP. </sample> + <sample name="JSP Reader" href="jsp/welcome.htm"> + An example of the JSPReader producing HTML out of using JSP. + </sample> <sample name="Python Generator" href="scripts/hello.py"> An example of the ScriptGenerator producing XML out of a Python program. You should make sure that you have the Jython interpreter (jython.jar) from 1.1 xml-cocoon2/webapp/docs/samples/jsp/welcome.jsp Index: welcome.jsp =================================================================== <%@ page import="java.util.*" %> <% response.setHeader("Expires", "0"); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>JSPreader test</title> <style type="text/css"> BODY {background-color: #FFFFFF; color: #660000; font-family: Verdana, Helvetica, Arial; } </style> </head> <body> <h3>This is a dynamic output from the JSPReader</h3> <h4>Current time: <%=new Date()%></h4> <hr noshade size="1"> <small><a href="..">Back to samples</a></small> </body> </html> ---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]