adammurdoch 2003/02/14 16:17:06 Modified: vfs/src/java/org/apache/commons/vfs Resources.properties vfs/src/java/org/apache/commons/vfs/impl StandardFileSystemManager.java Added: vfs/src/java/org/apache/commons/vfs/provider/webdav Test.java WebDavFileSystem.java WebdavFileObject.java WebdavProvider.java Log: First cut at a webdav provider. Revision Changes Path 1.16 +6 -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.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- Resources.properties 13 Feb 2003 04:28:45 -0000 1.15 +++ Resources.properties 15 Feb 2003 00:17:06 -0000 1.16 @@ -132,6 +132,12 @@ # URL Provider vfs.provider.url/badly-formed-uri.error=Badly formed URI "{0}". +# WebDAV provider +vfs.provider.webdav/write-file.error=Write to file failed with message: "{0}". +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}". + # Ant tasks vfs.tasks/sync.no-destination.error=No destination file or directory specified. vfs.tasks/sync.too-many-destinations.error=Cannot specify both a destination file and a destination directory. 1.9 +3 -3 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java Index: StandardFileSystemManager.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/StandardFileSystemManager.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- StandardFileSystemManager.java 12 Feb 2003 07:56:10 -0000 1.8 +++ StandardFileSystemManager.java 15 Feb 2003 00:17:06 -0000 1.9 @@ -92,6 +92,7 @@ addProvider( "jar", "org.apache.commons.vfs.provider.jar.JarFileSystemProvider" ); addProvider( "ftp", "org.apache.commons.vfs.provider.ftp.FtpFileSystemProvider" ); addProvider( "smb", "org.apache.commons.vfs.provider.smb.SmbFileSystemProvider" ); + addProvider( "webdav", "org.apache.commons.vfs.provider.webdav.WebdavProvider" ); addProvider( "tmp", "org.apache.commons.vfs.provider.temp.TemporaryFileProvider" ); // Add a default provider @@ -141,5 +142,4 @@ throw new FileSystemException( "vfs.impl/create-provider.error", providerClassName, e ); } } - -} +} \ No newline at end of file 1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/Test.java Index: Test.java =================================================================== /* ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2003 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 acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", 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 names without prior written * permission of the Apache Group. * * 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.commons.vfs.provider.webdav; import org.apache.commons.vfs.FileSystemManager; import org.apache.commons.vfs.VFS; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.Selectors; import java.io.OutputStream; /** * * * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision: 1.1 $ $Date: 2003/02/15 00:17:06 $ */ public class Test { public static void main( final String[] args ) throws Exception { final FileSystemManager manager = VFS.getManager(); final FileObject folder = manager.resolveFile( args[ 0 ] ); final FileObject file = folder.resolveFile( "file2.txt" ); System.out.println( "folder.exists() = " + folder.exists() ); System.out.println( "file.exists() = " + file.exists() ); folder.delete( Selectors.SELECT_ALL ); System.out.println( "folder.exists() = " + folder.exists() ); System.out.println( "file.exists() = " + file.exists() ); final OutputStream outstr = file.getContent().getOutputStream(); outstr.write( ( "hello\n" ).getBytes() ); outstr.close(); System.out.println( "folder.exists() = " + folder.exists() ); System.out.println( "file.exists() = " + file.exists() ); } } 1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebDavFileSystem.java Index: WebDavFileSystem.java =================================================================== /* ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2003 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 acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", 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 names without prior written * permission of the Apache Group. * * 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.commons.vfs.provider.webdav; import java.util.Collection; 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; /** * A WebDAV file system. * * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision: 1.1 $ $Date: 2003/02/15 00:17:06 $ */ class WebDavFileSystem extends AbstractFileSystem implements FileSystem { public WebDavFileSystem( final GenericFileName rootName ) { super( rootName, null ); } /** * Adds the capabilities of this file system. */ protected void addCapabilities( final Collection caps ) { caps.add( Capability.CREATE ); caps.add( Capability.DELETE ); caps.add( Capability.GET_TYPE ); caps.add( Capability.LIST_CHILDREN ); caps.add( Capability.READ_CONTENT ); caps.add( Capability.URI ); caps.add( Capability.WRITE_CONTENT ); } /** * 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 ); } } 1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileObject.java Index: WebdavFileObject.java =================================================================== /* ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2003 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 acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", 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 names without prior written * permission of the Apache Group. * * 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.commons.vfs.provider.webdav; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import org.apache.commons.vfs.FileObject; 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; /** * A WebDAV file. * * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision: 1.1 $ $Date: 2003/02/15 00:17:06 $ */ public class WebdavFileObject extends AbstractFileObject implements FileObject { private WebdavResource resource; private HttpURL url; public WebdavFileObject( final GenericFileName name, final AbstractFileSystem fileSystem ) throws FileSystemException { super( name, fileSystem ); } /** * 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 ) { resource.getHttpURL().setPath( getName().getPath() + '/' ); ok = resource.optionsMethod(); if ( !ok ) { return null; } } boolean exists = false; for ( Enumeration enum = resource.getAllowedMethods(); enum.hasMoreElements(); ) { final String method = (String)enum.nextElement(); if ( method.equals( "GET" ) ) { exists = true; break; } } boolean isDavResource = false; for ( Enumeration enum = resource.getDavCapabilities(); enum.hasMoreElements(); ) { isDavResource = true; break; } if ( !exists ) { return null; } // Get the properties of the resource resource.setProperties( WebdavResource.DEFAULT, 1 ); if ( resource.isCollection() ) { return FileType.FOLDER; } else { return FileType.FILE; } } /** * Lists the children of the file. */ protected String[] doListChildren() throws Exception { final String[] children = resource.list(); if ( children == null ) { throw new FileSystemException( "vfs.provider.webdav/list-children.error" ); } return children; } /** * Creates this file as a folder. */ protected void doCreateFolder() throws Exception { resource.getHttpURL().setPath( getName().getPath() + '/' ); final boolean ok = resource.mkcolMethod(); if ( !ok ) { throw new FileSystemException( "vfs.provider.webdav/create-collection.error", resource.getStatusMessage() ); } } /** * Deletes the file. */ protected void doDelete() throws Exception { final boolean ok = resource.deleteMethod( url.getPath() ); if ( !ok ) { throw new FileSystemException( "vfs.provider.webdav/delete-file.error", resource.getStatusMessage() ); } } /** * Creates an input stream to read the file content from. */ protected InputStream doGetInputStream() throws Exception { return new WebdavInputStream( resource ); } /** * Creates an output stream to write the file content to. */ protected OutputStream doGetOutputStream() throws Exception { return new WebdavOutputStream(); } /** * Returns the size of the file content (in bytes). */ protected long doGetContentSize() throws Exception { 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 */ private class WebdavOutputStream extends MonitorOutputStream { public WebdavOutputStream() { super( new ByteArrayOutputStream() ); } /** * Called after this stream is closed. */ protected void onClose() throws IOException { final ByteArrayOutputStream outstr = (ByteArrayOutputStream)out; resource.getHttpURL().setPath( getName().getPath() ); final boolean ok = resource.putMethod( outstr.toByteArray() ); if ( !ok ) { throw new FileSystemException( "vfs.provider.webdav/write-file.error", resource.getStatusMessage() ); } } } } 1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavProvider.java Index: WebdavProvider.java =================================================================== /* ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2003 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 acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", 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 names without prior written * permission of the Apache Group. * * 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.commons.vfs.provider.webdav; import org.apache.commons.vfs.FileName; import org.apache.commons.vfs.FileSystem; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider; import org.apache.commons.vfs.provider.GenericFileName; /** * A provider for WebDAV. * * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> * @version $Revision: 1.1 $ $Date: 2003/02/15 00:17:06 $ */ public class WebdavProvider extends AbstractOriginatingFileProvider { /** * Parses an abolute URI. */ protected FileName parseUri( final String uri ) throws FileSystemException { return GenericFileName.parseUri( uri, 80 ); } /** * Creates a filesystem. */ protected FileSystem doCreateFileSystem( final FileName name ) throws FileSystemException { final GenericFileName rootName = (GenericFileName)name; return new WebDavFileSystem( rootName ); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]