KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > vfs > IvyWebdavClientFactory


1 package fr.jayasoft.ivy.repository.vfs;
2
3 import java.io.IOException JavaDoc;
4
5 import org.apache.commons.httpclient.HttpClient;
6 import org.apache.commons.httpclient.HttpURL;
7 import org.apache.commons.vfs.FileSystemException;
8 import org.apache.commons.vfs.FileSystemOptions;
9 import org.apache.commons.vfs.provider.webdav.WebdavConnectionManager;
10 import org.apache.commons.vfs.provider.webdav.WebdavFileSystemConfigBuilder;
11 import org.apache.webdav.lib.WebdavResource;
12
13 import fr.jayasoft.ivy.url.HttpClientHandler;
14
15 /***************************************************************************************************
16  * Modified version of the WebdavClientFactory from VFS which adds support for httpclient 3.x.
17  * See http://issues.apache.org/jira/browse/VFS-74 for more info.
18  *
19  * Create a HttpClient instance
20  *
21  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
22  * @author Maarten Coene
23  * @version $Revision: 330479 $ $Date: 2005-11-03 07:19:24 +0100 (Do, 03 Nov 2005) $
24  */

25 class IvyWebdavClientFactory {
26
27     private IvyWebdavClientFactory() {
28     }
29
30     /***********************************************************************************************
31      * Creates a new connection to the server.
32      */

33     public static HttpClient createConnection(String JavaDoc hostname, int port, String JavaDoc username,
34             String JavaDoc password, FileSystemOptions fileSystemOptions) throws FileSystemException {
35         // Create an Http client
36
HttpClient client;
37         try {
38             final HttpURL url = new HttpURL(username, password, hostname, port, "/");
39
40             // WebdavResource resource = null;
41
WebdavResource resource = new WebdavResource() {
42             };
43
44             if (fileSystemOptions != null) {
45                 String JavaDoc proxyHost = WebdavFileSystemConfigBuilder.getInstance().getProxyHost(
46                         fileSystemOptions);
47                 int proxyPort = WebdavFileSystemConfigBuilder.getInstance().getProxyPort(
48                         fileSystemOptions);
49
50                 if (proxyHost != null && proxyPort > 0) {
51                     // resource = new WebdavResource(url, proxyHost, proxyPort);
52
resource.setProxy(proxyHost, proxyPort);
53                 }
54             }
55
56             /*
57              * if (resource == null) { resource = new WebdavResource(url); }
58              * resource.setProperties(WebdavResource.NOACTION, 1);
59              */

60             resource.setHttpURL(url, WebdavResource.NOACTION, 1);
61
62             client = resource.retrieveSessionInstance();
63             HttpClientHandler handler = new HttpClientHandler();
64             int httpClientVersion = handler.getHttpClientMajorVersion();
65             if (httpClientVersion == 2) {
66                 // VFS only supports httpclient v2 for now...
67
client.setHttpConnectionManager(new WebdavConnectionManager());
68             } else {
69                 client.setHttpConnectionManager(new IvyWebdavConnectionManager());
70             }
71         } catch (final IOException JavaDoc e) {
72             throw new FileSystemException("vfs.provider.webdav/connect.error", hostname, e);
73         }
74
75         return client;
76     }
77 }
78
Popular Tags