cziegeler    01/07/11 23:34:20

  Modified:    src/org/apache/cocoon/components/source SitemapSource.java
               src/org/apache/cocoon/environment/wrapper
                        EnvironmentWrapper.java
  Log:
  - SitemapSource calculates now a modification date if the underlying EventPipeline
    is cacheable. This enables caching of CA and every use of the cocoon: urls
  - If a cocoon: url points to a pipeline with a redirect this redirect is followed
  
  Revision  Changes    Path
  1.10      +99 -47    
xml-cocoon2/src/org/apache/cocoon/components/source/SitemapSource.java
  
  Index: SitemapSource.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/org/apache/cocoon/components/source/SitemapSource.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SitemapSource.java        2001/07/10 10:10:49     1.9
  +++ SitemapSource.java        2001/07/12 06:34:13     1.10
  @@ -19,13 +19,17 @@
   import java.io.Reader;
   import java.net.URL;
   import java.net.URLConnection;
  +import java.util.Map;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentManager;
   import org.apache.cocoon.Constants;
   import org.apache.cocoon.Processor;
   import org.apache.cocoon.ProcessingException;
  +import org.apache.cocoon.caching.PipelineCacheKey;
  +import org.apache.cocoon.components.pipeline.CacheableEventPipeline;
   import org.apache.cocoon.components.pipeline.EventPipeline;
   import org.apache.cocoon.components.pipeline.StreamPipeline;
  +import org.apache.cocoon.components.source.SourceHandler;
   import org.apache.cocoon.environment.Environment;
   import org.apache.cocoon.environment.Request;
   import org.apache.cocoon.environment.Source;
  @@ -33,6 +37,8 @@
   import org.apache.cocoon.serialization.Serializer;
   import org.apache.cocoon.sitemap.Sitemap;
   import org.apache.cocoon.sitemap.SitemapComponentSelector;
  +import org.apache.cocoon.util.HashUtil;
  +import org.apache.cocoon.xml.AbstractXMLConsumer;
   import org.apache.cocoon.xml.ContentHandlerWrapper;
   import org.apache.cocoon.xml.XMLConsumer;
   import org.apache.cocoon.xml.XMLProducer;
  @@ -44,18 +50,16 @@
    * Description of a source which is defined by a pipeline.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
  - * @version CVS $Revision: 1.9 $ $Date: 2001/07/10 10:10:49 $
  + * @version CVS $Revision: 1.10 $ $Date: 2001/07/12 06:34:13 $
    */
   
   public final class SitemapSource
  +extends AbstractXMLConsumer
   implements Source {
   
       /** The last modification date or 0 */
       private long lastModificationDate;
   
  -    /** The content length */
  -    private long contentLength;
  -
       /** The system id */
       private String systemId;
   
  @@ -74,6 +78,15 @@
       /** The prefix for the processing */
       private String prefix;
   
  +    /** The <code>EventPipeline</code> */
  +    private EventPipeline eventPipeline;
  +
  +    /** The <code>StreamPipeline</code> */
  +    private StreamPipeline pipeline;
  +
  +    /** The redirect <code>Source</code> */
  +    private Source redirectSource;
  +
       /**
        * Construct a new object
        */
  @@ -129,8 +142,7 @@
               uri = uri.substring(0, queryStringPos);
           }
           this.uri = uri;
  -        this.contentLength = -1;
  -        this.lastModificationDate = 0;
  +        this.refresh();
       }
   
       /**
  @@ -146,7 +158,7 @@
        * is not possible to determine the length.
        */
       public long getContentLength() {
  -        return this.contentLength;
  +        return -1;
       }
   
       /**
  @@ -154,47 +166,35 @@
        */
       public InputStream getInputStream()
       throws ProcessingException, IOException {
  -        EventPipeline eventPipeline = null;
  -        StreamPipeline pipeline = null;
  +        if ((this.eventPipeline == null || this.pipeline == null) && 
this.redirectSource == null) {
  +            throw new ProcessingException("could not lookup pipeline components.");
  +        }
           SitemapComponentSelector serializerSelector = null;
           Serializer serializer = null;
           try {
   
  -            eventPipeline = (EventPipeline)this.manager.lookup(EventPipeline.ROLE);
  -            pipeline = (StreamPipeline)this.manager.lookup(StreamPipeline.ROLE);
               serializerSelector = (SitemapComponentSelector) 
this.manager.lookup(Serializer.ROLE + "Selector");
               serializer = (Serializer)serializerSelector.select("xml");
               ByteArrayOutputStream os = new ByteArrayOutputStream();
               serializer.setOutputStream(os);
   
  -            pipeline.setEventPipeline(eventPipeline);
  +            this.stream(serializer);
   
  -            try {
  -                this.environment.pushURI(this.prefix, this.uri);
  -                this.processor.process(this.environment, pipeline, eventPipeline);
  -            } finally {
  -                this.environment.popURI();
  -            }
  -            try {
  -                this.environment.pushURI(this.prefix, this.uri);
  -                ((XMLProducer)eventPipeline).setConsumer(serializer);
  -                eventPipeline.process(this.environment);
  -            } finally {
  -                this.environment.popURI();
  -            }
  -
               return new ByteArrayInputStream(os.toByteArray());
           } catch (ComponentException cme) {
               throw new ProcessingException("could not lookup pipeline components", 
cme);
           } catch (Exception e) {
               throw new ProcessingException("Exception during processing of 
"+this.systemId, e);
           } finally {
  -            if (serializer != null) {
  -                serializerSelector.release(serializer);
  -            }
  +            if (serializer != null) serializerSelector.release(serializer);
               if (serializerSelector != null) 
this.manager.release(serializerSelector);
  -            if (eventPipeline != null) this.manager.release(eventPipeline);
  -            if (pipeline != null) this.manager.release(pipeline);
  +            if (this.eventPipeline != null) 
this.manager.release(this.eventPipeline);
  +            if (this.pipeline != null) this.manager.release(this.pipeline);
  +            this.eventPipeline = null;
  +            this.pipeline = null;
  +            this.lastModificationDate = 0;
  +            this.environment.reset();
  +            this.redirectSource = null;
           }
       }
   
  @@ -226,7 +226,60 @@
        * and content length.
        */
       public void refresh() {
  -        // nothing to do here
  +        if (eventPipeline != null) this.manager.release(eventPipeline);
  +        if (pipeline != null) this.manager.release(pipeline);
  +        this.lastModificationDate = 0;
  +        this.eventPipeline = null;
  +        this.pipeline = null;
  +        this.environment.reset();
  +        redirectSource = null;
  +        try {
  +            // initialize the pipelines
  +            this.eventPipeline = 
(EventPipeline)this.manager.lookup(EventPipeline.ROLE);
  +            this.pipeline = 
(StreamPipeline)this.manager.lookup(StreamPipeline.ROLE);
  +
  +            this.pipeline.setEventPipeline(eventPipeline);
  +            // set dummy consumer
  +            ((XMLProducer)eventPipeline).setConsumer(this);
  +
  +            try {
  +                this.environment.pushURI(this.prefix, this.uri);
  +                this.processor.process(this.environment, pipeline, eventPipeline);
  +            } finally {
  +                this.environment.popURI();
  +            }
  +            String redirectURL = this.environment.getRedirectURL();
  +            if (redirectURL == null) {
  +                if (this.eventPipeline instanceof CacheableEventPipeline) {
  +                    CacheableEventPipeline cep = 
(CacheableEventPipeline)this.eventPipeline;
  +                    PipelineCacheKey pck = cep.generateKey(this.environment);
  +                    Map validity = null;
  +                    if (pck != null) {
  +                        validity = cep.generateValidity(this.environment);
  +                        if (validity != null) {
  +                            // the event pipeline is cacheable
  +                            // now calculate a last modification date
  +                            String hashKey = pck.toString() + validity.toString();
  +                            System.out.println("Building hash key:" + hashKey);
  +                            this.lastModificationDate = HashUtil.hash(hashKey);
  +                            System.out.println("Hash:" + this.lastModificationDate);
  +                        }
  +                    }
  +                }
  +            } else {
  +                if (redirectURL.indexOf(":") == -1) {
  +                    redirectURL = "cocoon:/" + redirectURL;
  +                }
  +                this.redirectSource = this.environment.resolve(redirectURL);
  +                this.lastModificationDate = this.redirectSource.getLastModified();
  +            }
  +        } catch (Exception e) {
  +            e.printStackTrace();
  +            this.eventPipeline = null;
  +            this.pipeline = null;
  +            this.redirectSource = null;
  +            this.environment.reset();
  +        }
       }
   
       /**
  @@ -244,35 +297,34 @@
        */
       public void stream(XMLConsumer consumer)
       throws ProcessingException, SAXException, IOException {
  -        EventPipeline eventPipeline = null;
  -        StreamPipeline pipeline = null;
  +        if ((this.eventPipeline == null || this.pipeline == null) && 
this.redirectSource == null) {
  +            throw new ProcessingException("could not lookup pipeline components.");
  +        }
           try {
  -
  -            eventPipeline = (EventPipeline)this.manager.lookup(EventPipeline.ROLE);
  -            pipeline = (StreamPipeline)this.manager.lookup(StreamPipeline.ROLE);
   
  -            pipeline.setEventPipeline(eventPipeline);
  -
  -            try {
  -                this.environment.pushURI(this.prefix, this.uri);
  -                this.processor.process(this.environment, pipeline, eventPipeline);
  -            } finally {
  -                this.environment.popURI();
  -            }
  +            if (this.redirectSource != null) {
  +                this.redirectSource.stream(consumer);
  +            } else {
               try {
                   this.environment.pushURI(this.prefix, this.uri);
                   ((XMLProducer)eventPipeline).setConsumer(consumer);
                   eventPipeline.process(this.environment);
               } finally {
                   this.environment.popURI();
  +                }
               }
           } catch (ComponentException cme) {
               throw new ProcessingException("could not lookup pipeline components", 
cme);
           } catch (Exception e) {
               throw new ProcessingException("Exception during processing of 
"+this.systemId, e);
           } finally {
  -            if (eventPipeline != null) this.manager.release(eventPipeline);
  -            if (pipeline != null) this.manager.release(pipeline);
  +            if (this.eventPipeline != null) 
this.manager.release(this.eventPipeline);
  +            if (this.pipeline != null) this.manager.release(this.pipeline);
  +            this.eventPipeline = null;
  +            this.pipeline = null;
  +            this.lastModificationDate = 0;
  +            this.environment.reset();
  +            this.redirectSource = null;
           }
       }
   
  
  
  
  1.3       +35 -7     
xml-cocoon2/src/org/apache/cocoon/environment/wrapper/EnvironmentWrapper.java
  
  Index: EnvironmentWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/org/apache/cocoon/environment/wrapper/EnvironmentWrapper.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- EnvironmentWrapper.java   2001/07/10 10:10:54     1.2
  +++ EnvironmentWrapper.java   2001/07/12 06:34:17     1.3
  @@ -21,9 +21,10 @@
   import org.apache.cocoon.Constants;
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.source.SourceHandler;
  +import org.apache.cocoon.environment.Environment;
   import org.apache.cocoon.environment.Request;
   import org.apache.cocoon.environment.Response;
  -import org.apache.cocoon.environment.Environment;
  +import org.apache.cocoon.environment.Session;
   import org.apache.cocoon.environment.Source;
   
   
  @@ -33,7 +34,7 @@
    * contains a <code>RequestWrapper</code> object.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
  - * @version $Id: EnvironmentWrapper.java,v 1.2 2001/07/10 10:10:54 cziegeler Exp $
  + * @version $Id: EnvironmentWrapper.java,v 1.3 2001/07/12 06:34:17 cziegeler Exp $
    */
   public final class EnvironmentWrapper
   implements Environment {
  @@ -44,6 +45,12 @@
       /** The object model */
       private Map objectModel;
   
  +    /** The redirect url */
  +    private String redirectURL;
  +
  +    /** The request object */
  +    private Request request;
  +
       /**
        * Constructs an EnvironmentWrapper object from a Request
        * and Response objects
  @@ -61,9 +68,9 @@
               key = (String)keys.next();
               this.objectModel.put(key, oldObjectModel.get(key));
           }
  -        this.objectModel.put(Constants.REQUEST_OBJECT,
  -                             new 
RequestWrapper((Request)oldObjectModel.get(Constants.REQUEST_OBJECT),
  -                                                 requestURI, queryString));
  +        this.request = new 
RequestWrapper((Request)oldObjectModel.get(Constants.REQUEST_OBJECT),
  +                                                 requestURI, queryString);
  +        this.objectModel.put(Constants.REQUEST_OBJECT, this.request);
       }
   
   
  @@ -76,8 +83,29 @@
       /**
        * Redirect the client to a new URL is not allowed
        */
  -    public void redirect(boolean sessionmode, String newURL) throws IOException {
  -      throw new IOException("Redirecting to " + newURL + " not allowed.");
  +    public void redirect(boolean sessionmode, String newURL)
  +    throws IOException {
  +        this.redirectURL = newURL;
  +
  +        // check if session mode shall be activated
  +        if (sessionmode == true) {
  +            // The session
  +            Session session = null;
  +            // get session from request, or create new session
  +            session = request.getSession(true);
  +        }
  +    }
  +
  +    /**
  +     * if a redirect should happen this returns the url,
  +     * otherwise <code>null</code> is returned
  +     */
  +    public String getRedirectURL() {
  +        return this.redirectURL;
  +    }
  +
  +    public void reset() {
  +        this.redirectURL = null;
       }
   
       /**
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to