com.googlecode.xremoting.core.commonshttpclient
Class HttpClientBuilder

java.lang.Object
  extended by com.googlecode.xremoting.core.commonshttpclient.HttpClientBuilder

public class HttpClientBuilder
extends Object

Used to build an HttpClient using method chain pattern.

Author:
Roman Puchkovskiy

Constructor Summary
HttpClientBuilder()
           
 
Method Summary
 HttpClientBuilder authPreemprive(boolean pre)
          Sets whether the resulting HttpClient should send authorization before it's requested by the server.
 HttpClientBuilder basicAuth(String username, String password)
          Turns on a Basic authentication.
 HttpClientBuilder basicAuthHost(String host)
          Constraints host for which Basic auth is sent.
 HttpClientBuilder basicAuthPort(int port)
          Constraints port for which Basic auth is sent.
 org.apache.commons.httpclient.HttpClient build()
          Builds the configured HttpClient.
static HttpClientBuilder create()
          Creates a new builder.
 HttpClientBuilder keyStore(URL url, String password)
          Configures a key store for the current SSL host (see ssl(String)).
 HttpClientBuilder proxyHost(String host)
          Sets proxy host.
 HttpClientBuilder proxyPort(int port)
          Sets proxy port.
 HttpClientBuilder securePort(int port)
          Configures secure port for the HTTPS for current host (see ssl(String)).
 HttpClientBuilder secureSchema(String schema)
          Configures secure schema for the HTTPS for current host (see ssl(String)).
 HttpClientBuilder ssl(String host)
          Begins SSL configuration for the given host.
 HttpClientBuilder sslEnabledProtocols(String[] protocols)
          Sets a list of enabled SSL protocols.
 HttpClientBuilder timeouts(int connectionTimeout, int soTimeout, long connectionManagerTimeout)
          Configures timeouts of the HttpClient being built.
 HttpClientBuilder trustKeyStore(URL url, String password)
          Configures a trust store for the current SSL host (see ssl(String)).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpClientBuilder

public HttpClientBuilder()
Method Detail

create

public static HttpClientBuilder create()
Creates a new builder.

Returns:
builder

proxyHost

public HttpClientBuilder proxyHost(String host)
Sets proxy host. If called, proxy will be used by the resulting HttpClient, else it will connect directly.

Parameters:
host - proxy host
Returns:
this

proxyPort

public HttpClientBuilder proxyPort(int port)
Sets proxy port.

Parameters:
port - proxy port
Returns:
this
See Also:
proxyHost(String)

authPreemprive

public HttpClientBuilder authPreemprive(boolean pre)
Sets whether the resulting HttpClient should send authorization before it's requested by the server. If not set, it's true. Ignored if Basic authentication is not configured.

Parameters:
pre - whether to use auth preemptive
Returns:
this
See Also:
basicAuth(String, String)

basicAuth

public HttpClientBuilder basicAuth(String username,
                                   String password)
Turns on a Basic authentication.

Parameters:
username - username to send
password - password to send
Returns:
this
See Also:
basicAuthHost(String), basicAuthPort(int)

basicAuthHost

public HttpClientBuilder basicAuthHost(String host)
Constraints host for which Basic auth is sent. If not called, Basic auth will be sent to any host. Ignored if Basic auth is not configured.

Parameters:
host - host for which to send Basic auth (for others it will not be sent)
Returns:
this
See Also:
basicAuth(String, String)

basicAuthPort

public HttpClientBuilder basicAuthPort(int port)
Constraints port for which Basic auth is sent. If not called, Basic auth will be sent for connections to any port. Ignored if Basic auth is not configured.

Parameters:
port - port for which to send Basic auth (for others it will not be sent)
Returns:
this
See Also:
basicAuth(String, String)

ssl

public HttpClientBuilder ssl(String host)
Begins SSL configuration for the given host. Actual configuration must follow (see trustKeyStore(URL, String), keyStore(URL, String), secureSchema(String), securePort(int)). Allows to enable both server validation (by supplying keyStore) and client authentication (by supplying trustKeyStore).

Parameters:
host - host for which to enable SSL
Returns:
this
See Also:
trustKeyStore(URL, String), keyStore(URL, String), secureSchema(String), securePort(int)

sslEnabledProtocols

public HttpClientBuilder sslEnabledProtocols(String[] protocols)
Sets a list of enabled SSL protocols. No other protocol will not be used.

Parameters:
protocols - protocols
Returns:
this

trustKeyStore

public HttpClientBuilder trustKeyStore(URL url,
                                       String password)
Configures a trust store for the current SSL host (see ssl(String)). If set, SSL client authentication will be used when connecting to that host (i.e. the client certificate will be sent).

Parameters:
url - URL from which to obtain trust store
password - trust store password
Returns:
this
See Also:
ssl(String), keyStore(URL, String)

keyStore

public HttpClientBuilder keyStore(URL url,
                                  String password)
Configures a key store for the current SSL host (see ssl(String)). If set, SSL server validation will be used (i.e. the server certificate will be requested and validated).

Parameters:
url - URL from which to obtain key store
password - key store password
Returns:
this
See Also:
ssl(String), trustKeyStore(URL, String)

secureSchema

public HttpClientBuilder secureSchema(String schema)
Configures secure schema for the HTTPS for current host (see ssl(String)).

Parameters:
schema - secure schema (usually, this is "https")
Returns:
this
See Also:
ssl(String)

securePort

public HttpClientBuilder securePort(int port)
Configures secure port for the HTTPS for current host (see ssl(String)).

Parameters:
port - secure port (if this method is not called, it's defaulted to 443)
Returns:
this
See Also:
ssl(String)

timeouts

public HttpClientBuilder timeouts(int connectionTimeout,
                                  int soTimeout,
                                  long connectionManagerTimeout)
Configures timeouts of the HttpClient being built.

Parameters:
connectionTimeout - time in millis for connection to be established
soTimeout - SO timeout (millis)
connectionManagerTimeout - time in millis for connection to be obtained from connection manager
Returns:
this

build

public org.apache.commons.httpclient.HttpClient build()
Builds the configured HttpClient.

Returns:
HttpClient instance


Copyright © 2013. All Rights Reserved.