stephan     2002/08/19 01:05:27

  Modified:    src/scratchpad/src/org/apache/cocoon/components/source/impl
                        SlideSource.java
  Log:
  Retrieve the correct mime type of the context.
  Add transaction support for the upload.
  Add new method to create collections.
  
  Revision  Changes    Path
  1.17      +194 -25   
xml-cocoon2/src/scratchpad/src/org/apache/cocoon/components/source/impl/SlideSource.java
  
  Index: SlideSource.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/components/source/impl/SlideSource.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- SlideSource.java  15 Aug 2002 13:40:10 -0000      1.16
  +++ SlideSource.java  19 Aug 2002 08:05:27 -0000      1.17
  @@ -71,8 +71,9 @@
   import org.apache.cocoon.Constants;
   import org.apache.cocoon.components.source.InspectableSource;
   import org.apache.cocoon.components.source.LockableSource;
  +import org.apache.cocoon.components.source.ModifiableTraversableSource;
   import org.apache.cocoon.components.source.RestrictableSource;
  -import org.apache.cocoon.components.source.TraversableSource;
  +//import org.apache.cocoon.components.source.TraversableSource;
   import org.apache.cocoon.components.source.WriteableSource;
   import org.apache.cocoon.components.source.VersionableSource;
   import org.apache.cocoon.components.source.helpers.GroupSourcePermission;
  @@ -137,11 +138,14 @@
    * @version $Id$
    */
   public class SlideSource extends AbstractLogEnabled implements 
  -      Composable, Source, WriteableSource, TraversableSource, RestrictableSource, 
  -      LockableSource, InspectableSource, VersionableSource {
  +      Composable, Contextualizable, Source, WriteableSource, 
ModifiableTraversableSource, 
  +      RestrictableSource, LockableSource, InspectableSource, VersionableSource {
   
       /** Component manager */
       private ComponentManager manager;
  +  
  +    /** Component context */
  +    private Context context;
   
       /** Sytem id */
       private String systemid;
  @@ -244,6 +248,18 @@
       }
   
       /**
  +     * Pass the Context to the component.
  +     * This method is called after the Loggable.setLogger() (if present)
  +     * method and before any other method.
  +     *
  +     * @param context the context
  +     * @throws ContextException if context is invalid
  +     */
  +    public void contextualize(Context context) throws ContextException {
  +        this.context = context;
  +    }
  +
  +    /**
        * Initialialize the component. Initialization includes
        * allocating any resources required throughout the
        * components lifecycle.
  @@ -438,17 +454,21 @@
                   // Last modification date
                   revisionDescriptor.setLastModified(new Date());
   
  -                //nat.begin();
  +                nat.begin();
   
                   if (revisionNumber==null)
                       content.create(slideToken, config.getFilesPath()+uri, 
revisionDescriptor, null);
                   content.store(slideToken, config.getFilesPath()+uri, 
revisionDescriptor,
                                 revisionContent);
   
  -                //nat.commit();
  +                try { 
  +                    nat.commit();
  +                } catch (Exception cme) {
  +                    this.logger.warn("Could not commit the transaction.", cme);
  +                }
   
               } catch (ObjectNotFoundException e) {
  -                
  +
                   // Todo : Check to see if parent exists
                   SubjectNode subject = new SubjectNode();
   
  @@ -456,23 +476,18 @@
                       // Creating an object 
                       structure.create(slideToken, subject, 
config.getFilesPath()+uri);
                   } catch (SlideException se) {
  -                    // FIXME correct exception handling
  -                    e.printStackTrace();
  +                    this.logger.debug("Could not create source.", se);
                       throw new IOException(se.getMessage());
                   }
   
                   NodeRevisionDescriptor revisionDescriptor =
                       new NodeRevisionDescriptor(bytes.length);
   
  -                //NodeProperty property = null;
  -
  -                // Creation date
  -
                   // Resource type
  -               // revisionDescriptor.setResourceType(""); //FIXME works only with 
CVS code of Slide
  +                revisionDescriptor.setResourceType(""); 
   
                   // Source
  -                //revisionDescriptor.setSource("");
  +                revisionDescriptor.setSource("");
   
                   // Get content language
                   revisionDescriptor.setContentLanguage("en");
  @@ -481,15 +496,24 @@
                   revisionDescriptor.setContentLength(bytes.length);
   
                   // Get content type
  -                // FIXME retrieving the correct mime type
  -                revisionDescriptor.setContentType("application/octet-stream");
  +                String contentType = null;
  +                try {
  +                    contentType = ((org.apache.cocoon.environment.Context)
  +                        
context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT)).getMimeType(uri);
  +                } catch (ContextException ce) {
  +                    this.logger.warn("Could not get context to determine the mime 
type.");
  +                }
  +                if (contentType == null) {
  +                    contentType = "application/octet-stream";
  +                }
  +                revisionDescriptor.setContentType(contentType);
   
                   // Last modification date
                   revisionDescriptor.setLastModified(new Date());
   
                   // Owner
  -                //revisionDescriptor.setOwner(
  -                //    
slideToken.getCredentialthis.slideToken().getPublicCredentials());
  +                revisionDescriptor.setOwner(
  +                    slideToken.getCredentialsToken().getPublicCredentials());
   
                   // Creating revisionDescriptor associated with the object
                   NodeRevisionContent revisionContent =
  @@ -499,7 +523,20 @@
                   try {
                       content.create(slideToken, config.getFilesPath()+uri, 
revisionDescriptor,
                                      revisionContent);
  +
  +                    try {
  +                        nat.commit();
  +                    } catch (Exception cme) {
  +                        this.logger.warn("Could not commit the transaction.", cme);
  +                    }
                   } catch (SlideException se) {
  +
  +                    try {
  +                        nat.rollback();
  +                    } catch (Exception rbe) {
  +                        this.logger.warn("Could not rollback the transaction.", 
rbe);
  +                    }
  +
                       this.logger.error("Could not create source", se);
                       throw new IOException(se.getMessage());
                   }
  @@ -508,6 +545,13 @@
                   this.logger.error("Could not create source", e);
                   throw new IOException(e.getMessage());
               } finally {
  +   
  +                try {
  +                    nat.rollback();
  +                } catch (Exception rbe) {
  +                    this.logger.warn("Could not rollback the transaction.", rbe);
  +                }
  +
                   this.isClosed = true;
               }
           }
  @@ -527,6 +571,85 @@
       }
   
       /**
  +     * Move the source to source.getSystemId(), including all properties
  +     * deletes this source.
  +     *
  +     * @param source Destination of the source.
  +     */
  +    public void move(WriteableSource source) throws SourceException {
  +        if (source instanceof SlideSource) {
  +            try {
  +                this.macro.move(slideToken, this.config.getFilesPath()+this.uri, 
  +                                
this.config.getFilesPath()+((SlideSource)source).uri);
  +            } catch (SlideException se) { 
  +                throw new SourceException("Could not move source.", se);
  +            }
  +        } else {
  +            try {
  +                OutputStream out = source.getOutputStream();
  +                InputStream in = getInputStream();
  +
  +                byte[] buffer = new byte[8192];
  +                int length = -1;
  +
  +                while ((length = in.read(buffer)) > -1) {
  +                    out.write(buffer, 0, length);
  +                }
  +                in.close();
  +                out.flush();
  +                out.close();
  +            } catch (IOException ioe) {
  +                throw new SourceException("Could not copy source : 
"+ioe.getMessage());
  +            }
  +            this.delete();
  +        }
  +    }
  +
  +    /**
  +     * Copy the source to source.getSystemId(), including all properties. 
  +     *
  +     * @param source Destination of the source.
  +     */
  +    public void copy(WriteableSource source) throws SourceException {
  +        if (source instanceof SlideSource) {
  +            try {
  +                this.macro.copy(slideToken, this.config.getFilesPath()+this.uri,
  +                                
this.config.getFilesPath()+((SlideSource)source).uri);
  +            } catch (SlideException se) {
  +                throw new SourceException("Could not move source.", se);
  +            }
  +        } else {
  +            try {
  +                OutputStream out = source.getOutputStream();
  +                InputStream in = getInputStream();
  +
  +                byte[] buffer = new byte[8192];
  +                int length = -1;
  +
  +                while ((length = in.read(buffer)) > -1) {
  +                    out.write(buffer, 0, length);
  +                }
  +                in.close();
  +                out.flush();
  +                out.close();
  +            } catch (IOException ioe) {
  +                throw new SourceException("Could not copy source : 
"+ioe.getMessage());
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Delete the source.
  +     */
  +    public void delete() throws SourceException {
  +        try {
  +            this.macro.delete(slideToken, this.config.getFilesPath()+this.uri);
  +        } catch (SlideException se) {
  +            throw new SourceException("Could not delete source.", se);
  +        }
  +    }
  +
  +    /**
        * Return the content length of the content or -1 if the length is
        * unknown
        */
  @@ -656,11 +779,52 @@
               return null;
   
           if (this.uri.endsWith("/"))
  -           return protocol+":/"+this.uri.substring(0, 
this.uri.substring(0,this.uri.length()-1).lastIndexOf("/"));
  +            return protocol+":/"+this.uri.substring(0, 
  +                this.uri.substring(0,this.uri.length()-1).lastIndexOf("/"));
   
           return protocol+":/"+this.uri.substring(0, this.uri.lastIndexOf("/"));
       }
   
  +    /**
  +     * Create a collection of sources.
  +     *
  +     * @param collectionname Name of the collectiom, which 
  +     *                       should be created.
  +     */
  +    public void createCollection(String collectionname) throws SourceException {
  +
  +        SubjectNode collection = new SubjectNode();
  +        NodeRevisionDescriptor revisionDescriptor = new NodeRevisionDescriptor(0);
  +
  +        // Resource type
  +        revisionDescriptor.setResourceType("<collection/>");
  +
  +        // Creation date
  +        revisionDescriptor.setCreationDate(new Date());
  +
  +        // Last modification date
  +        revisionDescriptor.setLastModified(new Date());
  +
  +        // Content length name
  +        revisionDescriptor.setContentLength(0);
  +
  +        // Source
  +        revisionDescriptor.setSource("");
  +
  +        // Owner
  +        revisionDescriptor.setOwner(
  +            slideToken.getCredentialsToken().getPublicCredentials());
  +
  +        try {
  +            structure.create(slideToken, collection, 
this.config.getFilesPath()+this.uri+"/"+collectionname);
  +            content.create(slideToken, 
this.config.getFilesPath()+this.uri+"/"+collectionname, 
  +                           revisionDescriptor, null);
  +        } catch (SlideException se) {
  +            throw new SourceException("Could not create collection.", se);
  +        }
  +
  +    } 
  +
       /** 
        * Get the current credential for the source
        */
  @@ -960,19 +1124,24 @@
                           SourcePermission sourcepermission = null;
   
                           if (principal.equals("~"))
  -                            sourcepermission = new 
PrincipalSourcePermission(PrincipalSourcePermission.PRINCIPAL_SELF, null,
  +                            sourcepermission = 
  +                                new 
PrincipalSourcePermission(PrincipalSourcePermission.PRINCIPAL_SELF, null,
                                                                           
inheritedPermissions, negative);
                           else if (principal.equals("nobody")) 
  -                            sourcepermission = new 
PrincipalSourcePermission(PrincipalSourcePermission.PRINCIPAL_GUEST, null,
  +                            sourcepermission = 
  +                                new 
PrincipalSourcePermission(PrincipalSourcePermission.PRINCIPAL_GUEST, null,
                                                                           
inheritedPermissions, negative);
                           else if (principal.equals(userspath))
  -                            sourcepermission = new 
PrincipalSourcePermission(PrincipalSourcePermission.PRINCIPAL_ALL, null,
  +                            sourcepermission = 
  +                                new 
PrincipalSourcePermission(PrincipalSourcePermission.PRINCIPAL_ALL, null,
                                                                           
inheritedPermissions, negative);
                           else if (principal.startsWith(userspath+"/"))
  -                            sourcepermission = new 
PrincipalSourcePermission(principal.substring(userspath.length()+1), null,
  +                            sourcepermission = 
  +                                new 
PrincipalSourcePermission(principal.substring(userspath.length()+1), null,
                                                                           
inheritedPermissions, negative);
                           else if (principal.startsWith("+"+userspath+"/"))
  -                            sourcepermission = new 
GroupSourcePermission(principal.substring(userspath.length()+2), null,
  +                            sourcepermission = 
  +                                new 
GroupSourcePermission(principal.substring(userspath.length()+2), null,
                                                                            
inheritedPermissions, negative);
                           else
                               sourcepermission = new 
PrincipalSourcePermission(principal, 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