com.microsoft.tfs.core.config.httpclient
Class DefaultHTTPClientFactory

java.lang.Object
  extended by com.microsoft.tfs.core.config.httpclient.DefaultHTTPClientFactory
All Implemented Interfaces:
ConfigurableHTTPClientFactory, HTTPClientFactory

public class DefaultHTTPClientFactory
extends java.lang.Object
implements ConfigurableHTTPClientFactory

A default implementation of the HTTPClientFactory interface that uses a ConnectionInstanceData to configure an HttpClient. This implementation is intended to be subclassed and provides a number of hooks that subclasses may override.

See Also:
HTTPClientFactory, HttpClient
Since:
TEE-SDK-10.1
Thread-safety:
thread-safe

Field Summary
static int CONNECT_TIMEOUT_SECONDS_DEFAULT
           
static java.lang.String CONNECT_TIMEOUT_SECONDS_PROPERTY
           
static java.lang.String DISABLE_HTTP_CANCEL_THREAD_PROPERTY
           
static java.lang.String ECLIPSE_GROUP_NAME
           
static int MAX_CONNECTIONS_PER_HOST_DEFAULT
           
static java.lang.String MAX_CONNECTIONS_PER_HOST_PROPERTY
           
static int MAX_TOTAL_CONNECTIONS_DEFAULT
           
static java.lang.String MAX_TOTAL_CONNECTIONS_PROPERTY
           
static int SOCKET_TIMEOUT_SECONDS_DEFAULT
           
static java.lang.String SOCKET_TIMEOUT_SECONDS_PROPERTY
           
static int USER_AGENT_EXTRA_TEXT_MAX_CHARS
          The maximum length in characters of the extra user agent text part.
static int USER_AGENT_OS_INFO_MAX_CHARS
          The maximum length in characters of the operating system info part.
 
Constructor Summary
DefaultHTTPClientFactory(ConnectionInstanceData connectionInstanceData)
          Creates a new DefaultHTTPClientFactory that will data contained in the specified instance data to configure HttpClients.
 
Method Summary
protected  void addClientToCloserThread(HttpClient httpClient)
          Called to add the specified HttpClient instance to a static thread that will handle idle connection closing.
 void configureClient(HttpClient httpClient, ConnectionInstanceData connectionInstanceData)
          Called from HTTPClientFactory.newHTTPClient() to perform final configuration of a new HttpClient instance before it is returned to the caller of that method.
 void configureClientCredentials(HttpClient httpClient, HttpState state, ConnectionInstanceData connectionInstanceData)
          Called from HTTPClientFactory.newHTTPClient() to configure credentials for a new HttpClient instance.
 void configureClientParams(HttpClient httpClient, HttpClientParams params, ConnectionInstanceData connectionInstanceData)
          Called from HTTPClientFactory.newHTTPClient() to configure a new HttpClient's HttpClientParams.
 void configureClientProxy(HttpClient httpClient, HostConfiguration hostConfiguration, HttpState httpState, ConnectionInstanceData connectionInstanceData)
          Called from HTTPClientFactory.newHTTPClient() to configure proxy settings for a new HttpClient instance.
 HttpConnectionManager createConnectionManager(ConnectionInstanceData connectionInstanceData)
          Called from HTTPClientFactory.newHTTPClient() to create and configure a new HttpConnectionManager as part of creating a new HttpClient instance.
protected  Credentials createCredentials(ConnectionInstanceData connectionInstanceData)
          Called from configureClientCredentials(HttpClient, HttpState, ConnectionInstanceData) to create a new Credentials instance for the specified ConnectionInstanceData.
 HttpClient createHTTPClient(HttpConnectionManager connectionManager, ConnectionInstanceData connectionInstanceData)
          Called from HTTPClientFactory.newHTTPClient() to create a new HttpClient.
