You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by im...@apache.org on 2004/05/27 21:09:37 UTC
cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav WebdavClientFactory.java WebdavFileObject.java WebdavFileProvider.java WebDavFileSystem.java
imario 2004/05/27 12:09:37
Modified: vfs/src/java/org/apache/commons/vfs/provider/http
HttpFileProvider.java HttpFileSystem.java
vfs/src/java/org/apache/commons/vfs Resources.properties
vfs/src/java/org/apache/commons/vfs/provider/sftp
SftpFileProvider.java SftpFileSystem.java
vfs/src/java/org/apache/commons/vfs/provider/webdav
WebdavFileObject.java WebdavFileProvider.java
WebDavFileSystem.java
Added: vfs/src/java/org/apache/commons/vfs/provider/http
HttpClientFactory.java
vfs/src/java/org/apache/commons/vfs/provider/sftp
SftpClientFactory.java
vfs/src/java/org/apache/commons/vfs/provider/webdav
WebdavClientFactory.java
Log:
HTTP, FTP, SFTP, WEBDAV:
create the used filesystem client before the filesystem itself to have a fail-fast behaviour if the host and/or user/password is wrong
Revision Changes Path
1.8 +13 -3 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java
Index: HttpFileProvider.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- HttpFileProvider.java 19 May 2004 19:34:06 -0000 1.7
+++ HttpFileProvider.java 27 May 2004 19:09:37 -0000 1.8
@@ -15,6 +15,7 @@
*/
package org.apache.commons.vfs.provider.http;
+import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystem;
@@ -63,10 +64,19 @@
/**
* Creates a {@link FileSystem}.
*/
- protected FileSystem doCreateFileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions)
+ protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
throws FileSystemException
{
- return new HttpFileSystem((GenericFileName) rootName, fileSystemOptions);
+ // Create the file system
+ final GenericFileName rootName = (GenericFileName) name;
+
+ HttpClient httpClient = HttpClientFactory.createConnection(rootName.getHostName(),
+ rootName.getPort(),
+ rootName.getUserName(),
+ rootName.getPassword(),
+ fileSystemOptions);
+
+ return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
}
public FileSystemConfigBuilder getConfigBuilder()
1.10 +4 -40 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileSystem.java
Index: HttpFileSystem.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileSystem.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- HttpFileSystem.java 19 May 2004 19:34:06 -0000 1.9
+++ HttpFileSystem.java 27 May 2004 19:09:37 -0000 1.10
@@ -15,14 +15,10 @@
*/
package org.apache.commons.vfs.provider.http;
-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.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystem;
-import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.provider.AbstractFileSystem;
import org.apache.commons.vfs.provider.GenericFileName;
@@ -40,11 +36,12 @@
implements FileSystem
{
- private HttpClient client;
+ private final HttpClient client;
- public HttpFileSystem(final GenericFileName rootName, final FileSystemOptions fileSystemOptions)
+ public HttpFileSystem(final GenericFileName rootName, final HttpClient client, final FileSystemOptions fileSystemOptions)
{
super(rootName, null, fileSystemOptions);
+ this.client = client;
}
/**
@@ -55,41 +52,8 @@
caps.addAll(HttpFileProvider.capabilities);
}
- /**
- * 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());
-
- FileSystemOptions fso = getFileSystemOptions();
- if (fso != null)
- {
- String proxyHost = HttpFileSystemConfigBuilder.getInstance().getProxyHost(fso);
- int proxyPort = HttpFileSystemConfigBuilder.getInstance().getProxyPort(fso);
-
- if (proxyHost != null && proxyPort > 0)
- {
- config.setProxy(proxyHost, proxyPort);
- }
- }
-
- client.setHostConfiguration(config);
-
- if (rootName.getUserName() != null)
- {
- final UsernamePasswordCredentials creds =
- new UsernamePasswordCredentials(rootName.getUserName(), rootName.getPassword());
- client.getState().setCredentials(null, rootName.getHostName(), creds);
- }
- }
return client;
}
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpClientFactory.java
Index: HttpClientFactory.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs.provider.http;
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.httpclient.methods.HeadMethod;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemOptions;
/**
* Create a HttpClient instance
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/05/27 19:09:37 $
*/
public class HttpClientFactory
{
private HttpClientFactory()
{
}
/**
* Creates a new connection to the server.
*/
public static HttpClient createConnection(String hostname, int port, String username, String password, FileSystemOptions fileSystemOptions) throws FileSystemException
{
HttpClient client;
try
{
client = new HttpClient(new MultiThreadedHttpConnectionManager());
final HostConfiguration config = new HostConfiguration();
config.setHost(hostname, port);
if (fileSystemOptions != null)
{
String proxyHost = HttpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
int proxyPort = HttpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
if (proxyHost != null && proxyPort > 0)
{
config.setProxy(proxyHost, proxyPort);
}
}
client.setHostConfiguration(config);
if (username != null)
{
final UsernamePasswordCredentials creds =
new UsernamePasswordCredentials(username, password);
client.getState().setCredentials(null, hostname, creds);
}
client.executeMethod(new HeadMethod());
}
catch (final Exception exc)
{
throw new FileSystemException("vfs.provider.http/connect.error", new Object[]{hostname}, exc);
}
return client;
}
}
1.35 +1 -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.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- Resources.properties 24 May 2004 20:15:26 -0000 1.34
+++ Resources.properties 27 May 2004 19:09:37 -0000 1.35
@@ -172,6 +172,7 @@
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.
+vfs.provider.ftp/connect.error=Could not connect to HTTP server on "{0}".
# WebDAV Provider
vfs.provider.webdav/write-file.error=Write to file failed with message: "{0}".
1.12 +18 -122 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java
Index: SftpFileProvider.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileProvider.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- SftpFileProvider.java 24 May 2004 20:15:26 -0000 1.11
+++ SftpFileProvider.java 27 May 2004 19:09:37 -0000 1.12
@@ -15,8 +15,7 @@
*/
package org.apache.commons.vfs.provider.sftp;
-import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystem;
@@ -25,9 +24,7 @@
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
import org.apache.commons.vfs.provider.GenericFileName;
-import org.apache.commons.vfs.util.Os;
-import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -56,8 +53,6 @@
public final static String ATTR_USER_INFO = "UI";
- private static final String SSH_DIR_NAME = ".ssh";
-
// private JSch jSch = new JSch();
public SftpFileProvider()
@@ -68,131 +63,32 @@
/**
* Creates a {@link FileSystem}.
*/
- protected FileSystem doCreateFileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions) throws FileSystemException
- {
- JSch jsch = createJSch(fileSystemOptions);
-
- return new SftpFileSystem((GenericFileName) rootName, jsch, fileSystemOptions);
- }
-
- private JSch createJSch(FileSystemOptions fileSystemOptions) throws FileSystemException
+ protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) throws FileSystemException
{
- JSch jsch = new JSch();
-
- File sshDir = null;
+ // JSch jsch = createJSch(fileSystemOptions);
- // new style - user passed
- File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
- File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);
+ // Create the file system
+ final GenericFileName rootName = (GenericFileName) name;
- if (knownHostsFile != null)
+ Session session;
+ try
{
- jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
+ session = SftpClientFactory.createConnection(rootName.getHostName(),
+ rootName.getPort(),
+ rootName.getUserName(),
+ rootName.getPassword(),
+ fileSystemOptions);
}
- else
+ catch (final Exception e)
{
- if (sshDir == null)
- {
- sshDir = this.findSshDir();
- }
- // Load the known hosts file
- knownHostsFile = new File(sshDir, "known_hosts");
- if (knownHostsFile.isFile() && knownHostsFile.canRead())
- {
- jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
- }
+ throw new FileSystemException("vfs.provider.sftp/connect.error",
+ name,
+ e);
}
- if (identities != null)
- {
- for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++)
- {
- final File privateKeyFile = identities[iterIdentities];
- try
- {
- jsch.addIdentity(privateKeyFile.getAbsolutePath());
- }
- catch (final JSchException e)
- {
- throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
- }
- }
- }
- else
- {
- if (sshDir == null)
- {
- sshDir = this.findSshDir();
- }
-
- // Load the private key (rsa-key only)
- final File privateKeyFile = new File(sshDir, "id_rsa");
- if (privateKeyFile.isFile() && privateKeyFile.canRead())
- {
- try
- {
- jsch.addIdentity(privateKeyFile.getAbsolutePath());
- }
- catch (final JSchException e)
- {
- throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
- }
- }
- }
-
- return jsch;
+ return new SftpFileSystem(rootName, session, fileSystemOptions);
}
- /**
- * Finds the .ssh directory.
- * <p>The lookup order is:</p>
- * <ol>
- * <li>The system property <code>vfs.sftp.sshdir</code> (the override
- * mechanism)</li>
- * <li><code>{user.home}/.ssh</code></li>
- * <li>On Windows only: C:\cygwin\home\{user.name}\.ssh</li>
- * <li>The current directory, as a last resort.</li>
- * <ol>
- * <p/>
- * Windows Notes:
- * The default installation directory for Cygwin is <code>C:\cygwin</code>.
- * On my set up (Gary here), I have Cygwin in C:\bin\cygwin, not the default.
- * Also, my .ssh directory was created in the {user.home} directory.
- * </p>
- *
- * @return The .ssh directory
- */
- private File findSshDir()
- {
- String sshDirPath;
- sshDirPath = System.getProperty("vfs.sftp.sshdir");
- if (sshDirPath != null)
- {
- File sshDir = new File(sshDirPath);
- if (sshDir.exists())
- {
- return sshDir;
- }
- }
-
- File sshDir = new File(System.getProperty("user.home"), SSH_DIR_NAME);
- if (sshDir.exists())
- {
- return sshDir;
- }
-
- if (Os.isFamily(Os.OS_FAMILY_WINDOWS))
- {
- // TODO - this may not be true
- final String userName = System.getProperty("user.name");
- sshDir = new File("C:\\cygwin\\home\\" + userName + "\\" + SSH_DIR_NAME);
- if (sshDir.exists())
- {
- return sshDir;
- }
- }
- return new File("");
- }
/**
* Returns the JSch.
1.11 +9 -8 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java
Index: SftpFileSystem.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpFileSystem.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- SftpFileSystem.java 19 May 2004 19:34:06 -0000 1.10
+++ SftpFileSystem.java 27 May 2004 19:09:37 -0000 1.11
@@ -16,10 +16,8 @@
package org.apache.commons.vfs.provider.sftp;
import com.jcraft.jsch.ChannelSftp;
-import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
-import com.jcraft.jsch.UserInfo;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystem;
@@ -41,16 +39,16 @@
extends AbstractFileSystem
implements FileSystem
{
- private Session session;
- private final JSch jSch;
+ private final Session session;
+ // private final JSch jSch;
private ChannelSftp idleChannel;
public SftpFileSystem(final GenericFileName rootName,
- final JSch jSch,
+ final Session session,
final FileSystemOptions fileSystemOptions)
{
super(rootName, null, fileSystemOptions);
- this.jSch = jSch;
+ this.session = session;
}
/**
@@ -70,6 +68,7 @@
*/
protected ChannelSftp getChannel() throws IOException
{
+ /*
try
{
// Create the session
@@ -89,7 +88,9 @@
session.connect();
}
-
+ */
+ try
+ {
// Use the pooled channel, or create a new one
final ChannelSftp channel;
if (idleChannel != null)
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java
Index: SftpClientFactory.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs.provider.sftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.util.Os;
import java.io.File;
/**
* Create a HttpClient instance
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/05/27 19:09:37 $
*/
public class SftpClientFactory
{
private static final String SSH_DIR_NAME = ".ssh";
private SftpClientFactory()
{
}
/**
* Creates a new connection to the server.
*/
public static Session createConnection(String hostname, int port, String username, String password, FileSystemOptions fileSystemOptions) throws FileSystemException
{
JSch jsch = new JSch();
File sshDir = null;
// new style - user passed
File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);
if (knownHostsFile != null)
{
jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
}
else
{
if (sshDir == null)
{
sshDir = findSshDir();
}
// Load the known hosts file
knownHostsFile = new File(sshDir, "known_hosts");
if (knownHostsFile.isFile() && knownHostsFile.canRead())
{
jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
}
}
if (identities != null)
{
for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++)
{
final File privateKeyFile = identities[iterIdentities];
try
{
jsch.addIdentity(privateKeyFile.getAbsolutePath());
}
catch (final JSchException e)
{
throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
}
}
}
else
{
if (sshDir == null)
{
sshDir = findSshDir();
}
// Load the private key (rsa-key only)
final File privateKeyFile = new File(sshDir, "id_rsa");
if (privateKeyFile.isFile() && privateKeyFile.canRead())
{
try
{
jsch.addIdentity(privateKeyFile.getAbsolutePath());
}
catch (final JSchException e)
{
throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
}
}
}
Session session;
try
{
session = jsch.getSession(username,
hostname,
port);
session.setPassword(password);
UserInfo userInfo = SftpFileSystemConfigBuilder.getInstance().getUserInfo(fileSystemOptions);
if (userInfo != null)
{
session.setUserInfo(userInfo);
}
session.connect();
}
catch (final Exception exc)
{
throw new FileSystemException("vfs.provider.sftp/connect.error", new Object[]{hostname}, exc);
}
return session;
}
/**
* Finds the .ssh directory.
* <p>The lookup order is:</p>
* <ol>
* <li>The system property <code>vfs.sftp.sshdir</code> (the override
* mechanism)</li>
* <li><code>{user.home}/.ssh</code></li>
* <li>On Windows only: C:\cygwin\home\{user.name}\.ssh</li>
* <li>The current directory, as a last resort.</li>
* <ol>
* <p/>
* Windows Notes:
* The default installation directory for Cygwin is <code>C:\cygwin</code>.
* On my set up (Gary here), I have Cygwin in C:\bin\cygwin, not the default.
* Also, my .ssh directory was created in the {user.home} directory.
* </p>
*
* @return The .ssh directory
*/
private static File findSshDir()
{
String sshDirPath;
sshDirPath = System.getProperty("vfs.sftp.sshdir");
if (sshDirPath != null)
{
File sshDir = new File(sshDirPath);
if (sshDir.exists())
{
return sshDir;
}
}
File sshDir = new File(System.getProperty("user.home"), SSH_DIR_NAME);
if (sshDir.exists())
{
return sshDir;
}
if (Os.isFamily(Os.OS_FAMILY_WINDOWS))
{
// TODO - this may not be true
final String userName = System.getProperty("user.name");
sshDir = new File("C:\\cygwin\\home\\" + userName + "\\" + SSH_DIR_NAME);
if (sshDir.exists())
{
return sshDir;
}
}
return new File("");
}
}
1.18 +2 -122 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.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- WebdavFileObject.java 26 May 2004 08:24:51 -0000 1.17
+++ WebdavFileObject.java 27 May 2004 19:09:37 -0000 1.18
@@ -240,133 +240,13 @@
throw new IllegalStateException("this should not happen");
}
- /*
- private FileType doGetType(final String child) throws Exception
- {
- // do propfind on resource
- final int depth = child == null ? PropFindMethod.DEPTH_0 : PropFindMethod.DEPTH_1;
- final PropFindMethod propfindMethod = new PropFindMethod(getName().getPath(), depth, PROPS_TYPE.elements());
- // propfindMethod.setFollowRedirects(true);
- final int status = fileSystem.getClient().executeMethod(propfindMethod);
- if (status < 200 || status > 299)
- {
- if (child == null && (status == 401 || status == 403))
- {
- // This second pass should only happen if a secured resource was directly resolved.
- // using getChildren() on the parent already inject the type
- WebdavFileObject parent = (WebdavFileObject) getParent();
- if (parent != null)
- {
- // premission denied
- // ask the parent to find our type - this is bad
- return parent.doGetType(getName().getBaseName());
- }
- }
-
- return FileType.IMAGINARY;
- }
-
- // handle the (maybe) redirected url
- // resource.getHttpURL().setPath(propfindMethod.getPath());
-
- // find the ResourceTypeProperty
- String dirChild = null;
- if (child != null)
- {
- dirChild = child + "/";
- }
-
- Enumeration enum = propfindMethod.getResponses();
- while (enum.hasMoreElements())
- {
- ResponseEntity response = (ResponseEntity) enum.nextElement();
- if (child == null || response.getHref().endsWith(child) || response.getHref().endsWith(dirChild))
- {
- Enumeration properties = response.getProperties();
- while (properties.hasMoreElements())
- {
- Object property = properties.nextElement();
- if (property instanceof ResourceTypeProperty)
- {
- ResourceTypeProperty resourceType = (ResourceTypeProperty) property;
- if (resourceType.isCollection())
- {
- return FileType.FOLDER;
- }
- else
- {
- return FileType.FILE;
- }
- }
- }
- }
- }
-
- 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);
- if (status < 200 || status > 299)
- {
- return FileType.IMAGINARY;
- }
- resource.getHttpURL().setPath(optionsMethod.getPath());
-
- // Resource exists if we can do a GET on it
- 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)
- {
- return FileType.IMAGINARY;
- }
-
- // Check if the resource is a DAV resource
- final boolean davResource = optionsMethod.getDavCapabilities().hasMoreElements();
- if (!davResource)
- {
- // Assume a folder, and don't get the properties
- return FileType.FOLDER;
- }
-
- // 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
{
+ // use doListChildrenResolved for performance
return null;
- /*
- final String[] children = resource.list();
- if (children == null)
- {
- throw new FileSystemException("vfs.provider.webdav/list-children.error", resource.getStatusMessage());
- }
- return children;
- */
}
/**
1.8 +11 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileProvider.java
Index: WebdavFileProvider.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavFileProvider.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- WebdavFileProvider.java 19 May 2004 19:34:07 -0000 1.7
+++ WebdavFileProvider.java 27 May 2004 19:09:37 -0000 1.8
@@ -15,6 +15,7 @@
*/
package org.apache.commons.vfs.provider.webdav;
+import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystem;
@@ -70,8 +71,16 @@
protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
throws FileSystemException
{
+ // Create the file system
final GenericFileName rootName = (GenericFileName) name;
- return new WebDavFileSystem(rootName, fileSystemOptions);
+
+ HttpClient httpClient = WebdavClientFactory.createConnection(rootName.getHostName(),
+ rootName.getPort(),
+ rootName.getUserName(),
+ rootName.getPassword(),
+ fileSystemOptions);
+
+ return new WebDavFileSystem(rootName, httpClient, fileSystemOptions);
}
public Collection getCapabilities()
1.17 +5 -46 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.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- WebDavFileSystem.java 23 May 2004 18:34:33 -0000 1.16
+++ WebDavFileSystem.java 27 May 2004 19:09:37 -0000 1.17
@@ -16,7 +16,6 @@
package org.apache.commons.vfs.provider.webdav;
import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystem;
@@ -24,9 +23,7 @@
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.commons.vfs.provider.AbstractFileSystem;
import org.apache.commons.vfs.provider.GenericFileName;
-import org.apache.webdav.lib.WebdavResource;
-import java.io.IOException;
import java.util.Collection;
/**
@@ -39,11 +36,13 @@
extends AbstractFileSystem
implements FileSystem
{
- private HttpClient client;
+ private final HttpClient client;
- public WebDavFileSystem(final GenericFileName rootName, final FileSystemOptions fileSystemOptions)
+ public WebDavFileSystem(final GenericFileName rootName, final HttpClient client, final FileSystemOptions fileSystemOptions)
{
super(rootName, null, fileSystemOptions);
+
+ this.client = client;
}
/**
@@ -59,46 +58,6 @@
*/
protected HttpClient getClient() throws FileSystemException
{
- if (client == null)
- {
- // Create an Http client
- try
- {
- final GenericFileName rootName = (GenericFileName) getRootName();
- final HttpURL url = new HttpURL(rootName.getUserName(),
- rootName.getPassword(),
- rootName.getHostName(),
- rootName.getPort(),
- "/");
-
- WebdavResource resource = null;
-
- FileSystemOptions fso = getFileSystemOptions();
- if (fso != null)
- {
- String proxyHost = WebdavFileSystemConfigBuilder.getInstance().getProxyHost(fso);
- int proxyPort = WebdavFileSystemConfigBuilder.getInstance().getProxyPort(fso);
-
- if (proxyHost != null && proxyPort > 0)
- {
- resource = new WebdavResource(url, proxyHost, proxyPort);
- resource.setProxy(proxyHost, proxyPort);
- }
- }
-
- if (resource == null)
- {
- resource = new WebdavResource(url);
- }
- resource.setProperties(WebdavResource.NOACTION, 1);
-
- client = resource.retrieveSessionInstance();
- }
- catch (final IOException e)
- {
- throw new FileSystemException("vfs.provider.webdav/create-client.error", getRootName(), e);
- }
- }
return client;
}
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/webdav/WebdavClientFactory.java
Index: WebdavClientFactory.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.vfs.provider.webdav;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemOptions;
import org.apache.webdav.lib.WebdavResource;
import java.io.IOException;
/**
* Create a HttpClient instance
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/05/27 19:09:37 $
*/
public class WebdavClientFactory
{
private WebdavClientFactory()
{
}
/**
* Creates a new connection to the server.
*/
public static HttpClient createConnection(String hostname, int port, String username, String password, FileSystemOptions fileSystemOptions) throws FileSystemException
{
// Create an Http client
HttpClient client;
try
{
final HttpURL url = new HttpURL(username,
password,
hostname,
port,
"/");
WebdavResource resource = null;
if (fileSystemOptions != null)
{
String proxyHost = WebdavFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
int proxyPort = WebdavFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
if (proxyHost != null && proxyPort > 0)
{
resource = new WebdavResource(url, proxyHost, proxyPort);
resource.setProxy(proxyHost, proxyPort);
}
}
if (resource == null)
{
resource = new WebdavResource(url);
}
resource.setProperties(WebdavResource.NOACTION, 1);
client = resource.retrieveSessionInstance();
}
catch (final IOException e)
{
throw new FileSystemException("vfs.provider.webdav/connect.error", hostname, e);
}
return client;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org