|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||

java.lang.Objectorg.xlightweb.client.HttpClient
public class HttpClient
Higher level client-side abstraction of the client side endpoint. Internally, the HttpClient uses a pool
of HttpClientConnection to perform the requests. Example:
HttpClient httpClient = new HttpClient();
// set some properties
httpClient.setFollowsRedirect(true);
httpClient.setAutoHandleCookies(false);
// ...
// perform a synchronous call
IHttpResponse response = httpClient.call(new GetRequest("http://www.gmx.com/index.html"));
System.out.println(response.getStatus());
BlockingBodyDataSource bodyChannel = response.getBlockingBody();
System.out.println(bodyChannel.readString());
// perform an asynchronous request
MyResponseHandler respHdl = new MyResponseHandler();
httpClient.send(new HttpRequestHeader("GET", "http://www.gmx.com/index.html"), respHdl);
//..
httpClient.close();
The HttpClient is thread-safe
| Nested Class Summary | |
|---|---|
static class |
HttpClient.FollowsRedirectMode
|
| Field Summary | |
|---|---|
static boolean |
DEFAULT_AUTOCONFIRM_REDIRECT
|
static boolean |
DEFAULT_AUTOHANDLING_COOKIES
|
static int |
DEFAULT_CACHE_SIZE
|
static int |
DEFAULT_CREATION_MAX_WAIT_TIMEOUT
|
static boolean |
DEFAULT_FOLLOWS_REDIRECT
|
static HttpClient.FollowsRedirectMode |
DEFAULT_FOLLOWS_REDIRECTMODE
|
static int |
DEFAULT_MAX_REDIRECTS
|
static int |
DEFAULT_MAX_RETRIES
|
static int |
DEFAULT_POOLED_IDLE_TIMEOUT_MILLIS
|
static int |
DEFAULT_POOLED_LIFE_TIMEOUT_MILLIS
|
static boolean |
DEFAULT_PROXY_ACTIVATED
|
static Long |
DEFAULT_RESPONSE_TIMEOUT_SEC
|
static boolean |
DEFAULT_TREAT_302_REDIRECT_AS_303
|
| Fields inherited from interface org.xlightweb.client.IHttpClientEndpoint |
|---|
DEFAULT_AUTOHANDLE_100CONTINUE_RESPONSE |
| Fields inherited from interface org.xsocket.connection.IConnectionPool |
|---|
DEFAULT_CREATION_TIMEOUT_MILLIS, DEFAULT_IDLE_TIMEOUT_MILLIS, DEFAULT_LIFE_TIMEOUT_MILLIS, DEFAULT_MAX_ACTIVE, DEFAULT_MAX_ACTIVE_PER_SERVER, DEFAULT_MAX_IDLE, DEFAULT_MAX_WAIT_MILLIS, DEFAULT_MAX_WAITING |
| Constructor Summary | |
|---|---|
HttpClient()
constructor |
|
HttpClient(IHttpRequestHandler... interceptors)
constructor |
|
HttpClient(SSLContext sslCtx)
constructor |
|
HttpClient(SSLContext sslCtx,
IHttpRequestHandler... interceptors)
constructor |
|
| Method Summary | |
|---|---|
void |
addInterceptor(IHttpRequestHandler interceptor)
adds an interceptor. |
void |
addListener(ILifeCycle listener)
|
IHttpResponse |
call(IHttpRequest request)
performs a request. |
void |
close()
|
Integer |
getAcquireTimeoutMillis()
returns the acquire timeout. |
List<String> |
getActiveConnectionInfos()
|
long |
getBodyDataReceiveTimeoutMillis()
|
int |
getCacheMaxSizeKB()
returns the max cache size |
float |
getCacheSizeKB()
returns the cache size |
boolean |
getCallReturnOnMessage()
return true when the call will return, if the message is received |
int |
getConnectTimeoutMillis()
returns the connect timeout |
boolean |
getFollowsRedirect()
Deprecated. |
HttpClient.FollowsRedirectMode |
getFollowsRedirectMode()
returns the follow redirect mode |
String |
getId()
returns a unique id |
List<String> |
getIdleConnectionInfos()
|
int |
getMaxActive()
|
int |
getMaxActivePerServer()
|
int |
getMaxIdle()
|
int |
getMaxRedirects()
get the max redirects (of GET, DELETE and PUT calls) |
int |
getMaxRetries()
get the max retries (of GET, DELETE and PUT calls) |
int |
getNumActive()
|
int |
getNumCacheHit()
returns the number of cache hits |
int |
getNumCacheMiss()
returns the number of cache misses |
int |
getNumCreated()
|
int |
getNumDestroyed()
|
int |
getNumIdle()
|
int |
getNumTimeoutPooledMaxIdleTime()
|
int |
getNumTimeoutPooledMaxLifeTime()
|
int |
getPooledMaxIdleTimeMillis()
|
int |
getPooledMaxLifeTimeMillis()
|
long |
getResponseTimeoutMillis()
returns the response timeout |
boolean |
isAutohandleCookies()
returns if cookies should be auto handled |
boolean |
isAutoUncompress()
return true, if the response will be uncompressed (if compressed) |
boolean |
isCacheShared()
returns true, if the cache is shared between users |
boolean |
isOpen()
|
boolean |
isTreat302RedirectAs303()
Deprecated. |
boolean |
removeListener(ILifeCycle listener)
|
IFutureResponse |
send(IHttpRequest request)
send the request. |
BodyDataSink |
send(IHttpRequestHeader requestHeader,
IHttpResponseHandler responseHandler)
send the request. |
BodyDataSink |
send(IHttpRequestHeader requestHeader,
int contentLength,
IHttpResponseHandler responseHandler)
send the request in a plain body mode Connection-LifeCycle support: If the request header contains a 'Connection: close' entry, the connection will be closed after the HTTP transaction |
void |
send(IHttpRequest request,
IHttpResponseHandler responseHandler)
send the request. |
void |
setAcquireTimeoutMillis(Integer aquireTimeoutMillis)
sets the acquire time out. |
void |
setAutoHandleCookies(boolean isAutohandlingCookies)
sets if cookies should be auto handled |
void |
setAutoUncompress(boolean isAutoUncompress)
set if the response will be uncompressed (if compressed) |
void |
setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis)
|
void |
setCacheMaxSizeKB(int maxSizeKB)
sets the cache size (in kilo bytes) |
void |
setCacheShared(boolean isSharedCache)
sets if the cache is shared between users |
void |
setCallReturnOnMessage(boolean isCallReturnOnMessage)
set that the call will return if the complete message is received. |
void |
setConnectTimeoutMillis(int connectTimeoutMillis)
sets the connect timeout |
void |
setFollowsRedirect(boolean isFollowsRedirect)
Deprecated. use HttpClient#setFollowsRedirectMode(FollowsRedirectMode.ALL)) instead. |
void |
setFollowsRedirectMode(HttpClient.FollowsRedirectMode mode)
sets if redirects should be handled automatically. |
void |
setFollowsRedirectMode(String mode)
sets if redirects should be handled automatically. |
void |
setMaxActive(int maxActive)
|
void |
setMaxActivePerServer(int maxActivePerServer)
|
void |
setMaxIdle(int maxIdle)
|
void |
setMaxRedirects(int maxRedirects)
set the max redirects |
void |
setMaxRetries(int maxRetries)
sets the max retries (of GET, DELETE and PUT calls) |
void |
setPooledMaxIdleTimeMillis(int idleTimeoutMillis)
|
void |
setPooledMaxLifeTimeMillis(int lifeTimeoutMillis)
|
void |
setProxyHost(String proxyHost)
sets the proxy host to use. |
void |
setProxyPassword(String proxyPassword)
sets the user password for proxy authentification |
void |
setProxyPort(int proxyPort)
sets the proxy port. |
void |
setProxyUser(String proxyUser)
sets the user name for proxy authentification |
void |
setResponseBodyDefaultEncoding(String defaultEncoding)
set the response body default encoding. |
void |
setResponseTimeoutMillis(long responseTimeoutMillis)
set the response time out by performing the call or send method. |
void |
setTreat302RedirectAs303(boolean isTreat303RedirectAs302)
Deprecated. |
void |
setWorkerpool(Executor workerpool)
set the worker pool which will be assigned to the connections for call back handling |
String |
toString()
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int DEFAULT_CREATION_MAX_WAIT_TIMEOUT
public static final int DEFAULT_POOLED_LIFE_TIMEOUT_MILLIS
public static final int DEFAULT_POOLED_IDLE_TIMEOUT_MILLIS
public static final boolean DEFAULT_TREAT_302_REDIRECT_AS_303
public static final boolean DEFAULT_AUTOCONFIRM_REDIRECT
public static final Long DEFAULT_RESPONSE_TIMEOUT_SEC
public static final int DEFAULT_MAX_REDIRECTS
public static final boolean DEFAULT_FOLLOWS_REDIRECT
public static final HttpClient.FollowsRedirectMode DEFAULT_FOLLOWS_REDIRECTMODE
public static final int DEFAULT_MAX_RETRIES
public static final boolean DEFAULT_AUTOHANDLING_COOKIES
public static final boolean DEFAULT_PROXY_ACTIVATED
public static final int DEFAULT_CACHE_SIZE
| Constructor Detail |
|---|
public HttpClient()
public HttpClient(IHttpRequestHandler... interceptors)
interceptors - interceptorpublic HttpClient(SSLContext sslCtx)
sslCtx - the ssl context to use
public HttpClient(SSLContext sslCtx,
IHttpRequestHandler... interceptors)
sslCtx - the ssl context to useinterceptors - the interceptors| Method Detail |
|---|
public void addInterceptor(IHttpRequestHandler interceptor)
HttpClient httpClient = new HttpClient();
LoadBalancerRequestInterceptor lbInterceptor = new LoadBalancerRequestInterceptor();
lbInterceptor.addVirtualServer("http://customerService", "srv1:8030", "srv2:8030");
httpClient.addInterceptor(lbInterceptor);
// ...
GetRequest request = new GetRequest("http://customerService/price?id=2336&amount=5656");
IHttpResponse response = httpClient.call(request);
//...
interceptor - the interceptor to addpublic void setFollowsRedirect(boolean isFollowsRedirect)
HttpClient#setFollowsRedirectMode(FollowsRedirectMode.ALL)) instead.
public boolean getFollowsRedirect()
public void setFollowsRedirectMode(String mode)
mode - public void setFollowsRedirectMode(HttpClient.FollowsRedirectMode mode)
mode - public HttpClient.FollowsRedirectMode getFollowsRedirectMode()
public void setAutoHandleCookies(boolean isAutohandlingCookies)
isAutohandlingCookies - true, if cookies should be auto handledpublic void setCacheMaxSizeKB(int maxSizeKB)
maxSizeKB - the max cache size in bytes or 0 to deactivate cachingpublic int getCacheMaxSizeKB()
public float getCacheSizeKB()
public void setCacheShared(boolean isSharedCache)
isSharedCache - true, if the cache is shared between userspublic boolean isCacheShared()
public void setProxyHost(String proxyHost)
HttpClient httpClient = new HttpClient();
// sets the proxy adress
httpClient.setProxyHost(host);
httpClient.setProxyPort(port);
// set auth params (only necessary if proxy authentication is required)
httpClient.setProxyUser(user);
httpClient.setProxyPassword(pwd);
// calling through the proxy
IHttpResponse resp = httpClient.call(new GetRequest("http://www.gmx.com/");
// ...
proxyHost - the proxy host or public void setProxyPort(int proxyPort)
setProxyHost(String)
proxyPort - the proxy portpublic void setProxyUser(String proxyUser)
proxyUser - the user namepublic void setProxyPassword(String proxyPassword)
proxyPassword - the user passwordpublic boolean isAutohandleCookies()
public final void setAutoUncompress(boolean isAutoUncompress)
isAutoUncompress - true, if the response will be uncompressed (if compressed)public final boolean isAutoUncompress()
public void setMaxRedirects(int maxRedirects)
maxRedirects - the max redirectspublic int getMaxRedirects()
public int getMaxRetries()
public void setMaxRetries(int maxRetries)
maxRetries - the max retriespublic void setTreat302RedirectAs303(boolean isTreat303RedirectAs302)
public boolean isTreat302RedirectAs303()
public void setWorkerpool(Executor workerpool)
workerpool - the worker poolpublic void setResponseBodyDefaultEncoding(String defaultEncoding)
encoding - the defaultEncodingpublic void setCallReturnOnMessage(boolean isCallReturnOnMessage)
isCallReturnOnMessage - true, if the call is returned on Messagepublic boolean getCallReturnOnMessage()
public void setResponseTimeoutMillis(long responseTimeoutMillis)
setResponseTimeoutMillis in interface IHttpClientEndpointresponseTimeoutMillis - the response timeoutpublic long getResponseTimeoutMillis()
getResponseTimeoutMillis in interface IHttpClientEndpointpublic final void setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis)
public final long getBodyDataReceiveTimeoutMillis()
public void close()
throws IOException
close in interface CloseableIOExceptionpublic boolean isOpen()
isOpen in interface IConnectionPoolpublic String getId()
getId in interface IHttpClientEndpointpublic void addListener(ILifeCycle listener)
addListener in interface IConnectionPoolpublic boolean removeListener(ILifeCycle listener)
removeListener in interface IConnectionPoolpublic void setPooledMaxIdleTimeMillis(int idleTimeoutMillis)
setPooledMaxIdleTimeMillis in interface IConnectionPoolpublic int getPooledMaxIdleTimeMillis()
getPooledMaxIdleTimeMillis in interface IConnectionPoolpublic void setPooledMaxLifeTimeMillis(int lifeTimeoutMillis)
setPooledMaxLifeTimeMillis in interface IConnectionPoolpublic int getPooledMaxLifeTimeMillis()
getPooledMaxLifeTimeMillis in interface IConnectionPoolpublic void setMaxIdle(int maxIdle)
setMaxIdle in interface IConnectionPoolpublic int getMaxIdle()
getMaxIdle in interface IConnectionPoolpublic void setMaxActive(int maxActive)
setMaxActive in interface IConnectionPoolpublic int getMaxActivePerServer()
getMaxActivePerServer in interface IConnectionPoolpublic void setMaxActivePerServer(int maxActivePerServer)
setMaxActivePerServer in interface IConnectionPoolpublic int getMaxActive()
getMaxActive in interface IConnectionPoolpublic void setConnectTimeoutMillis(int connectTimeoutMillis)
connectTimeoutMillis - the connect timeoutpublic int getConnectTimeoutMillis()
public int getNumActive()
getNumActive in interface IConnectionPoolpublic int getNumIdle()
getNumIdle in interface IConnectionPoolpublic int getNumCreated()
getNumCreated in interface IConnectionPoolpublic int getNumDestroyed()
getNumDestroyed in interface IConnectionPoolpublic int getNumTimeoutPooledMaxIdleTime()
getNumTimeoutPooledMaxIdleTime in interface IConnectionPoolpublic int getNumTimeoutPooledMaxLifeTime()
getNumTimeoutPooledMaxLifeTime in interface IConnectionPoolpublic Integer getAcquireTimeoutMillis()
public void setAcquireTimeoutMillis(Integer aquireTimeoutMillis)
aquireTimeoutMillis - the acquire timeout or nullpublic int getNumCacheHit()
public int getNumCacheMiss()
public List<String> getActiveConnectionInfos()
getActiveConnectionInfos in interface IConnectionPoolpublic List<String> getIdleConnectionInfos()
getIdleConnectionInfos in interface IConnectionPool
public IHttpResponse call(IHttpRequest request)
throws IOException,
SocketTimeoutException
call in interface IHttpClientEndpointrequest - the request
IOException - if an exception occurs
ConnectException - if an error occurred while attempting to connect to a remote address and port.
SocketTimeoutException - if the received timeout is exceed
public IFutureResponse send(IHttpRequest request)
throws IOException,
ConnectException
send in interface IHttpClientEndpointrequest - the request
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs
public void send(IHttpRequest request,
IHttpResponseHandler responseHandler)
throws IOException,
ConnectException
send in interface IHttpClientEndpointrequest - the requestresponseHandler - the response handler or null (supported: IHttpRequestHandler, IHttpSocketTimeoutHandler)
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs
public BodyDataSink send(IHttpRequestHeader requestHeader,
int contentLength,
IHttpResponseHandler responseHandler)
throws IOException,
ConnectException
send in interface IHttpClientEndpointrequestHeader - the request headercontentLength - the content lengthresponseHandler - the response handler or null (supported: IHttpRequestHandler, IHttpSocketTimeoutHandler)
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurs
public BodyDataSink send(IHttpRequestHeader requestHeader,
IHttpResponseHandler responseHandler)
throws IOException,
ConnectException
send in interface IHttpClientEndpointrequestHeader - the request headerresponseHandler - the response handler or null (supported: IHttpRequestHandler, IHttpSocketTimeoutHandler)
ConnectException - if an error occurred while attempting to connect to a remote address and port.
IOException - if an exception occurspublic String toString()
toString in class Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||