You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ug...@apache.org on 2004/08/04 15:54:42 UTC

svn commit: rev 35681 - in cocoon/branches/butterfly: . src/java/org/apache/butterfly/components/pipeline src/java/org/apache/butterfly/components/pipeline/impl src/java/org/apache/butterfly/servlet src/test/org/apache/butterfly/components/pipeline/impl testdata tools/jetty/conf

Author: ugo
Date: Wed Aug  4 06:54:41 2004
New Revision: 35681

Added:
   cocoon/branches/butterfly/src/java/org/apache/butterfly/components/pipeline/ConnectionResetException.java
   cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/Pipeline.groovy
   cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/sitemap.groovy
   cocoon/branches/butterfly/testdata/welcome.xml
   cocoon/branches/butterfly/testdata/welcome.xslt
Modified:
   cocoon/branches/butterfly/build.xml
   cocoon/branches/butterfly/src/java/org/apache/butterfly/components/pipeline/impl/NonCachingProcessingPipeline.java
   cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/ButterflyServlet.java
   cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy
   cocoon/branches/butterfly/tools/jetty/conf/main.xml
Log:
ButterflyServlet can now read a sitemap and process a very simple request.


Modified: cocoon/branches/butterfly/build.xml
==============================================================================
--- cocoon/branches/butterfly/build.xml	(original)
+++ cocoon/branches/butterfly/build.xml	Wed Aug  4 06:54:41 2004
@@ -63,6 +63,10 @@
     </copy>
     <!-- Copy applicationContext file -->
     <copy file="src/java/applicationContext.xml" todir="${webapp.dir}/WEB-INF"/>
+    <!-- Copy Groovy scripts -->
+    <copy todir="${classes.dir}">
+      <fileset dir="src/java" includes="*.groovy"/>
+    </copy>
   </target>
 	
 </project>

Added: cocoon/branches/butterfly/src/java/org/apache/butterfly/components/pipeline/ConnectionResetException.java
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/components/pipeline/ConnectionResetException.java	Wed Aug  4 06:54:41 2004
@@ -0,0 +1,44 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.butterfly.components.pipeline;
+
+/**
+ * This Exception is thrown every time a component detects an exception
+ * due to a connection reset by peer.
+ *
+ */
+public class ConnectionResetException extends PipelineException {
+
+    /**
+     * Construct a new <code>ConnectionResetException</code> instance.
+     *
+     * @param message a <code>String</code> value
+     */
+    public ConnectionResetException(String message) {
+        super(message, null);
+    }
+
+    /**
+     * Construct a new <code>ConnectionResetException</code> that references
+     * a parent Exception.
+     *
+     * @param message a <code>String</code> value
+     * @param t a <code>Throwable</code> value
+     */
+    public ConnectionResetException(String message, Throwable t) {
+        super(message, t);
+    }
+}

Modified: cocoon/branches/butterfly/src/java/org/apache/butterfly/components/pipeline/impl/NonCachingProcessingPipeline.java
==============================================================================
--- cocoon/branches/butterfly/src/java/org/apache/butterfly/components/pipeline/impl/NonCachingProcessingPipeline.java	(original)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/components/pipeline/impl/NonCachingProcessingPipeline.java	Wed Aug  4 06:54:41 2004
@@ -17,10 +17,12 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.net.SocketException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.butterfly.components.pipeline.ConnectionResetException;
 import org.apache.butterfly.components.pipeline.InvalidPipelineException;
 import org.apache.butterfly.components.pipeline.PipelineProcessingException;
 import org.apache.butterfly.components.pipeline.ProcessingPipeline;
