1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http;
18
19 import org.apache.commons.httpclient.Cookie;
20 import org.apache.commons.httpclient.HostConfiguration;
21 import org.apache.commons.httpclient.HttpClient;
22 import org.apache.commons.httpclient.HttpConnectionManager;
23 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
24 import org.apache.commons.httpclient.UsernamePasswordCredentials;
25 import org.apache.commons.httpclient.auth.AuthScope;
26 import org.apache.commons.httpclient.params.HttpClientParams;
27 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.commons.lang3.time.DurationUtils;
30 import org.apache.commons.vfs2.FileSystemException;
31 import org.apache.commons.vfs2.FileSystemOptions;
32 import org.apache.commons.vfs2.UserAuthenticationData;
33 import org.apache.commons.vfs2.UserAuthenticator;
34 import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
35
36
37
38
39
40
41 @Deprecated
42 public final class HttpClientFactory {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme,
59 final String hostname, final int port, final String username, final String password,
60 final FileSystemOptions fileSystemOptions) throws FileSystemException {
61 final HttpClient client;
62 try {
63 final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager();
64 final HttpConnectionManagerParams connectionMgrParams = mgr.getParams();
65
66 client = new HttpClient(mgr);
67
68 final HostConfiguration config = new HostConfiguration();
69 config.setHost(hostname, port, scheme);
70
71 if (fileSystemOptions != null) {
72 final String proxyHost = builder.getProxyHost(fileSystemOptions);
73 final int proxyPort = builder.getProxyPort(fileSystemOptions);
74
75 if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) {
76 config.setProxy(proxyHost, proxyPort);
77 }
78
79 final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions);
80 if (proxyAuth != null) {
81 final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth,
82 new UserAuthenticationData.Type[] {UserAuthenticationData.USERNAME,
83 UserAuthenticationData.PASSWORD});
84
85 if (authData != null) {
86 final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(
87 UserAuthenticatorUtils.toString(
88 UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)),
89 UserAuthenticatorUtils.toString(
90 UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));
91
92 final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT);
93 client.getState().setProxyCredentials(scope, proxyCreds);
94 }
95
96 if (builder.isPreemptiveAuth(fileSystemOptions)) {
97 final HttpClientParams httpClientParams = new HttpClientParams();
98 httpClientParams.setAuthenticationPreemptive(true);
99 client.setParams(httpClientParams);
100 }
101 }
102
103 final Cookie[] cookies = builder.getCookies(fileSystemOptions);
104 if (cookies != null) {
105 client.getState().addCookies(cookies);
106 }
107 }
108
109
110
111
112
113 connectionMgrParams.setMaxConnectionsPerHost(config, builder.getMaxConnectionsPerHost(fileSystemOptions));
114 connectionMgrParams.setMaxTotalConnections(builder.getMaxTotalConnections(fileSystemOptions));
115
116 connectionMgrParams.setConnectionTimeout(
117 DurationUtils.toMillisInt(builder.getConnectionTimeoutDuration(fileSystemOptions)));
118 connectionMgrParams
119 .setSoTimeout(DurationUtils.toMillisInt(builder.getSoTimeoutDuration(fileSystemOptions)));
120
121 client.setHostConfiguration(config);
122
123 if (username != null) {
124 final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
125 final AuthScope scope = new AuthScope(hostname, AuthScope.ANY_PORT);
126 client.getState().setCredentials(scope, creds);
127 }
128 } catch (final Exception exc) {
129 throw new FileSystemException("vfs.provider.http/connect.error", exc, hostname);
130 }
131
132 return client;
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public static HttpClient createConnection(final String scheme, final String hostname, final int port, final String username, final String password,
148 final FileSystemOptions fileSystemOptions) throws FileSystemException {
149 return createConnection(HttpFileSystemConfigBuilder.getInstance(), scheme, hostname, port, username, password, fileSystemOptions);
150 }
151
152 private HttpClientFactory() {
153 }
154
155 }