adammurdoch 2003/06/28 04:16:33
Modified: vfs/src/java/org/apache/commons/vfs/provider/url
UrlFileObject.java
Log:
- Fixed detection of whether an HTTP resource exists.
- Fixed HTTP connection leak.
Patch submitted by Anthony Eden.
Revision Changes Path
1.9 +44 -7
jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/url/UrlFileObject.java
Index: UrlFileObject.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/url/UrlFileObject.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- UrlFileObject.java 28 Jun 2003 10:54:28 -0000 1.8
+++ UrlFileObject.java 28 Jun 2003 11:16:33 -0000 1.9
@@ -57,7 +57,9 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
+import java.net.HttpURLConnection;
import java.net.URL;
+import java.net.URLConnection;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
@@ -103,17 +105,34 @@
*/
protected FileType doGetType() throws Exception
{
- // Attempt to connect
try
{
- url.openConnection().connect();
+ // Attempt to connect & check status
+ final URLConnection conn = url.openConnection();
+ final InputStream in = conn.getInputStream();
+ try
+ {
+ if (conn instanceof HttpURLConnection)
+ {
+ final int status = ((HttpURLConnection)conn).getResponseCode();
+ // 200 is good, maybe add more later...
+ if ( HttpURLConnection.HTTP_OK != status)
+ {
+ return FileType.IMAGINARY;
+ }
+ }
+
+ return FileType.FILE;
+ }
+ finally
+ {
+ in.close();
+ }
}
catch ( final FileNotFoundException e )
{
return FileType.IMAGINARY;
}
-
- return FileType.FILE;
}
/**
@@ -121,7 +140,16 @@
*/
protected long doGetContentSize() throws Exception
{
- return url.openConnection().getContentLength();
+ final URLConnection conn = url.openConnection();
+ final InputStream in = conn.getInputStream();
+ try
+ {
+ return conn.getContentLength();
+ }
+ finally
+ {
+ in.close();
+ }
}
/**
@@ -130,7 +158,16 @@
protected long doGetLastModifiedTime()
throws Exception
{
- return url.openConnection().getLastModified();
+ final URLConnection conn = url.openConnection();
+ final InputStream in = conn.getInputStream();
+ try
+ {
+ return conn.getLastModified();
+ }
+ finally
+ {
+ in.close();
+ }
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]