001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.provider.http; 018 019import org.apache.commons.httpclient.Cookie; 020import org.apache.commons.httpclient.HostConfiguration; 021import org.apache.commons.httpclient.HttpClient; 022import org.apache.commons.httpclient.HttpConnectionManager; 023import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 024import org.apache.commons.httpclient.UsernamePasswordCredentials; 025import org.apache.commons.httpclient.auth.AuthScope; 026import org.apache.commons.httpclient.params.HttpClientParams; 027import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 028import org.apache.commons.lang3.StringUtils; 029import org.apache.commons.lang3.time.DurationUtils; 030import org.apache.commons.vfs2.FileSystemException; 031import org.apache.commons.vfs2.FileSystemOptions; 032import org.apache.commons.vfs2.UserAuthenticationData; 033import org.apache.commons.vfs2.UserAuthenticator; 034import org.apache.commons.vfs2.util.UserAuthenticatorUtils; 035 036/** 037 * Create a HttpClient instance. 038 * 039 * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}. 040 */ 041@Deprecated 042public final class HttpClientFactory { 043 044 /** 045 * Creates a new connection to the server. 046 * 047 * @param builder The HttpFileSystemConfigBuilder. 048 * @param scheme The protocol. 049 * @param hostname The hostname. 050 * @param port The port number. 051 * @param username The username. 052 * @param password The password 053 * @param fileSystemOptions The file system options. 054 * @return a new HttpClient connection. 055 * @throws FileSystemException if an error occurs. 056 * @since 2.0 057 */ 058 public static HttpClient createConnection(final HttpFileSystemConfigBuilder builder, final String scheme, 059 final String hostname, final int port, final String username, final String password, 060 final FileSystemOptions fileSystemOptions) throws FileSystemException { 061 final HttpClient client; 062 try { 063 final HttpConnectionManager mgr = new MultiThreadedHttpConnectionManager(); 064 final HttpConnectionManagerParams connectionMgrParams = mgr.getParams(); 065 066 client = new HttpClient(mgr); 067 068 final HostConfiguration config = new HostConfiguration(); 069 config.setHost(hostname, port, scheme); 070 071 if (fileSystemOptions != null) { 072 final String proxyHost = builder.getProxyHost(fileSystemOptions); 073 final int proxyPort = builder.getProxyPort(fileSystemOptions); 074 075 if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) { 076 config.setProxy(proxyHost, proxyPort); 077 } 078 079 final UserAuthenticator proxyAuth = builder.getProxyAuthenticator(fileSystemOptions); 080 if (proxyAuth != null) { 081 final UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth, 082 new UserAuthenticationData.Type[] {UserAuthenticationData.USERNAME, 083 UserAuthenticationData.PASSWORD}); 084 085 if (authData != null) { 086 final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials( 087 UserAuthenticatorUtils.toString( 088 UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)), 089 UserAuthenticatorUtils.toString( 090 UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null))); 091 092 final AuthScope scope = new AuthScope(proxyHost, AuthScope.ANY_PORT); 093 client.getState().setProxyCredentials(scope, proxyCreds); 094 } 095 096 if (builder.isPreemptiveAuth(fileSystemOptions)) { 097 final HttpClientParams httpClientParams = new HttpClientParams(); 098 httpClientParams.setAuthenticationPreemptive(true); 099 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}