@@ -161,8 +163,41 @@
      * @see org.apache.butterfly.components.pipeline.ProcessingPipeline#process()
      */
     public boolean process(Environment environment) {
-        // TODO Auto-generated method stub
-        return false;
+        // If this is an internal request, lastConsumer was reset!
+        /*
+        if (null == this.lastConsumer) {
+            this.lastConsumer = this.serializer;
+        } else {
+            this.preparePipeline(environment);
+        }
+        if ( this.reader != null ) {
+            this.preparePipeline(environment);            
+        }
+        */
+        
+        // See if we need to set an "Expires:" header
+        /*
+        if (this.expires != 0) {
+            Response res = ObjectModelHelper.getResponse(environment.getObjectModel());
+            res.setDateHeader("Expires", System.currentTimeMillis() + expires);
+            res.setHeader("Cache-Control", "max-age=" + expires/1000 + ", public");
+            if (getLogger().isDebugEnabled()) {
+                getLogger().debug("Setting a new Expires object for this resource");
+            }
+            environment.getObjectModel().put(ObjectModelHelper.EXPIRES_OBJECT,
+                                             new Long(expires + System.currentTimeMillis()));
+        }
+        */
+        
+        if (this.reader != null) {
+            if (checkIfModified(environment, this.reader.getLastModified())) {
+                return true;
+            }
+            return this.processReader(environment);
+        } else {
+            this.connectPipeline(environment);
+            return this.processXMLPipeline(environment);
+        }
     }
 
     /* (non-Javadoc)
@@ -260,6 +295,47 @@
     public String getKeyForEventPipeline() {
         // TODO Auto-generated method stub
         return null;
+    }
+
+    protected boolean checkIfModified(Environment environment,
+                                        long lastModified) {
+        // has the read resource been modified?
+        if(!environment.isResponseModified(lastModified)) {
+            // environment supports this, so we are finished
+            environment.setResponseIsNotModified();
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Process the pipeline using a reader.
+     * @throws ProcessingException if
+     */
+    protected boolean processReader(Environment environment) {
+        try {
+            if (this.reader.shouldSetContentLength()) {
+                ByteArrayOutputStream os = new ByteArrayOutputStream();
+                this.reader.setOutputStream(os);
+                this.reader.generate();
+                environment.setContentLength(os.size());
+                os.writeTo(environment.getOutputStream(0));
+            } else {
+                this.reader.setOutputStream(environment.getOutputStream(this.outputBufferSize));
+                this.reader.generate();
+            }
+        } catch ( SocketException se ) {
+            if (se.getMessage().indexOf("reset") > 0
+                    || se.getMessage().indexOf("aborted") > 0
+                    || se.getMessage().indexOf("connection abort") > 0) {
+                throw new ConnectionResetException("Connection reset by peer", se);
+            } else {
+                throw new PipelineProcessingException("Failed to execute reader pipeline.", se);
+            }
+        } catch ( Exception e ) {
+            throw new PipelineProcessingException("Error executing reader pipeline.",e);
+        }
+        return true;
     }
 
 }

Modified: cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/ButterflyServlet.java
==============================================================================
--- cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/ButterflyServlet.java	(original)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/ButterflyServlet.java	Wed Aug  4 06:54:41 2004
@@ -15,6 +15,9 @@
  */
 package org.apache.butterfly.servlet;
 
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.GroovyObject;
+
 import java.io.IOException;
 import java.net.MalformedURLException;
 
@@ -30,6 +33,7 @@
 import org.apache.commons.lang.BooleanUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.codehaus.groovy.control.CompilationFailedException;
 import org.springframework.web.context.WebApplicationContext;
 import org.springframework.web.context.support.WebApplicationContextUtils;
 
@@ -103,7 +107,28 @@
 
         Environment env = getEnvironment(uri, req, res);
         
