adammurdoch 2003/02/14 18:35:35 Modified: vfs/src/java/org/apache/commons/vfs Resources.properties vfs/src/java/org/apache/commons/vfs/provider/webdav WebDavFileSystem.java WebdavFileObject.java Log: - Use the same HttpClient for all files in a given WebDAV file system. - Get rid of dodgy retry code when figuring out the type of a file. Revision Changes Path 1.17 +1 -0 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Resources.properties Index: Resources.properties =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Resources.properties,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- Resources.properties 15 Feb 2003 00:17:06 -0000 1.16 +++ Resources.properties 15 Feb 2003 02:35:35 -0000 1.17 @@ -137,6 +137,7 @@ vfs.provider.webdav/list-children.error=Could not list child resources. vfs.provider.webdav/create-collection.error=Creat collection failed with message: "{0}". vfs.provider.webdav/delete-file.error=Delete file failed with message: "{0}". +vfs.provider.webdav/create-client.error=Could not create client for server "{0}". # Ant tasks vfs.tasks/sync.no-destination.error=No destination file or directory specified. 1.2 +35 -5 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.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- WebDavFileSystem.java 15 Feb 2003 00:17:06 -0000 1.1 +++ WebDavFileSystem.java 15 Feb 2003 02:35:35 -0000 1.2 @@ -56,13 +56,17 @@ package org.apache.commons.vfs.provider.webdav; import java.util.Collection; +import java.io.IOException; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.vfs.Capability; import org.apache.commons.vfs.FileName; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; -import org.apache.commons.vfs.Capability; import org.apache.commons.vfs.provider.AbstractFileSystem; import org.apache.commons.vfs.provider.GenericFileName; +import org.apache.util.HttpURL; +import org.apache.webdav.lib.WebdavResource; /** * A WebDAV file system. @@ -74,9 +78,27 @@ extends AbstractFileSystem implements FileSystem { - public WebDavFileSystem( final GenericFileName rootName ) + private final HttpClient client; + + public WebDavFileSystem( final GenericFileName rootName ) throws FileSystemException { super( rootName, null ); + + // Create an Http client + try + { + final HttpURL url = new HttpURL( rootName.getUserName(), + rootName.getPassword(), + rootName.getHostName(), + rootName.getPort(), + "/" ); + final WebdavResource resource = new WebdavResource( url, WebdavResource.NOACTION, 1 ); + client = resource.retrieveSessionInstance(); + } + catch ( final IOException e ) + { + throw new FileSystemException( "vfs.provider.webdav/create-client.error", rootName, e ); + } } /** @@ -94,13 +116,21 @@ } /** + * Returns the client for this file system. + */ + protected HttpClient getClient() + { + return client; + } + + /** * Creates a file object. This method is called only if the requested * file is not cached. */ protected FileObject createFile( final FileName name ) throws FileSystemException { - final GenericFileName rootName = (GenericFileName)name; - return new WebdavFileObject( rootName, this ); + final GenericFileName fileName = (GenericFileName)name; + return new WebdavFileObject( fileName, this ); } } 1.2 +31 -50 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.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- WebdavFileObject.java 15 Feb 2003 00:17:06 -0000 1.1 +++ WebdavFileObject.java 15 Feb 2003 02:35:35 -0000 1.2 @@ -64,12 +64,11 @@ import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.FileType; import org.apache.commons.vfs.provider.AbstractFileObject; -import org.apache.commons.vfs.provider.AbstractFileSystem; import org.apache.commons.vfs.provider.GenericFileName; -import org.apache.commons.vfs.util.MonitorInputStream; import org.apache.commons.vfs.util.MonitorOutputStream; import org.apache.util.HttpURL; import org.apache.webdav.lib.WebdavResource; +import org.apache.webdav.lib.methods.OptionsMethod; /** * A WebDAV file. @@ -81,42 +80,52 @@ extends AbstractFileObject implements FileObject { + private final WebDavFileSystem fileSystem; private WebdavResource resource; private HttpURL url; public WebdavFileObject( final GenericFileName name, - final AbstractFileSystem fileSystem ) throws FileSystemException + final WebDavFileSystem fileSystem ) throws FileSystemException { super( name, fileSystem ); + this.fileSystem = fileSystem; + } + + /** + * Attaches this file object to its file resource. This method is called + * before any of the doBlah() or onBlah() methods. Sub-classes can use + * this method to perform lazy initialisation. + * + * This implementation does nothing. + */ + protected void doAttach() throws Exception + { + final GenericFileName name = (GenericFileName)getName(); + url = new HttpURL( name.getUserName(), name.getPassword(), name.getHostName(), name.getPort(), name.getPath() ); + resource = new WebdavResource( fileSystem.getClient() ) { }; + resource.setHttpURL( url, WebdavResource.NOACTION, 1 ); } /** * Determines the type of the file, returns null if the file does not * exist. * - * @todo Pool and reuse the connections * @todo Bail if file is not a DAV resource */ protected FileType doGetType() throws Exception { - final GenericFileName name = (GenericFileName)getName(); - url = new HttpURL( name.getUserName(), name.getPassword(), name.getHostName(), name.getPort(), name.getPath() ); - resource = new WebdavResource( url, WebdavResource.NOACTION, 1 ); - // Determine whether the resource exists, and whether it is a DAV resource - boolean ok = resource.optionsMethod(); - if ( !ok ) + final OptionsMethod optionsMethod = new OptionsMethod( getName().getPath() ); + optionsMethod.setFollowRedirects( true ); + final int status = fileSystem.getClient().executeMethod( optionsMethod ); + if ( status < 200 || status > 299 ) { - resource.getHttpURL().setPath( getName().getPath() + '/' ); - ok = resource.optionsMethod(); - if ( !ok ) - { - return null; - } + return null; } + resource.getHttpURL().setPath( optionsMethod.getPath() ); boolean exists = false; - for ( Enumeration enum = resource.getAllowedMethods(); enum.hasMoreElements(); ) + for ( Enumeration enum = optionsMethod.getAllowedMethods(); enum.hasMoreElements(); ) { final String method = (String)enum.nextElement(); if ( method.equals( "GET" ) ) @@ -125,13 +134,6 @@ break; } } - boolean isDavResource = false; - for ( Enumeration enum = resource.getDavCapabilities(); enum.hasMoreElements(); ) - { - isDavResource = true; - break; - } - if ( !exists ) { return null; @@ -167,6 +169,7 @@ */ protected void doCreateFolder() throws Exception { + // Adjust resource path resource.getHttpURL().setPath( getName().getPath() + '/' ); final boolean ok = resource.mkcolMethod(); if ( !ok ) @@ -192,7 +195,7 @@ */ protected InputStream doGetInputStream() throws Exception { - return new WebdavInputStream( resource ); + return resource.getMethodData(); } /** @@ -211,33 +214,10 @@ return resource.getGetContentLength(); } - /** An InputStream that reads from a Webdav resource. */ - private static class WebdavInputStream - extends MonitorInputStream - { - private final WebdavResource resource; - - public WebdavInputStream( final WebdavResource resource ) - throws Exception - { - super( resource.getMethodData() ); - this.resource = resource; - } - - /** - * Called after the stream has been closed. - */ - protected void onClose() throws IOException - { - // TODO - clean up - //resource.close(); - } - } - /** * An OutputStream that writes to a Webdav resource. * - * @todo Don't gather up the body in a ByteArrayOutputStream, write directly to connection + * @todo Don't gather up the body in a ByteArrayOutputStream; need to write directly to connection */ private class WebdavOutputStream extends MonitorOutputStream @@ -254,6 +234,7 @@ { final ByteArrayOutputStream outstr = (ByteArrayOutputStream)out; + // Adjust the resource path (this file object may have been a folder) resource.getHttpURL().setPath( getName().getPath() ); final boolean ok = resource.putMethod( outstr.toByteArray() ); if ( !ok )
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]