Index: /Commons-VFS/sandbox/src/main/java/org/apache/commons/vfs/provider/webdav/WebdavFileObject.java =================================================================== --- /Commons-VFS/sandbox/src/main/java/org/apache/commons/vfs/provider/webdav/WebdavFileObject.java (revision 645753) +++ /Commons-VFS/sandbox/src/main/java/org/apache/commons/vfs/provider/webdav/WebdavFileObject.java (working copy) @@ -16,6 +16,20 @@ */ package org.apache.commons.vfs.provider.webdav; +import java.io.DataInputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.HttpStatus; @@ -26,10 +40,9 @@ import org.apache.commons.vfs.NameScope; import org.apache.commons.vfs.RandomAccessContent; import org.apache.commons.vfs.provider.AbstractFileObject; -import org.apache.commons.vfs.provider.AbstractRandomAccessContent; +import org.apache.commons.vfs.provider.AbstractRandomAccessStreamContent; import org.apache.commons.vfs.provider.GenericFileName; import org.apache.commons.vfs.provider.URLFileName; -import org.apache.commons.vfs.provider.AbstractRandomAccessStreamContent; import org.apache.commons.vfs.util.FileObjectUtils; import org.apache.commons.vfs.util.MonitorOutputStream; import org.apache.commons.vfs.util.RandomAccessMode; @@ -37,23 +50,10 @@ 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.DataInputStream; -import java.io.FilterInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; - /** * A WebDAV file. * @@ -114,6 +114,7 @@ */ private void setDavResource(WebdavResource resource) throws Exception { + redirectionResolved = false; final URLFileName name = (URLFileName) getName(); @@ -136,7 +137,14 @@ { /* now fill the dav properties */ String pathEncoded = name.getPathQueryEncoded(urlCharset); - final OptionsMethod optionsMethod = new OptionsMethod(pathEncoded); + + // We only execute the options method in order to + // setAllowedMethods. Haven't looked to see how important doing that is. + + // Existence is determined separately (see existsTest). + + + final OptionsMethod optionsMethod = new OptionsMethod(pathEncoded); configureMethod(optionsMethod); try { @@ -160,44 +168,44 @@ } // handle the (maybe) redirected url redirectionResolved = true; - resource.getHttpURL().setEscapedPath(optionsMethod.getURI().getPath()); + System.out.println(optionsMethod.getURI().getPath()); +// resource.getHttpURL().setEscapedPath(optionsMethod.getURI().getPath()); + resource.getHttpURL().setEscapedPath(optionsMethod.getURI().getEscapedPath()); setAllowedMethods(optionsMethod.getAllowedMethods()); - boolean exists = false; - for (Enumeration enumeration = optionsMethod.getAllowedMethods(); enumeration.hasMoreElements();) - { - final String method = (String) enumeration.nextElement(); - // IIS allows GET even if the file is non existend - so changed to COPY - // if (method.equals("GET")) - if (method.equals("COPY")) - { - exists = true; - break; - } - } + + boolean exists = existsTest(pathEncoded); if (!exists) { injectType(FileType.IMAGINARY); return; } - + try { resource.setProperties(WebdavResource.DEFAULT, 1); } catch (IOException e) { + e.printStackTrace(); + throw new FileSystemException(e); } } finally { - optionsMethod.releaseConnection(); + optionsMethod.releaseConnection(); } } ResourceTypeProperty resourceType = resource.getResourceType(); - if (resourceType.isCollection()) + if (resourceType == null) { + //This should not happen because resource has been checked for its existence + //and DAV:resourcetype property MUST be defined on all DAV compliant resources. + //However there may be a non-compliant server. + injectType(FileType.FILE_OR_FOLDER); + + } else if (resourceType.isCollection()) { injectType(FileType.FOLDER); } @@ -206,6 +214,28 @@ injectType(FileType.FILE); } } + + private boolean existsTest(String pathEncoded) throws IOException { + + final PropFindMethod propFindMethod = new PropFindMethod(pathEncoded); + configureMethod(propFindMethod); + try + { + propFindMethod.setFollowRedirects(true); + final int status = fileSystem.getClient().executeMethod(propFindMethod); + if (status < 200 || status > 299) + { + //injectType(FileType.IMAGINARY); + return false; + } else { + return true; + } + } + finally + { + propFindMethod.releaseConnection(); + } + } protected void configureMethod(HttpMethodBase httpMethod) {