protected  Credentials createProxyCredentials(ConnectionInstanceData connectionInstanceData)
           
 void dispose(HttpClient httpClient)
           Called to dispose an HttpClient that was previously obtained from a call to HTTPClientFactory.newHTTPClient().
 java.lang.String getUserAgent(HttpClient httpClient, ConnectionInstanceData connectionInstanceData)
          Called from ConfigurableHTTPClientFactory.configureClientParams(HttpClient, HttpClientParams, ConnectionInstanceData) to obtain a user-agent string to configure a new HttpClient with.
protected  java.lang.String getUserAgentExtraString(HttpClient httpClient, ConnectionInstanceData connectionInstanceData)
           Subclasses can override to provide extra text that gets appended to the parenthetical part of the user agent HTTP header.
 HttpClient newHTTPClient()
          Called to obtain a new HttpClient instance.
protected  boolean shouldAcceptUntrustedCertificates(ConnectionInstanceData connectionInstanceData)
          Called from createHTTPClient(HttpConnectionManager, ConnectionInstanceData) to test whether an HttpClient created for the specified connection should be configured to accept untrusted SSL certificates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONNECT_TIMEOUT_SECONDS_PROPERTY

public static final java.lang.String CONNECT_TIMEOUT_SECONDS_PROPERTY
Constant Field Value:
"com.microsoft.tfs.core.connectTimeoutSeconds"
See Also:
Constant Field Values

CONNECT_TIMEOUT_SECONDS_DEFAULT

public static final int CONNECT_TIMEOUT_SECONDS_DEFAULT
Constant Field Value:
30
See Also:
Constant Field Values

SOCKET_TIMEOUT_SECONDS_PROPERTY

public static final java.lang.String SOCKET_TIMEOUT_SECONDS_PROPERTY
Constant Field Value:
"com.microsoft.tfs.core.socketTimeoutSeconds"
See Also:
Constant Field Values

SOCKET_TIMEOUT_SECONDS_DEFAULT

public static final int SOCKET_TIMEOUT_SECONDS_DEFAULT
Constant Field Value:
1800
See Also:
Constant Field Values

MAX_TOTAL_CONNECTIONS_PROPERTY

public static final java.lang.String MAX_TOTAL_CONNECTIONS_PROPERTY
Constant Field Value:
"com.microsoft.tfs.core.maxTotalConnections"
See Also:
Constant Field Values

MAX_TOTAL_CONNECTIONS_DEFAULT

public static final int MAX_TOTAL_CONNECTIONS_DEFAULT
Constant Field Value:
40
See Also:
Constant Field Values

MAX_CONNECTIONS_PER_HOST_PROPERTY

public static final java.lang.String MAX_CONNECTIONS_PER_HOST_PROPERTY
Constant Field Value:
"com.microsoft.tfs.core.maxConnectionsPerHost"
See Also:
Constant Field Values

MAX_CONNECTIONS_PER_HOST_DEFAULT

public static final int MAX_CONNECTIONS_PER_HOST_DEFAULT
Constant Field Value:
10
See Also:
Constant Field Values

DISABLE_HTTP_CANCEL_THREAD_PROPERTY

public static final java.lang.String DISABLE_HTTP_CANCEL_THREAD_PROPERTY
Constant Field Value:
"com.microsoft.tfs.core.disableCancelThread"
See Also:
Constant Field Values

ECLIPSE_GROUP_NAME

public static final java.lang.String ECLIPSE_GROUP_NAME
Constant Field Value:
"Eclipse Platform"
See Also:
Constant Field Values

USER_AGENT_EXTRA_TEXT_MAX_CHARS

public static final int USER_AGENT_EXTRA_TEXT_MAX_CHARS
The maximum length in characters of the extra user agent text part.

Constant Field Value:
30
See Also:
Constant Field Values

USER_AGENT_OS_INFO_MAX_CHARS

public static final int USER_AGENT_OS_INFO_MAX_CHARS
The maximum length in characters of the operating system info part.

Constant Field Value:
30
See Also:
Constant Field Values
Constructor Detail

DefaultHTTPClientFactory

public DefaultHTTPClientFactory(ConnectionInstanceData connectionInstanceData)
Creates a new DefaultHTTPClientFactory that will data contained in the specified instance data to configure HttpClients.

