View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Create a HttpClient instance.
38   *
39   * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}.
40   */
41  @Deprecated
42  public final class HttpClientFactory {
43  
44      /**
45       * Creates a new connection to the server.
46       *
47       * @param builder The HttpFileSystemConfigBuilder.
48       * @param scheme The protocol.
49       * @param hostname The hostname.
50       * @param port The port number.
51       * @param username The username.
52       * @param password The password
53       * @param fileSystemOptions The file system options.
54       * @return a new HttpClient connection.
55       * @throws FileSystemException if an error occurs.
56       * @since 2.0
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              * ConnectionManager set methods must be called after the host & port and proxy host & port are set in the
110              * HostConfiguration. They are all used as part of the key when HttpConnectionManagerParams tries to locate
111              * the host configuration.
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      * Creates an HttpClient.
137      *
138      * @param scheme The host scheme.
139      * @param hostname The host name or IP address.
140      * @param port The host port.
141      * @param username The user name.
142      * @param password The user password.
143      * @param fileSystemOptions The file system options.
144      * @return a new HttpClient connection.
145      * @throws FileSystemException if an error occurs.
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 }