You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by im...@apache.org on 2004/05/22 22:32:05 UTC

cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav WebdavFileSystemConfigBuilder.java WebdavFileObject.java WebDavFileSystem.java

imario      2004/05/22 13:32:05

  Modified:    vfs/src/java/org/apache/commons/vfs/provider
                        AbstractFileObject.java
               vfs/src/java/org/apache/commons/vfs/provider/webdav
                        WebdavFileObject.java WebDavFileSystem.java
  Added:       vfs/src/java/org/apache/commons/vfs/provider/webdav
                        WebdavFileSystemConfigBuilder.java
  Log:
  WebDav proxy handling
  
  introduced a new (internal) way how to get the children of a folder.
  Now the fileobject can return already resolved object (instead of simple strings) to e.g. prefill the file-type.
  
  In WebDav you cant directly get the type of a resource when you do not have the rights on this folder, but the parent of the resource do know them.
  Now if the parent do a "list children" those info is stored into the child by the parent.
  
  Same story for the content-size, will fix this later.
  
  Revision  Changes    Path
  1.43      +38 -10    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java
  
  Index: AbstractFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- AbstractFileObject.java	21 May 2004 20:43:18 -0000	1.42
  +++ AbstractFileObject.java	22 May 2004 20:32:04 -0000	1.43
  @@ -146,6 +146,18 @@
       protected abstract String[] doListChildren() throws Exception;
   
       /**
  +     * Lists the children of this file.  Is only called if {@link #doGetType}
  +     * returns {@link FileType#FOLDER}.  The return value of this method
  +     * is cached, so the implementation can be expensive.<br>
  +     * Other than <code>doListChildren</code> you could return FileObject's to e.g. reinitialize the type of the file.<br>
  +     * (Introduced for Webdav: "permission denied on resource" during getType())
  +     */
  +    protected FileObject[] doListChildrenResolved() throws Exception
  +    {
  +        return null;
  +    }
  +
  +    /**
        * Deletes the file.  Is only called when:
        * <ul>
        * <li>{@link #doGetType} does not return {@link FileType#IMAGINARY}.
  @@ -488,16 +500,27 @@
               return children;
           }
   
  +        // allow the filesystem to return resolved children. e.g. prefill type for webdav
  +        try
  +        {
  +            children = doListChildrenResolved();
  +        }
  +        catch (Exception exc)
  +        {
  +            throw new FileSystemException("vfs.provider/list-children.error", new Object[]{name}, exc);
  +        }
  +
  +        if (children != null)
  +        {
  +            return children;
  +        }
  +
           // List the children
           final String[] files;
           try
           {
               files = doListChildren();
           }
  -        catch (RuntimeException re)
  -        {
  -            throw re;
  -        }
           catch (Exception exc)
           {
               throw new FileSystemException("vfs.provider/list-children.error", new Object[]{name}, exc);
  @@ -1031,16 +1054,16 @@
               // Attach and determine the file type
               doAttach();
               attached = true;
  -            type = doGetType();
               if (type == null)
               {
  -                type = FileType.IMAGINARY;
  +                // type not injected
  +                type = doGetType();
  +                if (type == null)
  +                {
  +                    type = FileType.IMAGINARY;
  +                }
               }
           }
  -        catch (RuntimeException re)
  -        {
  -            throw re;
  -        }
           catch (Exception exc)
           {
               throw new FileSystemException("vfs.provider/get-type.error", new Object[]{name}, exc);
  @@ -1255,5 +1278,10 @@
       {
           GlobalConfiguration gc = getFileSystem().getFileSystemManager().getGlobalConfiguration();
           return gc.getFileContentInfoFactory();
  +    }
  +
  +    protected void injectType(FileType fileType)
  +    {
  +        type = fileType;
       }
   }
  
  
  
  1.15      +151 -1    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileObject.java
  
  Index: WebdavFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileObject.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- WebdavFileObject.java	10 May 2004 20:09:54 -0000	1.14
  +++ WebdavFileObject.java	22 May 2004 20:32:04 -0000	1.15
  @@ -19,22 +19,28 @@
   import org.apache.commons.vfs.FileObject;
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.FileType;
  +import org.apache.commons.vfs.NameScope;
   import org.apache.commons.vfs.provider.AbstractFileObject;
   import org.apache.commons.vfs.provider.GenericFileName;
   import org.apache.commons.vfs.util.MonitorOutputStream;
   import org.apache.webdav.lib.BaseProperty;
  +import org.apache.webdav.lib.ResponseEntity;
   import org.apache.webdav.lib.WebdavResource;
   import org.apache.webdav.lib.methods.DepthSupport;
   import org.apache.webdav.lib.methods.OptionsMethod;
  +import org.apache.webdav.lib.methods.PropFindMethod;
   import org.apache.webdav.lib.methods.XMLResponseMethodBase;
  +import org.apache.webdav.lib.properties.ResourceTypeProperty;
   
   import java.io.ByteArrayOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.OutputStream;
  +import java.util.Arrays;
   import java.util.Enumeration;
   import java.util.HashMap;
   import java.util.Map;
  +import java.util.Vector;
   
   /**
    * A WebDAV file.
  @@ -50,6 +56,11 @@
       private WebdavResource resource;
       private HttpURL url;
   
  +    private final static Vector PROPS_TYPE = new Vector(Arrays.asList(new String[]
  +    {
  +        WebdavResource.RESOURCETYPE
  +    }));
  +
       public WebdavFileObject(final GenericFileName name,
                               final WebDavFileSystem fileSystem)
       {
  @@ -68,6 +79,44 @@
           {
           };
           resource.setHttpURL(url, WebdavResource.NOACTION, 1);
  +
  +        /* @todo: this should not be done in doAttach - will move later */
  +        /* only needet to fill the properties in resource */
  +        final OptionsMethod optionsMethod = new OptionsMethod(getName().getPath());
  +        optionsMethod.setFollowRedirects(true);
  +        final int status = fileSystem.getClient().executeMethod(optionsMethod);
  +        if (status < 200 || status > 299)
  +        {
  +            injectType(FileType.IMAGINARY);
  +            return;
  +        }
  +        // handle the (maybe) redirected url
  +        resource.getHttpURL().setPath(optionsMethod.getPath());
  +
  +        boolean exists = false;
  +        for (Enumeration enum = optionsMethod.getAllowedMethods(); enum.hasMoreElements();)
  +        {
  +            final String method = (String) enum.nextElement();
  +            if (method.equals("GET"))
  +            {
  +                exists = true;
  +                break;
  +            }
  +        }
  +        if (!exists)
  +        {
  +            injectType(FileType.IMAGINARY);
  +            return;
  +        }
  +
  +        try
  +        {
  +            resource.setProperties(WebdavResource.DEFAULT, 1);
  +        }
  +        catch (IOException e)
  +        {
  +            throw new FileSystemException(e);
  +        }
       }
   
       /**
  @@ -78,7 +127,74 @@
        */
       protected FileType doGetType() throws Exception
       {
  +        return doGetType(null);
  +    }
  +
  +    private FileType doGetType(final String child) throws Exception
  +    {
  +        // do propfind on resource
  +        final int depth = child == null ? PropFindMethod.DEPTH_0 : PropFindMethod.DEPTH_1;
  +        final PropFindMethod propfindMethod = new PropFindMethod(getName().getPath(), depth, PROPS_TYPE.elements());
  +        // propfindMethod.setFollowRedirects(true);
  +        final int status = fileSystem.getClient().executeMethod(propfindMethod);
  +        if (status < 200 || status > 299)
  +        {
  +            if (child == null && (status == 401 || status == 403))
  +            {
  +                // This second pass should only happen if a secured resource was directly resolved.
  +                // using getChildren() on the parent already inject the type
  +                WebdavFileObject parent = (WebdavFileObject) getParent();
  +                if (parent != null)
  +                {
  +                    // premission denied
  +                    // ask the parent to find our type - this is bad
  +                    return parent.doGetType(getName().getBaseName());
  +                }
  +            }
  +
  +            return FileType.IMAGINARY;
  +        }
  +
  +        // handle the (maybe) redirected url
  +        // resource.getHttpURL().setPath(propfindMethod.getPath());
  +
  +        // find the ResourceTypeProperty
  +        String dirChild = null;
  +        if (child != null)
  +        {
  +            dirChild = child + "/";
  +        }
  +
  +        Enumeration enum = propfindMethod.getResponses();
  +        while (enum.hasMoreElements())
  +        {
  +            ResponseEntity response = (ResponseEntity) enum.nextElement();
  +            if (child == null || response.getHref().endsWith(child) || response.getHref().endsWith(dirChild))
  +            {
  +                Enumeration properties = response.getProperties();
  +                while (properties.hasMoreElements())
  +                {
  +                    Object property = properties.nextElement();
  +                    if (property instanceof ResourceTypeProperty)
  +                    {
  +                        ResourceTypeProperty resourceType = (ResourceTypeProperty) property;
  +                        if (resourceType.isCollection())
  +                        {
  +                            return FileType.FOLDER;
  +                        }
  +                        else
  +                        {
  +                            return FileType.FILE;
  +                        }
  +                    }
  +                }
  +            }
  +        }
  +
  +        return FileType.IMAGINARY;
  +
           // Determine whether the resource exists, and whether it is a DAV resource
  +        /*
           final OptionsMethod optionsMethod = new OptionsMethod(getName().getPath());
           optionsMethod.setFollowRedirects(true);
           final int status = fileSystem.getClient().executeMethod(optionsMethod);
  @@ -122,6 +238,7 @@
           {
               return FileType.FILE;
           }
  +        */
       }
   
       /**
  @@ -129,12 +246,45 @@
        */
       protected String[] doListChildren() throws Exception
       {
  +        return null;
  +        /*
           final String[] children = resource.list();
           if (children == null)
           {
               throw new FileSystemException("vfs.provider.webdav/list-children.error", resource.getStatusMessage());
           }
           return children;
  +        */
  +    }
  +
  +    /**
  +     * Lists the children of the file.
  +     */
  +    protected FileObject[] doListChildrenResolved() throws Exception
  +    {
  +        WebdavResource[] children = resource.listWebdavResources();
  +        if (children == null)
  +        {
  +            throw new FileSystemException("vfs.provider.webdav/list-children.error", resource.getStatusMessage());
  +        }
  +
  +        WebdavFileObject[] vfs = new WebdavFileObject[children.length];
  +        for (int i = 0; i < children.length; i++)
  +        {
  +            WebdavResource dav = children[i];
  +
  +            WebdavFileObject fo = (WebdavFileObject) getFileSystem().resolveFile(getName().resolveName(dav.getDisplayName(), NameScope.CHILD));
  +            fo.injectType(dav.isCollection() ? FileType.FOLDER : FileType.FILE);
  +
  +            vfs[i] = fo;
  +        }
  +
  +        return vfs;
  +    }
  +
  +    protected void injectType(FileType fileType)
  +    {
  +        super.injectType(fileType);
       }
   
       /**
  
  
  
  1.15      +22 -2     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebDavFileSystem.java
  
  Index: WebDavFileSystem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebDavFileSystem.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- WebDavFileSystem.java	19 May 2004 19:34:07 -0000	1.14
  +++ WebDavFileSystem.java	22 May 2004 20:32:04 -0000	1.15
  @@ -70,7 +70,27 @@
                       rootName.getHostName(),
                       rootName.getPort(),
                       "/");
  -                final WebdavResource resource = new WebdavResource(url, WebdavResource.NOACTION, 1);
  +
  +                WebdavResource resource = null;
  +
  +                FileSystemOptions fso = getFileSystemOptions();
  +                if (fso != null)
  +                {
  +                    String proxyHost = WebdavFileSystemConfigBuilder.getInstance().getProxyHost(fso);
  +                    int proxyPort = WebdavFileSystemConfigBuilder.getInstance().getProxyPort(fso);
  +
  +                    if (proxyHost != null && proxyPort > 0)
  +                    {
  +                        resource = new WebdavResource(url, proxyHost, proxyPort);
  +                    }
  +                }
  +
  +                if (resource == null)
  +                {
  +                    resource = new WebdavResource(url);
  +                }
  +                resource.setProperties(WebdavResource.NOACTION, 1);
  +
                   client = resource.retrieveSessionInstance();
               }
               catch (final IOException e)
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileSystemConfigBuilder.java
  
  Index: WebdavFileSystemConfigBuilder.java
  ===================================================================
  /*
   * Copyright 2002, 2003,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.commons.vfs.provider.webdav;
  
  import org.apache.commons.vfs.FileSystemConfigBuilder;
  import org.apache.commons.vfs.FileSystemOptions;
  
  /**
   * Configuration options for WebDav
   * 
   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/05/22 20:32:04 $
   */
  public class WebdavFileSystemConfigBuilder extends FileSystemConfigBuilder
  {
      private final static WebdavFileSystemConfigBuilder builder = new WebdavFileSystemConfigBuilder();
  
      public static WebdavFileSystemConfigBuilder getInstance()
      {
          return builder;
      }
  
      private WebdavFileSystemConfigBuilder()
      {
      }
  
      /**
       * Set the proxy to use for webdav connection.<br>
       * You have to set the ProxyPort too if you would like to have the proxy relly used.
       *
       * @param proxyHost the host
       * @see #setProxyPort
       */
      public void setProxyHost(FileSystemOptions opts, String proxyHost)
      {
          setParam(opts, "proxyHost", proxyHost);
      }
  
      /**
       * Set the proxy-port to use for webdav connection
       * You have to set the ProxyHost too if you would like to have the proxy relly used.
       *
       * @param proxyPort the port
       * @see #setProxyHost
       */
      public void setProxyPort(FileSystemOptions opts, int proxyPort)
      {
          setParam(opts, "proxyPort", new Integer(proxyPort));
      }
  
      /**
       * Get the proxy to use for webdav connection
       * You have to set the ProxyPort too if you would like to have the proxy relly used.
       *
       * @return proxyHost
       * @see #setProxyPort
       */
      public String getProxyHost(FileSystemOptions opts)
      {
          return (String) getParam(opts, "proxyHost");
      }
  
      /**
       * Get the proxy-port to use for webdav the connection
       * You have to set the ProxyHost too if you would like to have the proxy relly used.
       *
       * @return proxyPort: the port number or 0 if it is not set
       * @see #setProxyHost
       */
      public int getProxyPort(FileSystemOptions opts)
      {
          if (!hasParam(opts, "proxyPort"))
          {
              return 0;
          }
  
          return ((Number) getParam(opts, "proxyPort")).intValue();
      }
  
      protected Class getConfigClass()
      {
          return WebDavFileSystem.class;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org