Parameters:
serverURI - the URI that will be connected to (must not be null)
Method Detail

newHTTPClient

public HttpClient newHTTPClient()
Description copied from interface: HTTPClientFactory
Called to obtain a new HttpClient instance.

Specified by:
newHTTPClient in interface HTTPClientFactory
Returns:
a new HttpClient (must not be null)

dispose

public void dispose(HttpClient httpClient)

Called to dispose an HttpClient that was previously obtained from a call to HTTPClientFactory.newHTTPClient().

This method is called by TFSConnection.close() to clean up any http client resources. It can be assumed that the specified HttpClient instance will no longer be used after calling this method.

Specified by:
dispose in interface HTTPClientFactory
Parameters:
httpClient - an HttpClient instance to dispose (must not be null)

createConnectionManager

public HttpConnectionManager createConnectionManager(ConnectionInstanceData connectionInstanceData)
Called from HTTPClientFactory.newHTTPClient() to create and configure a new HttpConnectionManager as part of creating a new HttpClient instance. Subclasses may override. The default behavior is to instantiate a MultiThreadedHttpConnectionManager and configure it with some default parameters.

Specified by:
createConnectionManager in interface ConfigurableHTTPClientFactory
Returns:
a new HttpConnectionManager (must not be null)

createHTTPClient

public HttpClient createHTTPClient(HttpConnectionManager connectionManager,
                                   ConnectionInstanceData connectionInstanceData)
Called from HTTPClientFactory.newHTTPClient() to create a new HttpClient. Most of the configuration should be done by other methods - see the javadoc on those methods for details. This method is called after ConfigurableHTTPClientFactory.createConnectionManager(ConnectionInstanceData) and is passed the HttpConnectionManager returned from that method. Subclasses may override.

This method will modify the HTTPClient Protocol registration for "https" protocols to use a custom SecureProtocolSocketFactory that will allow users to accept untrusted certificates.

Specified by:
createHTTPClient in interface ConfigurableHTTPClientFactory
Parameters:
connectionManager - the HttpConnectionManager returned from ConfigurableHTTPClientFactory.createConnectionManager(ConnectionInstanceData) (never null )
connectionInstanceData - the ConnectionInstanceData being used to supply configuration data (never null)
Returns:
a new HttpClient instance (must not be null)

shouldAcceptUntrustedCertificates

protected boolean shouldAcceptUntrustedCertificates(ConnectionInstanceData connectionInstanceData)
Called from createHTTPClient(HttpConnectionManager, ConnectionInstanceData) to test whether an HttpClient created for the specified connection should be configured to accept untrusted SSL certificates. Subclasses may override. The default behavior is to check an environment variable. If the value of the environment variable EnvironmentVariables.ACCEPT_UNTRUSTED_CERTIFICATES is set, true is returned. Otherwise, false is returned.

Parameters:
connectionInstanceData - the ConnectionInstanceData being used to supply configuration data (never null)
Returns:
true to configure the new HttpClient instance to accept untrusted SSL certificates

configureClientParams

public void configureClientParams(HttpClient httpClient,
                                  HttpClientParams params,
                                  ConnectionInstanceData connectionInstanceData)
Called from HTTPClientFactory.newHTTPClient() to configure a new HttpClient's HttpClientParams. Subclasses may override. The default behavior is to configure the http.protocol.expect-continue parameter to false and set a custom user agent string.

Specified by:
configureClientParams in interface ConfigurableHTTPClientFactory
Parameters:
httpClient - the HttpClient being configured (never null)
params - the HttpClientParams to configure (never null )
connectionInstanceData - the ConnectionInstanceData being used to supply configuration data (never null)

getUserAgent

public final java.lang.String getUserAgent(HttpClient httpClient,
                                           ConnectionInstanceData connectionInstanceData)
Called from ConfigurableHTTPClientFactory.configureClientParams(HttpClient, HttpClientParams, ConnectionInstanceData) to obtain a user-agent string to configure a new HttpClient with.

