Class HttpRequest
- java.lang.Object
-
- org.eclipse.jetty.client.HttpRequest
-
- All Implemented Interfaces:
Request
- Direct Known Subclasses:
WebSocketUpgradeRequest
public class HttpRequest extends java.lang.Object implements Request
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.client.api.Request
Request.BeginListener, Request.CommitListener, Request.ContentListener, Request.FailureListener, Request.HeadersListener, Request.Listener, Request.QueuedListener, Request.RequestListener, Request.SuccessListener
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
HttpRequest(HttpClient client, HttpConversation conversation, java.net.URI uri)
-
Method Summary
All Methods Instance Methods Concrete 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()
HttpConversation
getConversation()
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.util.function.BiFunction<Request,Request,Response.CompleteListener>
getPushListener()
java.lang.String
getQuery()
<T extends Request.RequestListener>
java.util.List<T>getRequestListeners(java.lang.Class<T> type)
protected java.util.List<Response.ResponseListener>
getResponseListeners()
java.lang.String
getScheme()
java.lang.Object
getTag()
long
getTimeout()
java.util.function.Supplier<HttpFields>
getTrailers()
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
.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.Request
port(int port)
Request
pushListener(java.util.function.BiFunction<Request,Request,Response.CompleteListener> listener)
Sets a listener for pushed resources.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)
java.lang.String
toString()
HttpRequest
trailers(java.util.function.Supplier<HttpFields> trailers)
Request
version(HttpVersion version)
-
-
-
Constructor Detail
-
HttpRequest
protected HttpRequest(HttpClient client, HttpConversation conversation, java.net.URI uri)
-
-
Method Detail
-
getConversation
public HttpConversation getConversation()
-
getScheme
public java.lang.String getScheme()
-
scheme
public Request scheme(java.lang.String scheme)
-
getHost
public java.lang.String getHost()
-
host
public Request host(java.lang.String host)
-
getPort
public int getPort()
-
port
public Request port(int port)
-
getMethod
public java.lang.String getMethod()
-
method
public Request method(HttpMethod method)
-
method
public Request method(java.lang.String method)
-
getPath
public java.lang.String getPath()
- Specified by:
getPath
in interfaceRequest
- Returns:
- the URI path of this request, such as "/" or "/path" - without the query
- See Also:
Request.getQuery()
-
path
public Request path(java.lang.String path)
Description copied from interface:Request
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, useRequest.param(String, String)
.
-
getQuery
public java.lang.String getQuery()
- Specified by:
getQuery
in interfaceRequest
- Returns:
- the URI query string of this request such as "param=1"
- See Also:
Request.getPath()
,Request.getParams()
-
getURI
public java.net.URI getURI()
-
getVersion
public HttpVersion getVersion()
- Specified by:
getVersion
in interfaceRequest
- Returns:
- the HTTP version of this request, such as "HTTP/1.1"
-
version
public Request version(HttpVersion version)
-
param
public Request param(java.lang.String name, java.lang.String value)
Description copied from interface:Request
Adds a URI query parameter with the given name and value. The value isUTF-8 URL encoded
.
-
getParams
public Fields getParams()
-
getAgent
public java.lang.String getAgent()
-
agent
public Request agent(java.lang.String agent)
-
accept
public Request accept(java.lang.String... accepts)
-
header
public Request header(java.lang.String name, java.lang.String value)
- Specified by:
header
in interfaceRequest
- Parameters:
name
- the name of the headervalue
- the value of the header- Returns:
- this request object
- See Also:
Request.header(HttpHeader, String)
-
header
public Request header(HttpHeader header, java.lang.String value)
Description copied from interface:Request
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.
-
getCookies
public java.util.List<java.net.HttpCookie> getCookies()
- Specified by:
getCookies
in interfaceRequest
- Returns:
- the cookies associated with this request
-
cookie
public Request cookie(java.net.HttpCookie cookie)
-
tag
public Request tag(java.lang.Object tag)
Description copied from interface:Request
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.
-
getTag
public java.lang.Object getTag()
-
attribute
public Request attribute(java.lang.String name, java.lang.Object value)
-
getAttributes
public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
- Specified by:
getAttributes
in interfaceRequest
- Returns:
- the attributes of this request
-
getHeaders
public HttpFields getHeaders()
- Specified by:
getHeaders
in interfaceRequest
- Returns:
- the headers of this request
-
getRequestListeners
public <T extends Request.RequestListener> java.util.List<T> getRequestListeners(java.lang.Class<T> type)
- Specified by:
getRequestListeners
in interfaceRequest
- Type Parameters:
T
- the type of listener class- Parameters:
type
- the class of the listener, or null for all listeners classes- Returns:
- the listeners for request events of the given class
-
listener
public Request listener(Request.Listener listener)
-
onRequestQueued
public Request onRequestQueued(Request.QueuedListener listener)
- Specified by:
onRequestQueued
in interfaceRequest
- Parameters:
listener
- a listener for request queued event- Returns:
- this request object
-
onRequestBegin
public Request onRequestBegin(Request.BeginListener listener)
- Specified by:
onRequestBegin
in interfaceRequest
- Parameters:
listener
- a listener for request begin event- Returns:
- this request object
-
onRequestHeaders
public Request onRequestHeaders(Request.HeadersListener listener)
- Specified by:
onRequestHeaders
in interfaceRequest
- Parameters:
listener
- a listener for request headers event- Returns:
- this request object
-
onRequestCommit
public Request onRequestCommit(Request.CommitListener listener)
- Specified by:
onRequestCommit
in interfaceRequest
- Parameters:
listener
- a listener for request commit event- Returns:
- this request object
-
onRequestContent
public Request onRequestContent(Request.ContentListener listener)
- Specified by:
onRequestContent
in interfaceRequest
- Parameters:
listener
- a listener for request content events- Returns:
- this request object
-
onRequestSuccess
public Request onRequestSuccess(Request.SuccessListener listener)
- Specified by:
onRequestSuccess
in interfaceRequest
- Parameters:
listener
- a listener for request success event- Returns:
- this request object
-
onRequestFailure
public Request onRequestFailure(Request.FailureListener listener)
- Specified by:
onRequestFailure
in interfaceRequest
- Parameters:
listener
- a listener for request failure event- Returns:
- this request object
-
onResponseBegin
public Request onResponseBegin(Response.BeginListener listener)
- Specified by:
onResponseBegin
in interfaceRequest
- Parameters:
listener
- a listener for response begin event- Returns:
- this request object
-
onResponseHeader
public Request onResponseHeader(Response.HeaderListener listener)
- Specified by:
onResponseHeader
in interfaceRequest
- Parameters:
listener
- a listener for response header event- Returns:
- this request object
-
onResponseHeaders
public Request onResponseHeaders(Response.HeadersListener listener)
- Specified by:
onResponseHeaders
in interfaceRequest
- Parameters:
listener
- a listener for response headers event- Returns:
- this request object
-
onResponseContent
public Request onResponseContent(Response.ContentListener listener)
- Specified by:
onResponseContent
in interfaceRequest
- Parameters:
listener
- a consuming listener for response content events- Returns:
- this request object
-
onResponseContentAsync
public Request onResponseContentAsync(Response.AsyncContentListener listener)
- Specified by:
onResponseContentAsync
in interfaceRequest
- Parameters:
listener
- an asynchronous listener for response content events- Returns:
- this request object
-
onResponseContentDemanded
public Request onResponseContentDemanded(Response.DemandedContentListener listener)
- Specified by:
onResponseContentDemanded
in interfaceRequest
- Parameters:
listener
- an asynchronous listener for response content events- Returns:
- this request object
-
onResponseSuccess
public Request onResponseSuccess(Response.SuccessListener listener)
- Specified by:
onResponseSuccess
in interfaceRequest
- Parameters:
listener
- a listener for response success event- Returns:
- this request object
-
onResponseFailure
public Request onResponseFailure(Response.FailureListener listener)
- Specified by:
onResponseFailure
in interfaceRequest
- Parameters:
listener
- a listener for response failure event- Returns:
- this request object
-
onComplete
public Request onComplete(Response.CompleteListener listener)
- Specified by:
onComplete
in interfaceRequest
- Parameters:
listener
- a listener for complete event- Returns:
- this request object
-
pushListener
public Request pushListener(java.util.function.BiFunction<Request,Request,Response.CompleteListener> listener)
Sets a listener for pushed resources.
When resources are pushed from the server, the given
listener
is invoked for every pushed resource. The parameters to theBiFunction
are this request and the synthesized request for the pushed resource. TheBiFunction
should return aCompleteListener
that may also implement other listener interfaces to be notified of various response events, ornull
to signal that the pushed resource should be canceled.- Parameters:
listener
- a listener for pushed resource events- Returns:
- this request object
-
trailers
public HttpRequest trailers(java.util.function.Supplier<HttpFields> trailers)
-
getContent
public ContentProvider getContent()
- Specified by:
getContent
in interfaceRequest
- Returns:
- the content provider of this request
-
content
public Request content(ContentProvider content)
-
content
public Request content(ContentProvider content, java.lang.String contentType)
-
file
public Request file(java.nio.file.Path file) throws java.io.IOException
Description copied from interface:Request
Shortcut method to specify a file as a content for this request, with the default content type of "application/octect-stream".
-
file
public Request file(java.nio.file.Path file, java.lang.String contentType) throws java.io.IOException
Description copied from interface:Request
Shortcut method to specify a file as a content for this request, with the given content type.
-
isFollowRedirects
public boolean isFollowRedirects()
- Specified by:
isFollowRedirects
in interfaceRequest
- Returns:
- whether this request follows redirects
-
followRedirects
public Request followRedirects(boolean follow)
- Specified by:
followRedirects
in interfaceRequest
- Parameters:
follow
- whether this request follows redirects- Returns:
- this request object
-
getIdleTimeout
public long getIdleTimeout()
- Specified by:
getIdleTimeout
in interfaceRequest
- Returns:
- the idle timeout for this request, in milliseconds
-
idleTimeout
public Request idleTimeout(long timeout, java.util.concurrent.TimeUnit unit)
- Specified by:
idleTimeout
in interfaceRequest
- Parameters:
timeout
- the idle timeout for this requestunit
- the idle timeout unit- Returns:
- this request object
-
getTimeout
public long getTimeout()
- Specified by:
getTimeout
in interfaceRequest
- Returns:
- the total timeout for this request, in milliseconds; zero or negative if the timeout is disabled
-
timeout
public Request timeout(long timeout, java.util.concurrent.TimeUnit unit)
-
send
public ContentResponse send() throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException
Description copied from interface:Request
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
Request.send(Response.CompleteListener)
instead, passing your ownResponse.Listener
or a utility listener such asInputStreamResponseListener
.The method returns when the
complete event
is fired.- Specified by:
send
in interfaceRequest
- 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
public void send(Response.CompleteListener listener)
Description copied from interface:Request
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.
-
getResponseListeners
protected java.util.List<Response.ResponseListener> getResponseListeners()
-
getPushListener
public java.util.function.BiFunction<Request,Request,Response.CompleteListener> getPushListener()
-
getTrailers
public java.util.function.Supplier<HttpFields> getTrailers()
-
abort
public boolean abort(java.lang.Throwable cause)
Description copied from interface:Request
Attempts to abort the send of this request.
-
getAbortCause
public java.lang.Throwable getAbortCause()
- Specified by:
getAbortCause
in interfaceRequest
- Returns:
- the abort cause passed to
Request.abort(Throwable)
, or null if this request has not been aborted
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-