imario 2004/05/23 11:34:33 Modified: vfs/src/java/org/apache/commons/vfs/provider AbstractFileObject.java vfs/src/java/org/apache/commons/vfs/provider/webdav WebdavFileObject.java WebDavFileSystem.java Log: changed the way how the webdav resources are fetched, and a better handling of how the childlist is processed.
This allows e.g. to determine the type and lastmoddate of restricted resources. Browsing through a webdav tree should be significant faster than before as many, many round-trips have been saved. Revision Changes Path 1.44 +5 -5 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java Index: AbstractFileObject.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java,v retrieving revision 1.43 retrieving revision 1.44 diff -u -r1.43 -r1.44 --- AbstractFileObject.java 22 May 2004 20:32:04 -0000 1.43 +++ AbstractFileObject.java 23 May 2004 18:34:33 -0000 1.44 @@ -1054,14 +1054,14 @@ // Attach and determine the file type doAttach(); attached = true; + // now the type could already be injected by doAttach (e.g from parent to child) if (type == null) { - // type not injected type = doGetType(); - if (type == null) - { - type = FileType.IMAGINARY; - } + } + if (type == null) + { + type = FileType.IMAGINARY; } } catch (Exception exc) 1.16 +137 -49 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.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- WebdavFileObject.java 22 May 2004 20:32:04 -0000 1.15 +++ WebdavFileObject.java 23 May 2004 18:34:33 -0000 1.16 @@ -15,6 +15,8 @@ */ package org.apache.commons.vfs.provider.webdav; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpURL; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystemException; @@ -24,11 +26,9 @@ import org.apache.commons.vfs.provider.GenericFileName; import org.apache.commons.vfs.util.MonitorOutputStream; import org.apache.webdav.lib.BaseProperty; -import org.apache.webdav.lib.ResponseEntity; 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; @@ -36,11 +36,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; -import java.util.Vector; /** * A WebDAV file. @@ -54,12 +52,8 @@ { private final WebDavFileSystem fileSystem; private WebdavResource resource; - private HttpURL url; - - private final static Vector PROPS_TYPE = new Vector(Arrays.asList(new String[] - { - WebdavResource.RESOURCETYPE - })); + private boolean redirectionResolved = false; + // private HttpURL url; public WebdavFileObject(final GenericFileName name, final WebDavFileSystem fileSystem) @@ -73,47 +67,128 @@ */ 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()) + if (resource == null) { - }; - resource.setHttpURL(url, WebdavResource.NOACTION, 1); + setDavResource(null, true); + } + } - /* @todo: this should not be done in doAttach - will move later */ - /* only needet to fill the properties in resource */ - final OptionsMethod optionsMethod = new OptionsMethod(getName().getPath()); - optionsMethod.setFollowRedirects(true); - final int status = fileSystem.getClient().executeMethod(optionsMethod); - if (status < 200 || status > 299) + protected void doDetach() throws Exception + { + if (resource != null) { - injectType(FileType.IMAGINARY); - return; + resource.close(); + resource = null; } - // handle the (maybe) redirected url - resource.getHttpURL().setPath(optionsMethod.getPath()); + } - boolean exists = false; - for (Enumeration enum = optionsMethod.getAllowedMethods(); enum.hasMoreElements();) + private void setDavResource(WebdavResource resource, boolean bCheckExists) throws Exception + { + redirectionResolved = false; + + // System.err.println("set on " + System.identityHashCode(this) + " " + getName() + ":" + bCheckExists); + if (resource == null) { - final String method = (String) enum.nextElement(); - if (method.equals("GET")) + final GenericFileName name = (GenericFileName) getName(); + HttpURL url = new HttpURL(name.getUserName(), name.getPassword(), name.getHostName(), name.getPort(), name.getPath()); + resource = new WebdavResource(fileSystem.getClient()) { - exists = true; - break; + }; + resource.setHttpURL(url, WebdavResource.NOACTION, 1); + } + + this.resource = resource; + + if (bCheckExists) + { + /* now fill the dav properties */ + final OptionsMethod optionsMethod = new OptionsMethod(getName().getPath()); + optionsMethod.setFollowRedirects(true); + final int status = fileSystem.getClient().executeMethod(optionsMethod); + if (status < 200 || status > 299) + { + if (status == 401 || status == 403) + { + // System.err.println("process on " + System.identityHashCode(this)); + // permission denied on this object, but we might get some informations from the parent + processParentDavResource(); + // System.err.println("leave on " + System.identityHashCode(this)); + return; + } + else + { + injectType(FileType.IMAGINARY); + } + return; + } + // handle the (maybe) redirected url + redirectionResolved = true; + resource.getHttpURL().setPath(optionsMethod.getPath()); + + boolean exists = false; + for (Enumeration enum = optionsMethod.getAllowedMethods(); enum.hasMoreElements();) + { + final String method = (String) enum.nextElement(); + if (method.equals("GET")) + { + exists = true; + break; + } + } + if (!exists) + { + injectType(FileType.IMAGINARY); + return; + } + + try + { + resource.setProperties(WebdavResource.DEFAULT, 1); + } + catch (IOException e) + { + throw new FileSystemException(e); } } - if (!exists) + + ResourceTypeProperty resourceType = resource.getResourceType(); + if (resourceType.isCollection()) + { + injectType(FileType.FOLDER); + } + else + { + injectType(FileType.FILE); + } + } + + private void resolveRedirection() throws IOException, FileSystemException + { + if (redirectionResolved) { - injectType(FileType.IMAGINARY); return; } + 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(optionsMethod.getPath()); + redirectionResolved = true; + } + } + + private void processParentDavResource() throws FileSystemException + { + WebdavFileObject parent = (WebdavFileObject) getParent(); try { - resource.setProperties(WebdavResource.DEFAULT, 1); + // System.err.println("tell him:" + parent.toString()); + // after this our resource should be reset + parent.doListChildrenResolved(); } - catch (IOException e) + catch (Exception e) { throw new FileSystemException(e); } @@ -122,14 +197,14 @@ /** * Determines the type of the file, returns null if the file does not * exist. - * - * @todo Shouldn't need 2 trips to the server to determine type. */ protected FileType doGetType() throws Exception { - return doGetType(null); + // return doGetType(null); + throw new IllegalStateException("this should not happen"); } + /* private FileType doGetType(final String child) throws Exception { // do propfind on resource @@ -194,7 +269,7 @@ return FileType.IMAGINARY; // Determine whether the resource exists, and whether it is a DAV resource - /* + [* final OptionsMethod optionsMethod = new OptionsMethod(getName().getPath()); optionsMethod.setFollowRedirects(true); final int status = fileSystem.getClient().executeMethod(optionsMethod); @@ -238,8 +313,9 @@ { return FileType.FILE; } - */ + *] } + */ /** * Lists the children of the file. @@ -262,7 +338,24 @@ */ protected FileObject[] doListChildrenResolved() throws Exception { - WebdavResource[] children = resource.listWebdavResources(); + WebdavResource[] children = new org.apache.webdav.lib.WebdavResource[0]; + try + { + children = resource.listWebdavResources(); + } + catch (HttpException e) + { + if (e.getReasonCode() == HttpStatus.SC_MOVED_PERMANENTLY || e.getReasonCode() == HttpStatus.SC_MOVED_TEMPORARILY) + { + resolveRedirection(); + children = resource.listWebdavResources(); + } + else + { + throw e; + } + } + if (children == null) { throw new FileSystemException("vfs.provider.webdav/list-children.error", resource.getStatusMessage()); @@ -274,7 +367,7 @@ WebdavResource dav = children[i]; WebdavFileObject fo = (WebdavFileObject) getFileSystem().resolveFile(getName().resolveName(dav.getDisplayName(), NameScope.CHILD)); - fo.injectType(dav.isCollection() ? FileType.FOLDER : FileType.FILE); + fo.setDavResource(dav, false); vfs[i] = fo; } @@ -282,11 +375,6 @@ return vfs; } - protected void injectType(FileType fileType) - { - super.injectType(fileType); - } - /** * Creates this file as a folder. */ @@ -306,7 +394,7 @@ */ protected void doDelete() throws Exception { - final boolean ok = resource.deleteMethod(url.getPath()); + final boolean ok = resource.deleteMethod(getName().getPath() /*url.getPath()*/); if (!ok) { throw new FileSystemException("vfs.provider.webdav/delete-file.error", resource.getStatusMessage()); 1.16 +2 -1 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.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- WebDavFileSystem.java 22 May 2004 20:32:04 -0000 1.15 +++ WebDavFileSystem.java 23 May 2004 18:34:33 -0000 1.16 @@ -82,6 +82,7 @@ if (proxyHost != null && proxyPort > 0) { resource = new WebdavResource(url, proxyHost, proxyPort); + resource.setProxy(proxyHost, proxyPort); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]