Use getUserAgentExtraString(HttpClient, ConnectionInstanceData) to customize thet user agent string.

Specified by:
getUserAgent in interface ConfigurableHTTPClientFactory
Parameters:
httpClient - the HttpClient being configured (never null)
connectionInstanceData - the ConnectionInstanceData being used to supply configuration data (never null)
Returns:
the user-agent string to use, or null to not configure a user-agent

getUserAgentExtraString

protected java.lang.String getUserAgentExtraString(HttpClient httpClient,
                                                   ConnectionInstanceData connectionInstanceData)

Subclasses can override to provide extra text that gets appended to the parenthetical part of the user agent HTTP header. If null or empty string is returned no extra text is appended. The returned string should be 30 characters or less (it will be truncated if it exceeds this limit).

The user agent header is formatted like:

 ProductFamily, SKU:XX (ProductName 1.2.3.4567890; OS Arch Version; extra text goes here if present)
 

Parameters:
httpClient - the HttpClient being configured (must not be null)
connectionInstanceData - the connection instance data (must not be null)
Returns:
a string to put at the end of the parenthetical part of the user agent header (30 characaters or less), or null or the emptry string to omit the extra part

configureClientCredentials

public void configureClientCredentials(HttpClient httpClient,
                                       HttpState state,
                                       ConnectionInstanceData connectionInstanceData)
Called from HTTPClientFactory.newHTTPClient() to configure credentials for a new HttpClient instance. Subclasses may override. The default behavior is to call #createCredentials(ConnectionInstanceData). If that method returns a non-null Credentials object, then that object is set as the credentials for the HttpState.

Specified by:
configureClientCredentials in interface ConfigurableHTTPClientFactory
Parameters:
httpClient - the HttpClient being configured (never null)
state - the HttpState to configure credentials on (never null)
connectionInstanceData - the ConnectionInstanceData being used to supply configuration data (never null)

createCredentials

protected Credentials createCredentials(ConnectionInstanceData connectionInstanceData)
Called from configureClientCredentials(HttpClient, HttpState, ConnectionInstanceData) to create a new Credentials instance for the specified ConnectionInstanceData. Subclasses may override. The default behavior is to simply return default credentials.

Parameters:
connectionInstanceData - the ConnectionInstanceData to get configuration data from (never null)
Returns:
a Credentials object or null to not use Credentials

createProxyCredentials

protected Credentials createProxyCredentials(ConnectionInstanceData connectionInstanceData)

configureClientProxy

public void configureClientProxy(HttpClient httpClient,
                                 HostConfiguration hostConfiguration,
                                 HttpState httpState,
                                 ConnectionInstanceData connectionInstanceData)
Called from HTTPClientFactory.newHTTPClient() to configure proxy settings for a new HttpClient instance. Subclasses may override.

Specified by:
configureClientProxy in interface ConfigurableHTTPClientFactory
Parameters:
httpClient - the HttpClient being configured (never null)
hostConfiguration - the HostConfiguration of the new HttpClient instance (never null)
httpState - the HttpState of the new HttpClient instance (never null)
connectionInstanceData - the ConnectionInstanceData being used to supply configuration data (never null)

configureClient

public void configureClient(HttpClient httpClient,
                            ConnectionInstanceData connectionInstanceData)
Called from HTTPClientFactory.newHTTPClient() to perform final configuration of a new HttpClient instance before it is returned to the caller of that method. Subclasses may override. The default behavior is to call #addClientToCloserThread(HttpClient) with the new client instance.

Specified by:
configureClient in interface ConfigurableHTTPClientFactory
Parameters:
httpClient - the new HttpClient instance (never null)
connectionInstanceData - the ConnectionInstanceData being used to supply configuration data (never null)

addClientToCloserThread

protected final void addClientToCloserThread(HttpClient httpClient)
Called to add the specified HttpClient instance to a static thread that will handle idle connection closing.

Parameters:
httpClient - the HttpClient instance to add (must not be null)


© 2015 Microsoft. All rights reserved.