cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nicola...@apache.org
Subject cvs commit: xml-cocoon2/src/scratchpad/src/org/apache/cocoon/components/source SlideSource.java SlideSourceFactory.java
Date Wed, 22 May 2002 15:28:38 GMT
nicolaken    02/05/22 08:28:38

  Added:       src/scratchpad/src/org/apache/cocoon/components/source
                        SlideSource.java SlideSourceFactory.java
  Log:
  [PATCH] SourceFactory for jakarta slide  (Bug#: 9267 )
  submitter by
  stephan@vern.chem.tu-berlin.de (Stephan Michels)
  
  to use: build installscratchpadwar and then remove the comments for
  the webdav servlet and mapping in web.xml.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/scratchpad/src/org/apache/cocoon/components/source/SlideSource.java
  
  Index: SlideSource.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 apache@apache.org.
   *
   * 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 (INCLUDING, 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.cocoon.components.source;
  
  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.thread.ThreadSafe;
  
  import org.apache.cocoon.Constants;
  import org.apache.cocoon.components.source.WriteableSource;
  
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceException;
  import org.apache.excalibur.source.SourceFactory;
  import org.apache.excalibur.source.SourceValidity;
  import org.apache.excalibur.source.impl.validity.TimeStampValidity;
  
  import org.apache.slide.authenticate.CredentialsToken;
  import org.apache.slide.authenticate.SecurityToken;
  import org.apache.slide.common.Domain;
  import org.apache.slide.common.NamespaceAccessToken;
  import org.apache.slide.common.ServiceAccessException;
  import org.apache.slide.common.SlideToken;
  import org.apache.slide.common.SlideTokenImpl;
  import org.apache.slide.content.Content;
  import org.apache.slide.content.NodeProperty;
  import org.apache.slide.content.NodeRevisionContent;
  import org.apache.slide.content.NodeRevisionDescriptor;
  import org.apache.slide.content.NodeRevisionDescriptors;
  import org.apache.slide.content.RevisionContentNotFoundException;
  import org.apache.slide.content.RevisionDescriptorNotFoundException;
  import org.apache.slide.content.RevisionNotFoundException;
  import org.apache.slide.content.NodeRevisionNumber;
  import org.apache.slide.lock.Lock;
  import org.apache.slide.lock.ObjectLockedException;
  import org.apache.slide.macro.Macro;
  import org.apache.slide.security.AccessDeniedException;
  import org.apache.slide.security.Security;
  import org.apache.slide.structure.LinkedObjectNotFoundException;
  import org.apache.slide.structure.ObjectNode;
  import org.apache.slide.structure.ObjectNotFoundException;
  import org.apache.slide.structure.Structure;
  
  import java.io.ByteArrayOutputStream;
  import java.io.InputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.Date;
  import java.util.Map;
  
  /**
   * A sources from jakarta slide domains. The sources wrappers
   * to the stores of slide. It uses slide:// as pseudo-protocoll. It is also 
   * possible to get access to the store 
   * via webdav.
   *
   * Possible parameter are username and revision
   *
   * @author <a href="mailto:stephan@vern.chem.tu-berlin.de">Stephan Michels</a>
   * @version $Id: SlideSource.java,v 1.1 2002/05/22 15:28:38 nicolaken Exp $
   */
  public class SlideSource implements Source, WriteableSource
  {
      /** Sytem id */
      private String systemid;
  
      /** Namespace access token. */
      private NamespaceAccessToken token;
      
      /** Structure helper. */
      private Structure structure;
      
      /** Content helper. */
      private Content content;
     
      /** Security helper. */
      private Security security;
  
  		/** Lock helper. */
  		private Lock lock;
  
  		/** Macro helper. */
  		private Macro macro;
  
  		/** Slide token. */
  		private SlideToken slideToken;
  
  		/** Request URI */
  		private String requestUri;
  
  		/** Revision number */
  		private NodeRevisionNumber revisionNumber;
  
  		private SourceValidity validity;
  
  		private SlideSourceOutputStream outputstream;
  
  		protected SlideSource(String location, Map parameters) {
  				this.systemid = location;
  
  				if (location.indexOf("://") >= 0) 
  						location = location.substring(location.indexOf("://")+3);
  
  				String namespace = location.substring(0, location.indexOf("/"));
  				//System.out.println("namespace = '"+namespace+"'");
  				location = location.substring(location.indexOf("/"));
  				//System.out.println("location = '"+location+"'");
  
  				if ((parameters!=null) && (parameters.containsKey("revision"))) {
  						this.revisionNumber = new NodeRevisionNumber(parameters.get("revision").toString());
  						//System.out.println("revision = '"+this.revisionNumber+"'");
  				} else {
              this.revisionNumber = null;
          }
  
  				this.token = Domain.accessNamespace(new SecurityToken(new String()),
  																						namespace);
  
  				this.structure = token.getStructureHelper();
  				this.content = token.getContentHelper();
  				this.security = token.getSecurityHelper();
  				this.lock = token.getLockHelper();
  				this.macro = token.getMacroHelper();
  
  				CredentialsToken credToken;
  				if ((parameters!=null) && (parameters.containsKey("username")))
  						credToken = new CredentialsToken(parameters.get("username").toString());
  				else
  						credToken = new CredentialsToken(new String("guest"));
  
  				this.slideToken = new SlideTokenImpl(credToken);
  
  				this.requestUri = location;
  		}
  
  		/**
  		 * Return an <code>InputStream</code> object to read from the source.
  		 * This is the data at the point of invocation of this method,
  		 * so if this is Modifiable, you might get different content
  		 * from two different invocations.
  		 */
  		public InputStream getInputStream()
  				throws IOException, SourceException
  		{
  				try
  				{
  						ObjectNode object = structure.retrieve(this.slideToken, this.requestUri);
  						NodeRevisionDescriptors revisionDescriptors =
  								content.retrieve(slideToken, this.requestUri);
  
  						if (revisionDescriptors.hasRevisions()) {
  
  								NodeRevisionDescriptor revisionDescriptor;
  								if (this.revisionNumber!=null)
  										revisionDescriptor =
  												content.retrieve(this.slideToken, revisionDescriptors, this.revisionNumber);
  								else
  										// Retrieve latest revision descriptor
  										revisionDescriptor =
  												content.retrieve(this.slideToken, revisionDescriptors);
  
  								if (revisionDescriptor != null) 
  										return content.retrieve(slideToken, revisionDescriptors,
  															 revisionDescriptor).streamContent();
  						}
  				} catch (ServiceAccessException sae) {
  					throw new SourceException("Could not get source", sae);
  				} catch (AccessDeniedException ade) {
  					throw new SourceException("Could not get source", ade);
  				} catch (LinkedObjectNotFoundException lonfe) {
  					throw new SourceException("Could not get source", lonfe);
  				} catch (ObjectNotFoundException onfe) {
  					throw new SourceException("Could not get source", onfe);
  				} catch (RevisionDescriptorNotFoundException rdnfe) {
  					throw new SourceException("Could not get source", rdnfe);
  				} catch (RevisionContentNotFoundException rcnfe) {
  					throw new SourceException("Could not get source", rcnfe);
  				} catch (RevisionNotFoundException rnfe) {
  					throw new SourceException("Could not get source", rnfe);
  				} catch (ObjectLockedException ole) {
  					throw new SourceException("Could not get source", ole);
  				}
  
  				return null;
  		}
  
  		/**
  		 * Return the unique identifer for this source
  		 */
  		public String getSystemId() {
  			return this.systemid;
  		}
  
  		/**
  		 *  Get the Validity object. This can either wrap the last modification
  		 *  date or the expires information or...
  		 *  If it is currently not possible to calculate such an information
  		 *  <code>null</code> is returned.
  		 */
  		public SourceValidity getValidity() {
  				try {
  						if (validity!=null) {
  								ObjectNode object = structure.retrieve(this.slideToken, this.requestUri);
  								NodeRevisionDescriptors revisionDescriptors =
  										content.retrieve(slideToken, this.requestUri);
  
  								if (revisionDescriptors.hasRevisions()) {
  
  										NodeRevisionDescriptor revisionDescriptor;
  										if (this.revisionNumber!=null)
  												revisionDescriptor =
  														content.retrieve(this.slideToken, revisionDescriptors, this.revisionNumber);
  										else
  												// Retrieve latest revision descriptor
  												revisionDescriptor =
  														content.retrieve(this.slideToken, revisionDescriptors);
  
  										if (revisionDescriptor != null) 
  												this.validity = new TimeStampValidity(
  														revisionDescriptor.getLastModifiedAsDate().getTime());
  								}
  						}
  				} catch (Exception e) {
  					return null;
  				}      
  
  				return this.validity;
  		}
  
  		/**
  		 * Refresh the content of this object after the underlying data
  		 * content has changed.
  		 */
  		public void discardValidity() {
  				this.validity = null;
  		}
  
  		/**
  		 * The mime-type of the content described by this object.
  		 * If the source is not able to determine the mime-type by itself
  		 * this can be null.
  		 */
  		public String getMimeType()
  		{
  				try 
  				{
  						ObjectNode object = structure.retrieve(this.slideToken, this.requestUri);
  						NodeRevisionDescriptors revisionDescriptors =
  								content.retrieve(slideToken, this.requestUri);
  
  						if (revisionDescriptors.hasRevisions()) {
  
  								NodeRevisionDescriptor revisionDescriptor;
  								if (this.revisionNumber!=null)
  										revisionDescriptor =
  												content.retrieve(this.slideToken, revisionDescriptors, this.revisionNumber);
  								else
  										// Retrieve latest revision descriptor
  										revisionDescriptor =
  												content.retrieve(this.slideToken, revisionDescriptors);
  
  								if (revisionDescriptor != null) 
  										revisionDescriptor.getContentType();
  						}
  				} catch (Exception e) {
  					return null;
  				}
  
  				return null;
  		}
  
  		/**
  		 * Does this source actually exist ?
  		 *
  		 * @return true if the resource exists.
  		 */
  		public boolean exists()
  		{
  				try {
  						structure.retrieve(this.slideToken, this.requestUri);
  				} catch (Exception e) {
  					return false;
  				}
  				return true;
  		}
  
  		/**
  		 * Get an <code>InputStream</code> where raw bytes can be written to.
  		 * The signification of these bytes is implementation-dependent and
  		 * is not restricted to a serialized XML document.
  		 *
  		 * @return a stream to write to
  		 */
  		public OutputStream getOutputStream() throws IOException, SourceException {
  				if (outputstream==null)
  						outputstream = new SlideSourceOutputStream();
  				return outputstream;
  		}
  
  		/**
  		 * Can the data sent to an <code>OutputStream</code> returned by
  		 * {@link #getOutputStream()} be cancelled ?
  		 *
  		 * @return true if the stream can be cancelled
  		 */
  		public boolean canCancel(OutputStream stream) {
  			return outputstream.canCancel();
  		}
  
  		/**
  		 * Cancel the data sent to an <code>OutputStream</code> returned by
  		 * {@link #getOutputStream()}.
  		 * <p>
  		 * After cancel, the stream should no more be used.
  		 */
  		public void cancel(OutputStream stream) throws SourceException {
  				if (outputstream==stream) {
  						try {
  								outputstream.cancel();
  						} catch (Exception e) {
  								throw new SourceException("Could not cancel output stream", e);
  						}
  				}
  		}
  
  		/**
  		 * Tests whether a resource is a collection resource.
  		 *
  		 * @param revisionDescriptor revision descriptor of the resource
  		 *
  		 * @return true if the descriptor represents a collection, false otherwise
  		 */
  		public boolean isCollection
  				(NodeRevisionDescriptor revisionDescriptor) {
  
  				boolean result = false;
  
  				if (revisionDescriptor == null)
  						return true;
  
  				NodeProperty property = revisionDescriptor.getProperty("resourcetype");
  
  				if ((property != null)
  						&& (property.getValue().equals("<collection/>"))) {
  						result = true;
  				}
  
  				return result;
  		}
  
  		/**
  		 * A file outputStream that will rename the temp file to the destination file upon close()
  		 * and discard the temp file upon cancel().
  		 */
  		public class SlideSourceOutputStream extends ByteArrayOutputStream {
  
  				private boolean isClosed = false;
  
  				public void close() throws IOException {
  						super.close();
  
  						try {
  								NodeRevisionDescriptors revisionDescriptors =
  										content.retrieve(slideToken, requestUri);
  
  								NodeRevisionDescriptor revisionDescriptor;
  								if (revisionNumber!=null)
  										revisionDescriptor =
  												content.retrieve(slideToken, revisionDescriptors, revisionNumber);
  								else
  										// Retrieve latest revision descriptor
  										revisionDescriptor =
  												content.retrieve(slideToken, revisionDescriptors);
  
  								NodeRevisionContent revisionContent =
  										new NodeRevisionContent();
  
  								byte[] bytes = toByteArray();
  								revisionContent.setContent(bytes);
  
  								revisionDescriptor.setContentLength(bytes.length);
  
  								// Last modification date
  								revisionDescriptor.setLastModified(new Date());
  
  								//token.begin();
  
  								if (revisionNumber==null)
  										content.create(slideToken, requestUri, revisionDescriptor, null);
  								content.store(slideToken, requestUri, revisionDescriptor,
  															revisionContent);
  
  								//token.commit();
  
  						} catch (Exception e) {
  								// FIXME: What should I do?
  								e.printStackTrace();
  						} finally {
  								this.isClosed = true;
  						}
  				}
  
  				public boolean canCancel() {
  						return !this.isClosed;
  				}
  
  				public void cancel() throws Exception {
  						if (this.isClosed) {
  								throw new IllegalStateException("Cannot cancel : outputstrem is already closed");
  						}
  
  						this.isClosed = true;
  						super.close();
  				}
  		}
  }
  
  
  
  
  1.1                  xml-cocoon2/src/scratchpad/src/org/apache/cocoon/components/source/SlideSourceFactory.java
  
  Index: SlideSourceFactory.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 apache@apache.org.
   *
   * 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 (INCLUDING, 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.cocoon.components.source;
  
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.configuration.Configurable;
  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.thread.ThreadSafe;
  
  import org.apache.cocoon.Constants;
  
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceFactory;
  
  import org.apache.slide.common.Domain;
  
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.util.Map;
  
  /**
   * A factory for sources from jakarta slide domains. The sources wrappers
   * to the stores of slide. It uses slide:// as pseudo-protocoll. It is also 
   * possible to get access to the store 
   * via webdav.
   *
   * Possible parameter are username and revision
   *
   * @author <a href="mailto:stephan@vern.chem.tu-berlin.de">Stephan Michels</a>
   * @version $Id: SlideSourceFactory.java,v 1.1 2002/05/22 15:28:38 nicolaken Exp $
   */
  public class SlideSourceFactory extends AbstractLoggable
    implements SourceFactory, ThreadSafe/*, Contextualizable, Configurable*/ {
  
      /** The context */
  //    protected org.apache.cocoon.environment.Context context;
      
      /** Contextualize this class */
  /*    public void contextualize(Context context)
              throws ContextException { 
          this.context = (org.apache.cocoon.environment.Context)
                          context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
      }*/
      
      /**
       * Pass the Configuration to the Configurable class. This method must 
       * always be called after the constructor and before any other method.
       *
       * @param configuration the class configurations.
       */
  /*    public void configure(Configuration configuration)
        throws ConfigurationException {
          if (configuration!=null) {
              Configuration domainconfiguration = configuration.getChild("domain-configuration");
              if (domainconfiguration!=null) {
                  try {
                      System.out.println("domain-configuration = '"+
                        this.context.getRealPath(domainconfiguration.getValue())+"'");
                      Domain.init(this.context.getRealPath(domainconfiguration.getValue()));
                  } catch (Exception e) {
                      // Could not initialize the domain
                      e.printStackTrace();
                  }
              }
          }
      }*/
  
      /**
       * Get a <code>Source</code> object.
       * @param parameters This is optional.
       */
      public Source getSource( String location, Map parameters )
          throws MalformedURLException, IOException {
          if ((this.getLogger()!=null) && (this.getLogger().isDebugEnabled())) {
              this.getLogger().debug( "Creating source object for " + location );
          }
          return new SlideSource(location, parameters);
      }
  }
  
  
  
  

----------------------------------------------------------------------
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


Mime
View raw message