-        // TODO: process the request with the given environment
+        // Process the request with the given environment
+        ClassLoader parent = getClass().getClassLoader();
+        GroovyClassLoader loader = new GroovyClassLoader(parent);
+        try {
+            Class pipelineClass = loader.parseClass(getClass().getResourceAsStream("Pipeline.groovy"));
+            Class sitemapClass = loader.parseClass(getClass().getResourceAsStream("sitemap.groovy"));
+            GroovyObject sitemap = (GroovyObject) sitemapClass.newInstance();
+            sitemap.setProperty("beanFactory", this.applicationContext);
+            Object[] args = { uri };
+            sitemap.invokeMethod("setup", args);
+            sitemap.invokeMethod("process", new Object[] { env });
+        } catch (CompilationFailedException e) {
+            logger.error("Cannot compile Groovy sitemap.", e);
+            throw new ServletException(e);
+        } catch (InstantiationException e) {
+            logger.error("Cannot instantiate sitemap.", e);
+            throw new ServletException(e);
+        } catch (IllegalAccessException e) {
+            logger.error("Cannot access sitemap method.", e);
+            throw new ServletException(e);
+        }
+
     }
 
     /**

Added: cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/Pipeline.groovy
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/Pipeline.groovy	Wed Aug  4 06:54:41 2004
@@ -0,0 +1,52 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.butterfly.xml.dom.DOMBuilder
+import org.apache.butterfly.components.pipeline.impl.NonCachingProcessingPipeline
+
+class Pipeline {
+    public beanFactory;
+    private pipeline;
+    
+    protected Pipeline() {
+        this.pipeline = new NonCachingProcessingPipeline()
+    }
+            
+    protected void generate(src) {
+        generator = beanFactory.getBean("fileGenerator")
+        generator.inputSource = src
+        this.pipeline.generator = generator
+    }
+    
+    protected void transform(type, src) {
+        factory = beanFactory.getBean(type + "TransformerFactory")
+        transformer = factory.getTransformer(src)
+        this.pipeline.addTransformer(transformer)
+    }
+    
+    protected void serialize(type) {
+        serializer = beanFactory.getBean(type + "Serializer")
+        this.pipeline.serializer = serializer
+    }
+    
+    public void process(environment, consumer) {
+        this.pipeline.process(environment, consumer)
+    }
+    
+    public void process(environment) {
+        this.pipeline.process(environment)
+    }
+}
\ No newline at end of file

Added: cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/sitemap.groovy
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/sitemap.groovy	Wed Aug  4 06:54:41 2004
@@ -0,0 +1,28 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+class Sitemap extends Pipeline {
+  
+    void setup(String requestPath) {
+        if (requestPath =~ ".*\.html") {
+            generate "testdata/welcome.xml"
+            transform "trax", "testdata/welcome.xslt" 
+            serialize "xml"
+        }
+        else {
+            println("No matches for URI [" + requestPath + "]");
+        }
+    }
+}
\ No newline at end of file

Modified: cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy
==============================================================================
--- cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy	(original)
+++ cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy	Wed Aug  4 06:54:41 2004
@@ -45,4 +45,8 @@
     public void process(environment, consumer) {
         this.pipeline.process(environment, consumer)
     }
+    
+    public void process(environment) {
+        this.pipeline.process(environment)
+    }
 }

Added: cocoon/branches/butterfly/testdata/welcome.xml
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/testdata/welcome.xml	Wed Aug  4 06:54:41 2004
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?><!--
+  Copyright 1999-2004 The Apache Software Foundation
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+--><!--+
+    | This is a very simple XML page done just to show how a simple 
+    | cocoon pipeline works.
+    |
+    | CVS $Id: welcome.xml,v 1.5 2004/04/05 12:25:35 antonio Exp $
+    +--><welcome>
+ <message>
+   Congratulations! If you are reading this page, it means that
+   your Apache Cocoon installation was successful.
+ </message>
+
+
+  <message id="samples">
+   To know more about Cocoon capabilities, look at the <link href="samples/">samples</link>
+  </message>
+  
+</welcome>
\ No newline at end of file

Added: cocoon/branches/butterfly/testdata/welcome.xslt
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/testdata/welcome.xslt	Wed Aug  4 06:54:41 2004
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Copyright 1999-2004 The Apache Software Foundation
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+
+<!-- CVS $Id: welcome.xslt,v 1.11 2004/03/06 02:25:58 antonio Exp $ -->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                              xmlns="http://www.w3.org/1999/xhtml">
+
+  <xsl:param name="contextPath"/>
+
+  <xsl:template match="welcome">
+    <html xml:lang="en" lang="en">
+      <head>
+        <title>Welcome to Apache Cocoon!</title>
+        <!-- 
+             NOTE (SM): this meta tag reflects the *output* of the pipeline and not
+             the encoding of this file. I agree it's sort of an hack and it should
+             be the XHTML serializer to add the meta tag to the response, but, for
+             now, this fixes encoding problems in those user-agents that don't parse
+             the <?xml?> processing instruction to understand the encoding of the
+             stream 
+         --> 
+        <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8"/>
+        <link href="{$contextPath}/styles/main.css" type="text/css" rel="stylesheet"/>
+        <link href="favicon.ico" rel="SHORTCUT ICON" />
+      </head>
+      <body>
+        <h1>Welcome to Apache Cocoon!</h1>
+        <xsl:apply-templates/>
+        <p class="copyright">
+         Copyright © 1999-2004 <a href="http://www.apache.org/">The Apache Software Foundation</a>. All rights reserved.
+        </p>
+        <p class="block">
+          <a href="http://cocoon.apache.org/"><img src="{$contextPath}/images/powered.gif" alt="Powered by Apache Cocoon"/></a>
+        </p>
+      </body>
+    </html>
+  </xsl:template>
+
+  <xsl:template match="message">
+    <p class="block"><xsl:apply-templates/></p>
+  </xsl:template>
+  
+  <xsl:template match="link">
+    <a href="{@href}"><xsl:apply-templates/></a>
+  </xsl:template>
+
+</xsl:stylesheet>

Modified: cocoon/branches/butterfly/tools/jetty/conf/main.xml
==============================================================================
--- cocoon/branches/butterfly/tools/jetty/conf/main.xml	(original)
+++ cocoon/branches/butterfly/tools/jetty/conf/main.xml	Wed Aug  4 06:54:41 2004
@@ -49,25 +49,4 @@
     <Set name="defaultsDescriptor"><SystemProperty name="home" default="."/>/tools/jetty/conf/webdefaults.xml</Set>
   </Call>
 
-  <!-- =============================================================== -->
-  <!-- Configure the Authentication Realms                             -->
-  <!-- =============================================================== -->
-  <Call name="addRealm">
-    <Arg>
-      <New class="org.mortbay.http.HashUserRealm">
-        <Arg>Quicksilver</Arg>
-        <Arg><SystemProperty name="jetty.home" default="."/>/tools/jetty/conf/quicksilver.realm.properties</Arg>
-      </New>
-    </Arg>
-  </Call>
-  
-  <Call name="addRealm">
-    <Arg>
-      <New class="org.mortbay.http.HashUserRealm">
-        <Arg>PetStore</Arg>
-        <Arg><SystemProperty name="jetty.home" default="."/>/tools/jetty/conf/petstore.realm.properties</Arg>
-      </New>
-    </Arg>
-  </Call>
-
 </Configure>