|
|||||||||
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
Field Summary | |
---|---|
static boolean |
DEFAULT_AUTOHANDLING_COOKIES
|
static int |
DEFAULT_CACHE_SIZE
|
static int |
DEFAULT_CREATION_MAX_WAIT_TIMEOUT
|
static boolean |
DEFAULT_FOLLOWS_REDIRECT
|
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()
|
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()
returns true, if redirects should be followed |
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()
gets if a 302 response should be treat as a 303 response |
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 |
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)
sets if redirects should be followed. |
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)
sets if a 302 response should be treat as a 303 response |
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 Long DEFAULT_RESPONSE_TIMEOUT_SEC
public static final int DEFAULT_MAX_REDIRECTS
public static final boolean DEFAULT_FOLLOWS_REDIRECT
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 usepublic HttpClient(SSLContext sslCtx, IHttpRequestHandler... interceptors)
sslCtx
- the ssl context to useinterceptors
- the interceptorsMethod 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)
isFollowsRedirect
- true, if redirects should be followedpublic boolean getFollowsRedirect()
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)
isTreat303RedirectAs302
- true, if a 303 response should be treat a a 303 responsepublic 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 IHttpClientEndpoint
responseTimeoutMillis
- the response timeoutpublic long getResponseTimeoutMillis()
getResponseTimeoutMillis
in interface IHttpClientEndpoint
public final void setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis)
public final long getBodyDataReceiveTimeoutMillis()
public void close() throws IOException
close
in interface Closeable
IOException
public boolean isOpen()
isOpen
in interface IConnectionPool
public String getId()
getId
in interface IHttpClientEndpoint
public void addListener(ILifeCycle listener)
addListener
in interface IConnectionPool
public boolean removeListener(ILifeCycle listener)
removeListener
in interface IConnectionPool
public void setPooledMaxIdleTimeMillis(int idleTimeoutMillis)
setPooledMaxIdleTimeMillis
in interface IConnectionPool
public int getPooledMaxIdleTimeMillis()
getPooledMaxIdleTimeMillis
in interface IConnectionPool
public void setPooledMaxLifeTimeMillis(int lifeTimeoutMillis)
setPooledMaxLifeTimeMillis
in interface IConnectionPool
public int getPooledMaxLifeTimeMillis()
getPooledMaxLifeTimeMillis
in interface IConnectionPool
public void setMaxIdle(int maxIdle)
setMaxIdle
in interface IConnectionPool
public int getMaxIdle()
getMaxIdle
in interface IConnectionPool
public void setMaxActive(int maxActive)
setMaxActive
in interface IConnectionPool
public int getMaxActivePerServer()
getMaxActivePerServer
in interface IConnectionPool
public void setMaxActivePerServer(int maxActivePerServer)
setMaxActivePerServer
in interface IConnectionPool
public int getMaxActive()
getMaxActive
in interface IConnectionPool
public void setConnectTimeoutMillis(int connectTimeoutMillis)
connectTimeoutMillis
- the connect timeoutpublic int getConnectTimeoutMillis()
public int getNumActive()
getNumActive
in interface IConnectionPool
public int getNumIdle()
getNumIdle
in interface IConnectionPool
public int getNumCreated()
getNumCreated
in interface IConnectionPool
public int getNumDestroyed()
getNumDestroyed
in interface IConnectionPool
public int getNumTimeoutPooledMaxIdleTime()
getNumTimeoutPooledMaxIdleTime
in interface IConnectionPool
public int getNumTimeoutPooledMaxLifeTime()
getNumTimeoutPooledMaxLifeTime
in interface IConnectionPool
public int getNumCacheHit()
public int getNumCacheMiss()
public List<String> getActiveConnectionInfos()
getActiveConnectionInfos
in interface IConnectionPool
public List<String> getIdleConnectionInfos()
getIdleConnectionInfos
in interface IConnectionPool
public IHttpResponse call(IHttpRequest request) throws IOException, SocketTimeoutException
call
in interface IHttpClientEndpoint
request
- 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 exceedpublic IFutureResponse send(IHttpRequest request) throws IOException, ConnectException
send
in interface IHttpClientEndpoint
request
- the request
ConnectException
- if an error occurred while attempting to connect to a remote address and port.
IOException
- if an exception occurspublic void send(IHttpRequest request, IHttpResponseHandler responseHandler) throws IOException, ConnectException
send
in interface IHttpClientEndpoint
request
- 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 occurspublic BodyDataSink send(IHttpRequestHeader requestHeader, int contentLength, IHttpResponseHandler responseHandler) throws IOException, ConnectException
send
in interface IHttpClientEndpoint
requestHeader
- 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 occurspublic BodyDataSink send(IHttpRequestHeader requestHeader, IHttpResponseHandler responseHandler) throws IOException, ConnectException
send
in interface IHttpClientEndpoint
requestHeader
- 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 |