You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ad...@apache.org on 2003/06/28 12:50:39 UTC
cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http HttpFileObject.java HttpFileProvider.java HttpFileSystem.java
adammurdoch 2003/06/28 03:50:38
Modified: vfs/src/java/org/apache/commons/vfs Resources.properties
vfs/src/java/org/apache/commons/vfs/impl providers.xml
Added: vfs/src/java/org/apache/commons/vfs/provider/http
HttpFileObject.java HttpFileProvider.java
HttpFileSystem.java
Log:
Added an HTTP provider that uses comons httpclient.
Revision Changes Path
1.25 +6 -1 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.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- Resources.properties 24 Jun 2003 10:39:04 -0000 1.24
+++ Resources.properties 28 Jun 2003 10:50:38 -0000 1.25
@@ -140,7 +140,12 @@
# URL Provider
vfs.provider.url/badly-formed-uri.error=Badly formed URI "{0}".
-# WebDAV provider
+# Http Provider
+vfs.provider.http/get.error=GET method failed for "{0}".
+vfs.provider.http/head.error=HEAD method failed for "{0}".
+vfs.provider.http/last-modified.error=No Last-Modified header in HTTP response.
+
+# WebDAV Provider
vfs.provider.webdav/write-file.error=Write to file failed with message: "{0}".
vfs.provider.webdav/list-children.error=List child resources failed with message: "{0}".
vfs.provider.webdav/create-collection.error=Create collection failed with message: "{0}".
1.4 +5 -0 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/providers.xml
Index: providers.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/providers.xml,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- providers.xml 24 Feb 2003 07:31:08 -0000 1.3
+++ providers.xml 28 Jun 2003 10:50:38 -0000 1.4
@@ -20,9 +20,14 @@
<scheme name="smb"/>
<if-available class-name="jcifs.smb.SmbFile"/>
</provider>
+ <provider class-name="org.apache.commons.vfs.provider.http.HttpFileProvider">
+ <scheme name="http"/>
+ <if-available class-name="org.apache.commons.httpclient.HttpClient"/>
+ </provider>
<provider class-name="org.apache.commons.vfs.provider.webdav.WebdavFileProvider">
<scheme name="webdav"/>
<if-available class-name="org.apache.webdav.lib.WebdavResource"/>
+ <if-available class-name="org.apache.commons.httpclient.HttpClient"/>
</provider>
<provider class-name="org.apache.commons.vfs.provider.sftp.SftpFileProvider">
<scheme name="sftp"/>
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileObject.java
Index: HttpFileObject.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 apache@apache.org.
*
* 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.http;
import java.io.InputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.util.DateParser;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.HeadMethod;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.util.MonitorInputStream;
import org.apache.commons.vfs.provider.AbstractFileObject;
/**
* A file object backed by commons httpclient.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2003/06/28 10:50:38 $
*
* @todo status codes
*/
public class HttpFileObject
extends AbstractFileObject
{
private final HttpFileSystem fileSystem;
private HeadMethod method;
public HttpFileObject( final FileName name,
final HttpFileSystem fileSystem )
{
super( name, fileSystem );
this.fileSystem = fileSystem;
}
/**
* Detaches this file object from its file resource.
*/
protected void doDetach()
throws Exception
{
method = null;
}
/**
* Determines the type of this file. Must not return null. The return
* value of this method is cached, so the implementation can be expensive.
*/
protected FileType doGetType()
throws Exception
{
// Use the HEAD method to probe the file.
method = new HeadMethod();
setupMethod( method );
final HttpClient client = fileSystem.getClient();
final int status = client.executeMethod( method );
method.releaseConnection();
if ( status == HttpURLConnection.HTTP_OK )
{
return FileType.FILE;
}
else if ( status == HttpURLConnection.HTTP_NOT_FOUND
|| status == HttpURLConnection.HTTP_GONE )
{
return FileType.IMAGINARY;
}
else
{
throw new FileSystemException( "vfs.provider.http/head.error", getName() );
}
}
/**
* Lists the children of this file.
*/
protected String[] doListChildren()
throws Exception
{
throw new Exception( "Not implemented." );
}
/**
* Returns the size of the file content (in bytes).
*/
protected long doGetContentSize()
throws Exception
{
final Header header = method.getResponseHeader( "content-length" );
if ( header == null )
{
// Assume 0 content-length
return 0;
}
return Integer.parseInt( header.getValue() );
}
/**
* Returns the last modified time of this file.
*
* This implementation throws an exception.
*/
protected long doGetLastModifiedTime()
throws Exception
{
final Header header = method.getResponseHeader( "last-modified" );
if ( header == null )
{
throw new FileSystemException( "vfs.provider.http/last-modified.error", getName() );
}
return DateParser.parseDate( header.getValue() ).getTime();
}
/**
* Creates an input stream to read the file content from. Is only called
* if {@link #doGetType} returns {@link FileType#FILE}.
*
* <p>It is guaranteed that there are no open output streams for this file
* when this method is called.
*
* <p>The returned stream does not have to be buffered.
*/
protected InputStream doGetInputStream()
throws Exception
{
final GetMethod getMethod = new GetMethod();
setupMethod( getMethod );
final int status = fileSystem.getClient().executeMethod( getMethod );
if ( status != HttpURLConnection.HTTP_OK )
{
throw new FileSystemException( "vfs.provider.http/get.error", getName() );
}
return new HttpInputStream( getMethod );
}
/**
* Prepares a Method object.
*/
private void setupMethod( final HttpMethod method )
{
method.setPath( getName().getPath() );
method.setFollowRedirects( true );
method.setRequestHeader( "User-Agent", "Jakarta-Commons-VFS" );
}
/** An InputStream that cleans up the HTTP connection on close. */
private static class HttpInputStream
extends MonitorInputStream
{
private final GetMethod method;
public HttpInputStream( final GetMethod method )
throws IOException
{
super( method.getResponseBodyAsStream() );
this.method = method;
}
/**
* Called after the stream has been closed.
*/
protected void onClose()
throws IOException
{
method.releaseConnection();
}
}
}
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java
Index: HttpFileProvider.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 apache@apache.org.
*
* 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.http;
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;
/**
* An HTTP provider that uses commons-httpclient.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2003/06/28 10:50:38 $
*/
public class HttpFileProvider
extends AbstractOriginatingFileProvider
{
/**
* Parses an abolute URI.
*/
protected FileName parseUri( final String uri )
throws FileSystemException
{
return GenericFileName.parseUri( uri, 80 );
}
/**
* Creates a {@link FileSystem}.
*/
protected FileSystem doCreateFileSystem( final FileName rootName )
throws FileSystemException
{
return new HttpFileSystem( (GenericFileName)rootName );
}
}
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileSystem.java
Index: HttpFileSystem.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 apache@apache.org.
*
* 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.http;
import java.util.Collection;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileSystem;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.provider.AbstractFileSystem;
import org.apache.commons.vfs.provider.GenericFileName;
/**
* An HTTP file system.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2003/06/28 10:50:38 $
*/
public class HttpFileSystem
extends AbstractFileSystem
implements FileSystem
{
private HttpClient client;
public HttpFileSystem( final GenericFileName rootName )
{
super( rootName, null );
}
/**
* Adds the capabilities of this file system.
*/
protected void addCapabilities( final Collection caps )
{
caps.add( Capability.READ_CONTENT );
caps.add( Capability.URI );
caps.add( Capability.GET_LAST_MODIFIED );
caps.add( Capability.ATTRIBUTES );
}
/**
* Returns the client for this file system.
*/
protected HttpClient getClient()
throws FileSystemException
{
if ( client == null )
{
// Create an Http client
final GenericFileName rootName = (GenericFileName)getRootName();
client = new HttpClient( new MultiThreadedHttpConnectionManager() );
final HostConfiguration config = new HostConfiguration();
config.setHost( rootName.getHostName(), rootName.getPort() );
client.setHostConfiguration( config );
final UsernamePasswordCredentials creds =
new UsernamePasswordCredentials( rootName.getUserName(), rootName.getPassword() );
client.getState().setCredentials( null, rootName.getHostName(), creds );
}
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 Exception
{
return new HttpFileObject( name, this );
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org