cziegeler    2004/01/06 04:49:26

  Modified:    src/java/org/apache/cocoon/components/source/impl
                        SitemapSource.java SitemapSourceFactory.java
               src/java/org/apache/cocoon/environment/wrapper
                        EnvironmentWrapper.java
  Added:       src/java/org/apache/cocoon/components/source/impl
                        SitemapSourceInfo.java
  Log:
  Refactor sitemap protocol handling a little bit; remove duplicate code
  
  Revision  Changes    Path
  1.20      +11 -84    
cocoon-2.2/src/java/org/apache/cocoon/components/source/impl/SitemapSource.java
  
  Index: SitemapSource.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/source/impl/SitemapSource.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- SitemapSource.java        5 Jan 2004 12:41:48 -0000       1.19
  +++ SitemapSource.java        6 Jan 2004 12:49:26 -0000       1.20
  @@ -60,9 +60,7 @@
   
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.Logger;
  -import org.apache.avalon.framework.service.ServiceException;
   import org.apache.avalon.framework.service.ServiceManager;
  -import org.apache.cocoon.Constants;
   import org.apache.cocoon.Processor;
   import org.apache.cocoon.ResourceNotFoundException;
   import org.apache.cocoon.components.pipeline.ProcessingPipeline;
  @@ -104,9 +102,6 @@
       /** The system id used for caching */
       private String systemIdForCaching;
       
  -    /** The uri */
  -//    private String uri;
  -
       /** The current ServiceManager */
       private ServiceManager manager;
   
  @@ -119,9 +114,6 @@
       /** The environment */
       private MutableEnvironmentFacade environment;
   
  -    /** The prefix for the processing */
  -//    private String prefix;
  -
       /** The <code>ProcessingPipeline</code> */
       private ProcessingPipeline processingPipeline;
   
  @@ -163,84 +155,20 @@
           this.manager = manager;
           this.enableLogging(logger);
   
  -        boolean rawMode = false;
  -
  -        // remove the protocol
  -        int position = uri.indexOf(':') + 1;
  -        if (position != 0) {
  -            this.protocol = uri.substring(0, position-1);
  -            // check for subprotocol
  -            if (uri.startsWith("raw:", position)) {
  -                position += 4;
  -                rawMode = true;
  -            }
  -        } else {
  -            throw new MalformedURLException("No protocol found for sitemap 
source in " + uri);
  -        }
  +        SitemapSourceInfo info = SitemapSourceInfo.parseURI(env, uri);
   
           // does the uri point to this sitemap or to the root sitemap?
           String prefix;
  -        if (uri.startsWith("//", position)) {
  -            position += 2;
  -            try {
  -                this.processor = 
(Processor)this.manager.lookup(Processor.ROLE);
  -            } catch (ServiceException e) {
  -                throw new MalformedURLException("Cannot get Processor 
instance");
  -            }
  -            prefix = ""; // start at the root
  -        } else if (uri.startsWith("/", position)) {
  -            position ++;
  -            prefix = null;
  -            this.processor = EnvironmentHelper.getCurrentProcessor();
  -        } else {
  -            throw new MalformedURLException("Malformed cocoon URI: " + uri);
  -        }
  -
  -        // create the queryString (if available)
  -        String queryString = null;
  -        int queryStringPos = uri.indexOf('?', position);
  -        if (queryStringPos != -1) {
  -            queryString = uri.substring(queryStringPos + 1);
  -            uri = uri.substring(position, queryStringPos);
  -        } else if (position > 0) {
  -            uri = uri.substring(position);
  -        }
  -        
  -        // determine if the queryString specifies a cocoon-view
  -        String view = null;
  -        if (queryString != null) {
  -            int index = queryString.indexOf(Constants.VIEW_PARAM);
  -            if (index != -1 
  -                && (index == 0 || queryString.charAt(index-1) == '&')
  -                && queryString.length() > index + 
Constants.VIEW_PARAM.length() 
  -                && queryString.charAt(index+Constants.VIEW_PARAM.length()) 
== '=') {
  -                
  -                String tmp = 
queryString.substring(index+Constants.VIEW_PARAM.length()+1);
  -                index = tmp.indexOf('&');
  -                if (index != -1) {
  -                    view = tmp.substring(0,index);
  -                } else {
  -                    view = tmp;
  -                }
  -            } else {
  -                view = env.getView();
  -            }
  +        if (info.prefix.length() == 0) {
  +            this.processor = 
EnvironmentHelper.getCurrentProcessor().getRootProcessor();
           } else {
  -            view = env.getView();
  +            this.processor = EnvironmentHelper.getCurrentProcessor();
           }
   
  -        // build the request uri which is relative to the context
  -        String requestURI = (prefix == null ? env.getURIPrefix() + uri : 
uri);
  -
  -        // create system ID
  -        this.systemId = queryString == null ?
  -            this.protocol + "://" + requestURI :
  -            this.protocol + "://" + requestURI + "?" + queryString;
  -
           // create environment...
  -        EnvironmentWrapper wrapper = new EnvironmentWrapper(env, requestURI, 
  -                                                   queryString, logger, 
manager, rawMode, view);
  -        wrapper.setURI(prefix, uri);
  +        EnvironmentWrapper wrapper = new EnvironmentWrapper(env, 
info.requestURI, 
  +                                                   info.queryString, logger, 
manager, info.rawMode, info.view);
  +        wrapper.setURI(info.prefix, uri);
           
           // The environment is a facade whose delegate can be changed in case 
of internal redirects
           this.environment = new MutableEnvironmentFacade(wrapper);
  @@ -284,7 +212,7 @@
        * Return an <code>InputStream</code> object to read from the source.
        */
       public InputStream getInputStream()
  -      throws IOException, SourceException {
  +    throws IOException, SourceException {
   
           if (this.needsRefresh) {
               this.refresh();
  @@ -381,7 +309,6 @@
               this.processKey = 
EnvironmentHelper.startProcessing(this.environment);
               this.processingPipeline = 
this.processor.buildPipeline(this.environment);
               this.pipelineProcessor = 
EnvironmentHelper.getLastProcessor(this.environment);
  -            //FIXME - set the context of the environment to the context of 
the pipeline processor
               
this.pipelineProcessor.getEnvironmentHelper().setContext(this.environment);
   
               String redirectURL = this.environment.getRedirectURL();
  @@ -435,7 +362,7 @@
        * Stream content to the content handler
        */
       public void toSAX(ContentHandler contentHandler)
  -        throws SAXException
  +    throws SAXException
       {
           if (this.needsRefresh) {
               this.refresh();
  @@ -507,6 +434,7 @@
           this.reset();
           if ( this.sourceResolver != null ) {
               this.manager.release( this.sourceResolver );
  +            this.sourceResolver = null;
           }
       }
   
  @@ -536,6 +464,5 @@
       public Iterator getParameterNames() {
           return java.util.Collections.EMPTY_LIST.iterator();
       }
  -
   
   }
  
  
  
  1.6       +2 -3      
cocoon-2.2/src/java/org/apache/cocoon/components/source/impl/SitemapSourceFactory.java
  
  Index: SitemapSourceFactory.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/src/java/org/apache/cocoon/components/source/impl/SitemapSourceFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SitemapSourceFactory.java 26 Dec 2003 18:43:39 -0000      1.5
  +++ SitemapSourceFactory.java 6 Jan 2004 12:49:26 -0000       1.6
  @@ -59,11 +59,10 @@
   import org.apache.avalon.framework.service.ServiceManager;
   import org.apache.avalon.framework.service.Serviceable;
   import org.apache.avalon.framework.thread.ThreadSafe;
  -
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceFactory;
  -import org.apache.excalibur.source.URIAbsolutizer;
   import org.apache.excalibur.source.SourceUtil;
  +import org.apache.excalibur.source.URIAbsolutizer;
   
   /**
    * This class implements the cocoon: protocol.
  
  
  
  1.1                  
cocoon-2.2/src/java/org/apache/cocoon/components/source/impl/SitemapSourceInfo.java
  
  Index: SitemapSourceInfo.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.source.impl;
  
  import java.net.MalformedURLException;
  
  import org.apache.cocoon.Constants;
  import org.apache.cocoon.environment.Environment;
  
  /**
   * This is a helper class for the cocoon protocol.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
   * @version CVS $Id: SitemapSourceInfo.java,v 1.1 2004/01/06 12:49:26 
cziegeler Exp $
   */
  public final class SitemapSourceInfo {
      
      public boolean rawMode;
      public String protocol;
      public String requestURI;
      public String systemId;
      public String view;
      public String prefix;
      public String queryString;
      public String uri;
      
      public static SitemapSourceInfo parseURI(Environment env, String 
sitemapURI) 
      throws MalformedURLException {
          SitemapSourceInfo info = new SitemapSourceInfo();
          info.rawMode = false;
  
          // remove the protocol
          int position = sitemapURI.indexOf(':') + 1;
          if (position != 0) {
              info.protocol = sitemapURI.substring(0, position-1);
              // check for subprotocol
              if (sitemapURI.startsWith("raw:", position)) {
                  position += 4;
                  info.rawMode = true;
              }
          } else {
              throw new MalformedURLException("No protocol found for sitemap 
source in " + sitemapURI);
          }
  
          // does the uri point to this sitemap or to the root sitemap?
          if (sitemapURI.startsWith("//", position)) {
              position += 2;
              info.prefix = ""; // start at the root
          } else if (sitemapURI.startsWith("/", position)) {
              position ++;
              info.prefix = env.getURIPrefix();
          } else {
              throw new MalformedURLException("Malformed cocoon URI: " + 
sitemapURI);
          }
  
          // create the queryString (if available)
          int queryStringPos = sitemapURI.indexOf('?', position);
          if (queryStringPos != -1) {
              info.queryString = sitemapURI.substring(queryStringPos + 1);
              info.uri = sitemapURI.substring(position, queryStringPos);
          } else if (position > 0) {
              info.uri = sitemapURI.substring(position);
          }
  
          
          // determine if the queryString specifies a cocoon-view
          info.view = null;
          if (info.queryString != null) {
              int index = info.queryString.indexOf(Constants.VIEW_PARAM);
              if (index != -1 
                      && (index == 0 || info.queryString.charAt(index-1) == '&')
                      && info.queryString.length() > index + 
Constants.VIEW_PARAM.length() 
                      && 
info.queryString.charAt(index+Constants.VIEW_PARAM.length()) == '=') {
                  
                  String tmp = 
info.queryString.substring(index+Constants.VIEW_PARAM.length()+1);
                  index = tmp.indexOf('&');
                  if (index != -1) {
                      info.view = tmp.substring(0,index);
                  } else {
                      info.view = tmp;
                  }
              } else {
                  info.view = env.getView();
              }
          } else {
              info.view = env.getView();
          }
  
          // build the request uri which is relative to the context
          info.requestURI = info.prefix + info.uri;
  
          // create system ID
          final StringBuffer buffer = new StringBuffer(info.protocol);
          buffer.append("://").append(info.requestURI);
          if (info.queryString != null ) {
              buffer.append('?').append(info.queryString);
          }
          info.systemId = buffer.toString();
  
          return info;
      }
      
  }
  
  
  
  1.16      +21 -114   
cocoon-2.2/src/java/org/apache/cocoon/environment/wrapper/EnvironmentWrapper.java
  
  Index: EnvironmentWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.2/src/java/org/apache/cocoon/environment/wrapper/EnvironmentWrapper.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- EnvironmentWrapper.java   31 Oct 2003 07:22:35 -0000      1.15
  +++ EnvironmentWrapper.java   6 Jan 2004 12:49:26 -0000       1.16
  @@ -59,7 +59,7 @@
   
   import org.apache.avalon.framework.logger.Logger;
   import org.apache.avalon.framework.service.ServiceManager;
  -import org.apache.cocoon.Constants;
  +import org.apache.cocoon.components.source.impl.SitemapSourceInfo;
   import org.apache.cocoon.environment.AbstractEnvironment;
   import org.apache.cocoon.environment.Environment;
   import org.apache.cocoon.environment.ObjectModelHelper;
  @@ -104,20 +104,7 @@
                                 String      queryString,
                                 Logger      logger)
       throws MalformedURLException {
  -        this(env, requestURI, queryString, logger, false);
  -    }
  -
  -    /**
  -     * Constructs an EnvironmentWrapper object from a Request
  -     * and Response objects
  -     */
  -    public EnvironmentWrapper(Environment env,
  -                              String      requestURI,
  -                              String      queryString,
  -                              Logger      logger,
  -                              boolean     rawMode)
  -    throws MalformedURLException {
  -        this(env, requestURI, queryString, logger, null, rawMode);
  +        this(env, requestURI, queryString, logger, null, false, null);
       }
   
       /**
  @@ -128,20 +115,6 @@
                                 String           requestURI,
                                 String           queryString,
                                 Logger           logger,
  -                              ServiceManager   manager,
  -                              boolean          rawMode)
  -    throws MalformedURLException {
  -        this(env, requestURI, queryString, logger, null, 
rawMode,env.getView());
  -    }
  -    
  -    /**
  -     * Constructs an EnvironmentWrapper object from a Request
  -     * and Response objects
  -     */
  -    public EnvironmentWrapper(Environment      env,
  -                              String           requestURI,
  -                              String           queryString,
  -                              Logger           logger,
                                 ServiceManager manager,
                                 boolean          rawMode,
                                 String           view)
  @@ -150,6 +123,24 @@
           init(env, requestURI, queryString, logger, manager, rawMode, view);
       }
   
  +    /**
  +     * Constructor
  +     * @param env
  +     * @param manager
  +     * @param uri
  +     * @param logger
  +     * @throws MalformedURLException
  +     */
  +    public EnvironmentWrapper(Environment env, ServiceManager manager, 
String uri,  Logger logger)  throws MalformedURLException {
  +        super(env.getURI(), env.getView(), env.getAction());
  +
  +        SitemapSourceInfo info = SitemapSourceInfo.parseURI(env, uri);
  +
  +        this.init(env, info.requestURI, info.queryString, logger, manager, 
info.rawMode, info.view);
  +        this.setURI(info.prefix, info.uri);
  +        
  +    }
  +    
       private void init(Environment      env,
                                 String           requestURI,
                                 String           queryString,
  @@ -184,90 +175,6 @@
           this.objectModel.put(ObjectModelHelper.REQUEST_OBJECT, this.request);
       }
      
  -    public EnvironmentWrapper(Environment env, ServiceManager manager, 
String uri,  Logger logger)  throws MalformedURLException {
  -        super(env.getURI(), env.getView(), env.getAction());
  -
  -        // FIXME(SW): code stolen from SitemapSource. Factorize somewhere...
  -        boolean rawMode = false;
  -
  -        // remove the protocol
  -        int position = uri.indexOf(':') + 1;
  -        if (position != 0) {
  -//            this.protocol = uri.substring(0, position-1);
  -            // check for subprotocol
  -            if (uri.startsWith("raw:", position)) {
  -                position += 4;
  -                rawMode = true;
  -            }
  -        } else {
  -            throw new MalformedURLException("No protocol found for sitemap 
source in " + uri);
  -        }
  -
  -        // does the uri point to this sitemap or to the root sitemap?
  -        String prefix;
  -        if (uri.startsWith("//", position)) {
  -            position += 2;
  -//            try {
  -//                this.processor = 
(Processor)this.manager.lookup(Processor.ROLE);
  -//            } catch (ComponentException e) {
  -//                throw new MalformedURLException("Cannot get Processor 
instance");
  -//            }
  -            prefix = ""; // start at the root
  -        } else if (uri.startsWith("/", position)) {
  -            position ++;
  -            prefix = null;
  -//            this.processor = CocoonComponentManager.getCurrentProcessor();
  -        } else {
  -            throw new MalformedURLException("Malformed cocoon URI: " + uri);
  -        }
  -
  -        // create the queryString (if available)
  -        String queryString = null;
  -        int queryStringPos = uri.indexOf('?', position);
  -        if (queryStringPos != -1) {
  -            queryString = uri.substring(queryStringPos + 1);
  -            uri = uri.substring(position, queryStringPos);
  -        } else if (position > 0) {
  -            uri = uri.substring(position);
  -        }
  -
  -        
  -        // determine if the queryString specifies a cocoon-view
  -        String view = null;
  -        if (queryString != null) {
  -            int index = queryString.indexOf(Constants.VIEW_PARAM);
  -            if (index != -1 
  -                && (index == 0 || queryString.charAt(index-1) == '&')
  -                && queryString.length() > index + 
Constants.VIEW_PARAM.length() 
  -                && queryString.charAt(index+Constants.VIEW_PARAM.length()) 
== '=') {
  -                
  -                String tmp = 
queryString.substring(index+Constants.VIEW_PARAM.length()+1);
  -                index = tmp.indexOf('&');
  -                if (index != -1) {
  -                    view = tmp.substring(0,index);
  -                } else {
  -                    view = tmp;
  -                }
  -            } else {
  -                view = env.getView();
  -            }
  -        } else {
  -            view = env.getView();
  -        }
  -
  -        // build the request uri which is relative to the context
  -        String requestURI = (prefix == null ? env.getURIPrefix() + uri : 
uri);
  -
  -//        // create system ID
  -//        this.systemId = queryString == null ?
  -//            this.protocol + "://" + requestURI :
  -//            this.protocol + "://" + requestURI + "?" + queryString;
  -
  -        this.init(env, requestURI, queryString, logger, manager, rawMode, 
view);
  -        this.setURI((prefix == null ? env.getURIPrefix() : ""), uri);
  -        
  -    }
  -
       /**
        * Redirect the client to a new URL is not allowed
        */
  
  
  

Reply via email to