1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.provider.http;
18
19 import org.apache.commons.httpclient.HostConfiguration;
20 import org.apache.commons.httpclient.HttpClient;
21 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
22 import org.apache.commons.httpclient.UsernamePasswordCredentials;
23 import org.apache.commons.httpclient.Cookie;
24 import org.apache.commons.httpclient.methods.HeadMethod;
25 import org.apache.commons.vfs.FileSystemException;
26 import org.apache.commons.vfs.FileSystemOptions;
27 import org.apache.commons.vfs.UserAuthenticator;
28 import org.apache.commons.vfs.UserAuthenticationData;
29 import org.apache.commons.vfs.util.UserAuthenticatorUtils;
30
31 /***
32 * Create a HttpClient instance
33 *
34 * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
35 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
36 */
37 public class HttpClientFactory
38 {
39 private HttpClientFactory()
40 {
41 }
42
43 /***
44 * Creates a new connection to the server.
45 */
46 public static HttpClient createConnection(String scheme, String hostname, int port, String username, String password, FileSystemOptions fileSystemOptions) throws FileSystemException
47 {
48 HttpClient client;
49 try
50 {
51 client = new HttpClient(new MultiThreadedHttpConnectionManager());
52 final HostConfiguration config = new HostConfiguration();
53 config.setHost(hostname, port, scheme);
54
55 if (fileSystemOptions != null)
56 {
57 String proxyHost = HttpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
58 int proxyPort = HttpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
59
60 if (proxyHost != null && proxyPort > 0)
61 {
62 config.setProxy(proxyHost, proxyPort);
63 }
64
65 UserAuthenticator proxyAuth = HttpFileSystemConfigBuilder.getInstance().getProxyAuthenticator(fileSystemOptions);
66 if (proxyAuth != null)
67 {
68 UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[]
69 {
70 UserAuthenticationData.USERNAME,
71 UserAuthenticationData.PASSWORD
72 });
73
74 if (authData != null)
75 {
76 final UsernamePasswordCredentials proxyCreds =
77 new UsernamePasswordCredentials(
78 UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)),
79 UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));
80
81 client.getState().setProxyCredentials(null, proxyHost, proxyCreds);
82 }
83 }
84
85 Cookie[] cookies = HttpFileSystemConfigBuilder.getInstance().getCookies(fileSystemOptions);
86 if (cookies != null)
87 {
88 client.getState().addCookies(cookies);
89 }
90 }
91
92 client.setHostConfiguration(config);
93
94 if (username != null)
95 {
96 final UsernamePasswordCredentials creds =
97 new UsernamePasswordCredentials(username, password);
98 client.getState().setCredentials(null, hostname, creds);
99 }
100
101 client.executeMethod(new HeadMethod());
102 }
103 catch (final Exception exc)
104 {
105 throw new FileSystemException("vfs.provider.http/connect.error", new Object[]{hostname}, exc);
106 }
107
108 return client;
109 }
110 }