Interface Request
-
- All Known Implementing Classes:
HttpRequest
,WebSocketUpgradeRequest
public interface Request
Request
represents an HTTP request, and offers a fluent interface to customize various attributes such as the path, the headers, the content, etc.You can create
Request
objects viaHttpClient.newRequest(String)
and you can send them using eithersend()
for a blocking semantic, orsend(Response.CompleteListener)
for an asynchronous semantic.- See Also:
Response
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
Request.BeginListener
Listener for the request begin event.static interface
Request.CommitListener
Listener for the request committed event.static interface
Request.ContentListener
Listener for the request content event.static interface
Request.FailureListener
Listener for the request failed event.static interface
Request.HeadersListener
Listener for the request headers event.static interface
Request.Listener
Listener for all request events.static interface
Request.QueuedListener
Listener for the request queued event.static interface
Request.RequestListener
Common, empty, super-interface for request listeners.static interface
Request.SuccessListener
Listener for the request succeeded event.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description boolean
abort(java.lang.Throwable cause)
Attempts to abort the send of this request.Request
accept(java.lang.String... accepts)
Request
agent(java.lang.String agent)
Request
attribute(java.lang.String name, java.lang.Object value)
Request
content(ContentProvider content)
Request
content(ContentProvider content, java.lang.String contentType)
Request
cookie(java.net.HttpCookie cookie)
Request
file(java.nio.file.Path file)
Shortcut method to specify a file as a content for this request, with the default content type of "application/octect-stream".Request
file(java.nio.file.Path file, java.lang.String contentType)
Shortcut method to specify a file as a content for this request, with the given content type.Request
followRedirects(boolean follow)
java.lang.Throwable
getAbortCause()
java.lang.String
getAgent()
java.util.Map<java.lang.String,java.lang.Object>
getAttributes()
ContentProvider
getContent()
java.util.List<java.net.HttpCookie>
getCookies()
HttpFields
getHeaders()
java.lang.String
getHost()
long
getIdleTimeout()
java.lang.String
getMethod()
Fields
getParams()
java.lang.String
getPath()
int
getPort()
java.lang.String
getQuery()
<T extends Request.RequestListener>
java.util.List<T>getRequestListeners(java.lang.Class<T> listenerClass)
java.lang.String
getScheme()
java.lang.Object
getTag()
long
getTimeout()
java.net.URI
getURI()
HttpVersion
getVersion()
Request
header(java.lang.String name, java.lang.String value)
Request
header(HttpHeader header, java.lang.String value)
Adds the givenvalue
to the specifiedheader
.default Request
host(java.lang.String host)
Request
idleTimeout(long timeout, java.util.concurrent.TimeUnit unit)
boolean
isFollowRedirects()
Request
listener(Request.Listener listener)
Request
method(java.lang.String method)
Request
method(HttpMethod method)
Request
onComplete(Response.CompleteListener listener)
Request
onRequestBegin(Request.BeginListener listener)
Request
onRequestCommit(Request.CommitListener listener)
Request
onRequestContent(Request.ContentListener listener)
Request
onRequestFailure(Request.FailureListener listener)
Request
onRequestHeaders(Request.HeadersListener listener)
Request
onRequestQueued(Request.QueuedListener listener)
Request
onRequestSuccess(Request.SuccessListener listener)
Request
onResponseBegin(Response.BeginListener listener)
Request
onResponseContent(Response.ContentListener listener)
Request
onResponseContentAsync(Response.AsyncContentListener listener)
Request
onResponseContentDemanded(Response.DemandedContentListener listener)
Request
onResponseFailure(Response.FailureListener listener)
Request
onResponseHeader(Response.HeaderListener listener)
Request
onResponseHeaders(Response.HeadersListener listener)
Request
onResponseSuccess(Response.SuccessListener listener)
Request
param(java.lang.String name, java.lang.String value)
Adds a URI query parameter with the given name and value.Request
path(java.lang.String path)
Specifies the URI path - and possibly the query - of this request.default Request
port(int port)
Request
scheme(java.lang.String scheme)
ContentResponse
send()
Sends this request and returns the response.void
send(Response.CompleteListener listener)
Sends this request and asynchronously notifies the given listener for response events.Request
tag(java.lang.Object tag)
Tags this request with the given metadata tag.Request
timeout(long timeout, java.util.concurrent.TimeUnit unit)
Request
version(HttpVersion version)
-
-
-
Method Detail
-
getScheme
java.lang.String getScheme()
- Returns:
- the URI scheme of this request, such as "http" or "https"
-
scheme
Request scheme(java.lang.String scheme)
- Parameters:
scheme
- the URI scheme of this request, such as "http" or "https"- Returns:
- this request object
-
getHost
java.lang.String getHost()
- Returns:
- the URI host of this request, such as "127.0.0.1" or "google.com"
-
host
default Request host(java.lang.String host)
- Parameters:
host
- the URI host of this request, such as "127.0.0.1" or "google.com"- Returns:
- this request object
-
getPort
int getPort()
- Returns:
- the URI port of this request such as 80 or 443
-
port
default Request port(int port)
- Parameters:
port
- the URI port of this request such as 80 or 443- Returns:
- this request object
-
getMethod
java.lang.String getMethod()
- Returns:
- the method of this request, such as GET or POST, as a String
-
method
Request method(HttpMethod method)
- Parameters:
method
- the method of this request, such as GET or POST- Returns:
- this request object
-
method
Request method(java.lang.String method)
- Parameters:
method
- the method of this request, such as GET or POST- Returns:
- this request object
-
getPath
java.lang.String getPath()
- Returns:
- the URI path of this request, such as "/" or "/path" - without the query
- See Also:
getQuery()
-
path
Request path(java.lang.String path)
Specifies the URI path - and possibly the query - of this request. If the query part is specified, parameter values must be properlyUTF-8 URL encoded
. For example, if the value for parameter "currency" is the euro symbol € then the query string for this parameter must be "currency=%E2%82%AC". For transparent encoding of parameter values, useparam(String, String)
.- Parameters:
path
- the URI path of this request, such as "/" or "/path?param=1"- Returns:
- this request object
-
getQuery
java.lang.String getQuery()
- Returns:
- the URI query string of this request such as "param=1"
- See Also:
getPath()
,getParams()
-
getURI
java.net.URI getURI()
- Returns:
- the full URI of this request such as "http://host:port/path?param=1"
-
getVersion
HttpVersion getVersion()
- Returns:
- the HTTP version of this request, such as "HTTP/1.1"
-
version
Request version(HttpVersion version)
- Parameters:
version
- the HTTP version of this request, such as "HTTP/1.1"- Returns:
- this request object
-
getParams
Fields getParams()
- Returns:
- the URI query parameters of this request
-
param
Request param(java.lang.String name, java.lang.String value)
Adds a URI query parameter with the given name and value. The value isUTF-8 URL encoded
.- Parameters:
name
- the name of the query parametervalue
- the value of the query parameter- Returns:
- this request object
-
getHeaders
HttpFields getHeaders()
- Returns:
- the headers of this request
-
header
Request header(java.lang.String name, java.lang.String value)
- Parameters:
name
- the name of the headervalue
- the value of the header- Returns:
- this request object
- See Also:
header(HttpHeader, String)
-
header
Request header(HttpHeader header, java.lang.String value)
Adds the given
value
to the specifiedheader
.Multiple calls with the same parameters will add multiple values; use the value
null
to remove the header completely.- Parameters:
header
- the header namevalue
- the value of the header- Returns:
- this request object
-
getCookies
java.util.List<java.net.HttpCookie> getCookies()
- Returns:
- the cookies associated with this request
-
cookie
Request cookie(java.net.HttpCookie cookie)
- Parameters:
cookie
- a cookie for this request- Returns:
- this request object
-
tag
Request tag(java.lang.Object tag)
Tags this request with the given metadata tag.
Each different tag will create a different destination, even if the destination origin is the same.
This is particularly useful in proxies, where requests for the same origin but from different clients may be tagged with client's metadata (e.g. the client remote address).
The tag metadata class must correctly implement
Object.hashCode()
andObject.equals(Object)
so that it can be used, along with the origin, to identify a destination.- Parameters:
tag
- the metadata to tag the request with- Returns:
- this request object
-
getTag
java.lang.Object getTag()
- Returns:
- the metadata this request has been tagged with
-
attribute
Request attribute(java.lang.String name, java.lang.Object value)
- Parameters:
name
- the name of the attributevalue
- the value of the attribute- Returns:
- this request object
-
getAttributes
java.util.Map<java.lang.String,java.lang.Object> getAttributes()
- Returns:
- the attributes of this request
-
getContent
ContentProvider getContent()
- Returns:
- the content provider of this request
-
content
Request content(ContentProvider content)
- Parameters:
content
- the content provider of this request- Returns:
- this request object
-
content
Request content(ContentProvider content, java.lang.String contentType)
- Parameters:
content
- the content provider of this requestcontentType
- the content type- Returns:
- this request object
-
file
Request file(java.nio.file.Path file) throws java.io.IOException
Shortcut method to specify a file as a content for this request, with the default content type of "application/octect-stream".- Parameters:
file
- the file to upload- Returns:
- this request object
- Throws:
java.io.IOException
- if the file does not exist or cannot be read
-
file
Request file(java.nio.file.Path file, java.lang.String contentType) throws java.io.IOException
Shortcut method to specify a file as a content for this request, with the given content type.- Parameters:
file
- the file to uploadcontentType
- the content type of the file- Returns:
- this request object
- Throws:
java.io.IOException
- if the file does not exist or cannot be read
-
getAgent
java.lang.String getAgent()
- Returns:
- the user agent for this request
-
agent
Request agent(java.lang.String agent)
- Parameters:
agent
- the user agent for this request (corresponds to theUser-Agent
header)- Returns:
- this request object
-
accept
Request accept(java.lang.String... accepts)
- Parameters:
accepts
- the media types that are acceptable in the response, such as "text/plain;q=0.5" or "text/html" (corresponds to theAccept
header)- Returns:
- this request object
-
getIdleTimeout
long getIdleTimeout()
- Returns:
- the idle timeout for this request, in milliseconds
-
idleTimeout
Request idleTimeout(long timeout, java.util.concurrent.TimeUnit unit)
- Parameters:
timeout
- the idle timeout for this requestunit
- the idle timeout unit- Returns:
- this request object
-
getTimeout
long getTimeout()
- Returns:
- the total timeout for this request, in milliseconds; zero or negative if the timeout is disabled
-
timeout
Request timeout(long timeout, java.util.concurrent.TimeUnit unit)
- Parameters:
timeout
- the total timeout for the request/response conversation; use zero or a negative value to disable the timeoutunit
- the timeout unit- Returns:
- this request object
-
isFollowRedirects
boolean isFollowRedirects()
- Returns:
- whether this request follows redirects
-
followRedirects
Request followRedirects(boolean follow)
- Parameters:
follow
- whether this request follows redirects- Returns:
- this request object
-
getRequestListeners
<T extends Request.RequestListener> java.util.List<T> getRequestListeners(java.lang.Class<T> listenerClass)
- Type Parameters:
T
- the type of listener class- Parameters:
listenerClass
- the class of the listener, or null for all listeners classes- Returns:
- the listeners for request events of the given class
-
listener
Request listener(Request.Listener listener)
- Parameters:
listener
- a listener for request events- Returns:
- this request object
-
onRequestQueued
Request onRequestQueued(Request.QueuedListener listener)
- Parameters:
listener
- a listener for request queued event- Returns:
- this request object
-
onRequestBegin
Request onRequestBegin(Request.BeginListener listener)
- Parameters:
listener
- a listener for request begin event- Returns:
- this request object
-
onRequestHeaders
Request onRequestHeaders(Request.HeadersListener listener)
- Parameters:
listener
- a listener for request headers event- Returns:
- this request object
-
onRequestCommit
Request onRequestCommit(Request.CommitListener listener)
- Parameters:
listener
- a listener for request commit event- Returns:
- this request object
-
onRequestContent
Request onRequestContent(Request.ContentListener listener)
- Parameters:
listener
- a listener for request content events- Returns:
- this request object
-
onRequestSuccess
Request onRequestSuccess(Request.SuccessListener listener)
- Parameters:
listener
- a listener for request success event- Returns:
- this request object
-
onRequestFailure
Request onRequestFailure(Request.FailureListener listener)
- Parameters:
listener
- a listener for request failure event- Returns:
- this request object
-
onResponseBegin
Request onResponseBegin(Response.BeginListener listener)
- Parameters:
listener
- a listener for response begin event- Returns:
- this request object
-
onResponseHeader
Request onResponseHeader(Response.HeaderListener listener)
- Parameters:
listener
- a listener for response header event- Returns:
- this request object
-
onResponseHeaders
Request onResponseHeaders(Response.HeadersListener listener)
- Parameters:
listener
- a listener for response headers event- Returns:
- this request object
-
onResponseContent
Request onResponseContent(Response.ContentListener listener)
- Parameters:
listener
- a consuming listener for response content events- Returns:
- this request object
-
onResponseContentAsync
Request onResponseContentAsync(Response.AsyncContentListener listener)
- Parameters:
listener
- an asynchronous listener for response content events- Returns:
- this request object
-
onResponseContentDemanded
Request onResponseContentDemanded(Response.DemandedContentListener listener)
- Parameters:
listener
- an asynchronous listener for response content events- Returns:
- this request object
-
onResponseSuccess
Request onResponseSuccess(Response.SuccessListener listener)
- Parameters:
listener
- a listener for response success event- Returns:
- this request object
-
onResponseFailure
Request onResponseFailure(Response.FailureListener listener)
- Parameters:
listener
- a listener for response failure event- Returns:
- this request object
-
onComplete
Request onComplete(Response.CompleteListener listener)
- Parameters:
listener
- a listener for complete event- Returns:
- this request object
-
send
ContentResponse send() throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException
Sends this request and returns the response.This method should be used when a simple blocking semantic is needed, and when it is known that the response content can be buffered without exceeding memory constraints.
For example, this method is not appropriate to download big files from a server; consider using
send(Response.CompleteListener)
instead, passing your ownResponse.Listener
or a utility listener such asInputStreamResponseListener
.The method returns when the
complete event
is fired.- Returns:
- a
ContentResponse
for this request - Throws:
java.lang.InterruptedException
- if send thread is interruptedjava.util.concurrent.TimeoutException
- if send times outjava.util.concurrent.ExecutionException
- if execution fails- See Also:
Response.CompleteListener.onComplete(Result)
-
send
void send(Response.CompleteListener listener)
Sends this request and asynchronously notifies the given listener for response events.
This method should be used when the application needs to be notified of the various response events as they happen, or when the application needs to efficiently manage the response content.
The listener passed to this method may implement not only
Response.CompleteListener
but also other response listener interfaces, and all the events implemented will be notified. This allows application code to write a single listener class to handle all relevant events.- Parameters:
listener
- the listener that receives response events
-
abort
boolean abort(java.lang.Throwable cause)
Attempts to abort the send of this request.- Parameters:
cause
- the abort cause, must not be null- Returns:
- whether the abort succeeded
-
getAbortCause
java.lang.Throwable getAbortCause()
- Returns:
- the abort cause passed to
abort(Throwable)
, or null if this request has not been aborted
-
-