Class HttpRequest

java.lang.Object
org.eclipse.jetty.client.HttpRequest
All Implemented Interfaces:
Request
Direct Known Subclasses:
CoreClientUpgradeRequest, HttpProxy.TunnelRequest

public class HttpRequest extends Object implements Request
  • Constructor Details

  • Method Details

    • getConversation

      public HttpConversation getConversation()
    • getScheme

      public String getScheme()
      Specified by:
      getScheme in interface Request
      Returns:
      the URI scheme of this request, such as "http" or "https"
    • scheme

      public Request scheme(String scheme)
      Specified by:
      scheme in interface Request
      Parameters:
      scheme - the URI scheme of this request, such as "http" or "https"
      Returns:
      this request object
    • getHost

      public String getHost()
      Specified by:
      getHost in interface Request
      Returns:
      the URI host of this request, such as "127.0.0.1" or "google.com"
    • host

      public Request host(String host)
      Specified by:
      host in interface Request
      Parameters:
      host - the URI host of this request, such as "127.0.0.1" or "google.com"
      Returns:
      this request object
    • getPort

      public int getPort()
      Specified by:
      getPort in interface Request
      Returns:
      the URI port of this request such as 80 or 443
    • port

      public Request port(int port)
      Specified by:
      port in interface Request
      Parameters:
      port - the URI port of this request such as 80 or 443
      Returns:
      this request object
    • getMethod

      public String getMethod()
      Specified by:
      getMethod in interface Request
      Returns:
      the method of this request, such as GET or POST, as a String
    • method

      public Request method(HttpMethod method)
      Specified by:
      method in interface Request
      Parameters:
      method - the method of this request, such as GET or POST
      Returns:
      this request object
    • method

      public Request method(String method)
      Specified by:
      method in interface Request
      Parameters:
      method - the method of this request, such as GET or POST
      Returns:
      this request object
    • getPath

      public String getPath()
      Specified by:
      getPath in interface Request
      Returns:
      the URI path of this request, such as "/" or "/path" - without the query
      See Also:
    • path

      public Request path(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 properly UTF-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, use Request.param(String, String).
      Specified by:
      path in interface Request
      Parameters:
      path - the URI path of this request, such as "/" or "/path?param=1"
      Returns:
      this request object
    • getQuery

      public String getQuery()
      Specified by:
      getQuery in interface Request
      Returns:
      the URI query string of this request such as "param=1"
      See Also:
    • getURI

      public URI getURI()
      Specified by:
      getURI in interface Request
      Returns:
      the full URI of this request such as "http://host:port/path?param=1"
    • getVersion

      public HttpVersion getVersion()
      Specified by:
      getVersion in interface Request
      Returns:
      the HTTP version of this request, such as "HTTP/1.1"
    • isVersionExplicit

      public boolean isVersionExplicit()
    • version

      public Request version(HttpVersion version)
      Specified by:
      version in interface Request
      Parameters:
      version - the HTTP version of this request, such as "HTTP/1.1"
      Returns:
      this request object
    • param

      public Request param(String name, String value)
      Description copied from interface: Request
      Adds a URI query parameter with the given name and value. The value is UTF-8 URL encoded.
      Specified by:
      param in interface Request
      Parameters:
      name - the name of the query parameter
      value - the value of the query parameter
      Returns:
      this request object
    • getParams

      public Fields getParams()
      Specified by:
      getParams in interface Request
      Returns:
      the URI query parameters of this request
    • getAgent

      public String getAgent()
      Specified by:
      getAgent in interface Request
      Returns:
      the user agent for this request
    • agent

      public Request agent(String agent)
      Specified by:
      agent in interface Request
      Parameters:
      agent - the user agent for this request (corresponds to the User-Agent header)
      Returns:
      this request object
    • accept

      public Request accept(String... accepts)
      Specified by:
      accept in interface Request
      Parameters:
      accepts - the media types that are acceptable in the response, such as "text/plain;q=0.5" or "text/html" (corresponds to the Accept header)
      Returns:
      this request object
    • header

      @Deprecated public Request header(String name, String value)
      Deprecated.
      Specified by:
      header in interface Request
      Parameters:
      name - the name of the header
      value - the value of the header
      Returns:
      this request object
      See Also:
    • header

      @Deprecated public Request header(HttpHeader header, String value)
      Deprecated.
      Description copied from interface: Request

      Adds the given value to the specified header.

      Multiple calls with the same parameters will add multiple values; use the value null to remove the header completely.

      Specified by:
      header in interface Request
      Parameters:
      header - the header name
      value - the value of the header
      Returns:
      this request object
    • getCookies

      public List<HttpCookie> getCookies()
      Specified by:
      getCookies in interface Request
      Returns:
      the cookies associated with this request
    • cookie

      public Request cookie(HttpCookie cookie)
      Specified by:
      cookie in interface Request
      Parameters:
      cookie - a cookie for this request
      Returns:
      this request object
    • tag

      public Request tag(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() and Object.equals(Object) so that it can be used, along with the origin, to identify a destination.

      Specified by:
      tag in interface Request
      Parameters:
      tag - the metadata to tag the request with
      Returns:
      this request object
    • getTag

      public Object getTag()
      Specified by:
      getTag in interface Request
      Returns:
      the metadata this request has been tagged with
    • attribute

      public Request attribute(String name, Object value)
      Specified by:
      attribute in interface Request
      Parameters:
      name - the name of the attribute
      value - the value of the attribute
      Returns:
      this request object
    • getAttributes

      public Map<String,Object> getAttributes()
      Specified by:
      getAttributes in interface Request
      Returns:
      the attributes of this request
    • getHeaders

      public HttpFields getHeaders()
      Specified by:
      getHeaders in interface Request
      Returns:
      the headers of this request
    • headers

      public Request headers(Consumer<HttpFields.Mutable> consumer)
      Description copied from interface: Request
      Modifies the headers of this request.
      Specified by:
      headers in interface Request
      Parameters:
      consumer - the code that modifies the headers of this request
      Returns:
      this request object
    • addHeader

      public HttpRequest addHeader(HttpField header)
    • getRequestListeners

      public <T extends Request.RequestListener> List<T> getRequestListeners(Class<T> type)
      Specified by:
      getRequestListeners in interface Request
      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)
      Specified by:
      listener in interface Request
      Parameters:
      listener - a listener for request events
      Returns:
      this request object
    • onRequestQueued

      public Request onRequestQueued(Request.QueuedListener listener)
      Specified by:
      onRequestQueued in interface Request
      Parameters:
      listener - a listener for request queued event
      Returns:
      this request object
    • onRequestBegin

      public Request onRequestBegin(Request.BeginListener listener)
      Specified by:
      onRequestBegin in interface Request
      Parameters:
      listener - a listener for request begin event
      Returns:
      this request object
    • onRequestHeaders

      public Request onRequestHeaders(Request.HeadersListener listener)
      Specified by:
      onRequestHeaders in interface Request
      Parameters:
      listener - a listener for request headers event
      Returns:
      this request object
    • onRequestCommit

      public Request onRequestCommit(Request.CommitListener listener)
      Specified by:
      onRequestCommit in interface Request
      Parameters:
      listener - a listener for request commit event
      Returns:
      this request object
    • onRequestContent

      public Request onRequestContent(Request.ContentListener listener)
      Specified by:
      onRequestContent in interface Request
      Parameters:
      listener - a listener for request content events
      Returns:
      this request object
    • onRequestSuccess

      public Request onRequestSuccess(Request.SuccessListener listener)
      Specified by:
      onRequestSuccess in interface Request
      Parameters:
      listener - a listener for request success event
      Returns:
      this request object
    • onRequestFailure

      public Request onRequestFailure(Request.FailureListener listener)
      Specified by:
      onRequestFailure in interface Request
      Parameters:
      listener - a listener for request failure event
      Returns:
      this request object
    • onResponseBegin

      public Request onResponseBegin(Response.BeginListener listener)
      Specified by:
      onResponseBegin in interface Request
      Parameters:
      listener - a listener for response begin event
      Returns:
      this request object
    • onResponseHeader

      public Request onResponseHeader(Response.HeaderListener listener)
      Specified by:
      onResponseHeader in interface Request
      Parameters:
      listener - a listener for response header event
      Returns:
      this request object
    • onResponseHeaders

      public Request onResponseHeaders(Response.HeadersListener listener)
      Description copied from interface: Request

      Registers a listener for the headers event.

      Note that the response headers at this event may be different from the headers received in Request.onResponseHeader(Response.HeaderListener) as specified in Response.getHeaders().

      Specified by:
      onResponseHeaders in interface Request
      Parameters:
      listener - a listener for response headers event
      Returns:
      this request object
    • onResponseContent

      public Request onResponseContent(Response.ContentListener listener)
      Specified by:
      onResponseContent in interface Request
      Parameters:
      listener - a consuming listener for response content events
      Returns:
      this request object
    • onResponseContentAsync

      public Request onResponseContentAsync(Response.AsyncContentListener listener)
      Specified by:
      onResponseContentAsync in interface Request
      Parameters:
      listener - an asynchronous listener for response content events
      Returns:
      this request object
    • onResponseContentDemanded

      public Request onResponseContentDemanded(Response.DemandedContentListener listener)
      Specified by:
      onResponseContentDemanded in interface Request
      Parameters:
      listener - an asynchronous listener for response content events
      Returns:
      this request object
    • onResponseSuccess

      public Request onResponseSuccess(Response.SuccessListener listener)
      Specified by:
      onResponseSuccess in interface Request
      Parameters:
      listener - a listener for response success event
      Returns:
      this request object
    • onResponseFailure

      public Request onResponseFailure(Response.FailureListener listener)
      Specified by:
      onResponseFailure in interface Request
      Parameters:
      listener - a listener for response failure event
      Returns:
      this request object
    • onComplete

      public Request onComplete(Response.CompleteListener listener)
      Specified by:
      onComplete in interface Request
      Parameters:
      listener - a listener for complete event
      Returns:
      this request object
    • pushListener

      public Request pushListener(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 the BiFunction are this request and the synthesized request for the pushed resource. The BiFunction should return a CompleteListener that may also implement other listener interfaces to be notified of various response events, or null 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(Supplier<HttpFields> trailers)
    • upgradeProtocol

      public HttpRequest upgradeProtocol(String upgradeProtocol)
    • getContent

      public ContentProvider getContent()
      Specified by:
      getContent in interface Request
      Returns:
      the content provider of this request
    • content

      public Request content(ContentProvider content)
      Specified by:
      content in interface Request
      Parameters:
      content - the content provider of this request
      Returns:
      this request object
    • content

      public Request content(ContentProvider content, String contentType)
      Specified by:
      content in interface Request
      Parameters:
      content - the content provider of this request
      contentType - the content type
      Returns:
      this request object
    • getBody

      public Request.Content getBody()
      Specified by:
      getBody in interface Request
      Returns:
      the request content of this request
    • body

      public Request body(Request.Content content)
      Specified by:
      body in interface Request
      Parameters:
      content - the request content of this request
      Returns:
      this request object
    • file

      public Request file(Path file) throws 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/octet-stream".
      Specified by:
      file in interface Request
      Parameters:
      file - the file to upload
      Returns:
      this request object
      Throws:
      IOException - if the file does not exist or cannot be read
    • file

      public Request file(Path file, String contentType) throws IOException
      Description copied from interface: Request
      Shortcut method to specify a file as a content for this request, with the given content type.
      Specified by:
      file in interface Request
      Parameters:
      file - the file to upload
      contentType - the content type of the file
      Returns:
      this request object
      Throws:
      IOException - if the file does not exist or cannot be read
    • isFollowRedirects

      public boolean isFollowRedirects()
      Specified by:
      isFollowRedirects in interface Request
      Returns:
      whether this request follows redirects
    • followRedirects

      public Request followRedirects(boolean follow)
      Specified by:
      followRedirects in interface Request
      Parameters:
      follow - whether this request follows redirects
      Returns:
      this request object
    • getIdleTimeout

      public long getIdleTimeout()
      Specified by:
      getIdleTimeout in interface Request
      Returns:
      the idle timeout for this request, in milliseconds
    • idleTimeout

      public Request idleTimeout(long timeout, TimeUnit unit)
      Specified by:
      idleTimeout in interface Request
      Parameters:
      timeout - the idle timeout for this request
      unit - the idle timeout unit
      Returns:
      this request object
    • getTimeout

      public long getTimeout()
      Specified by:
      getTimeout in interface Request
      Returns:
      the total timeout for this request, in milliseconds; zero or negative if the timeout is disabled
    • timeout

      public Request timeout(long timeout, TimeUnit unit)
      Specified by:
      timeout in interface Request
      Parameters:
      timeout - the total timeout for the request/response conversation; use zero or a negative value to disable the timeout
      unit - the timeout unit
      Returns:
      this request object
    • send

      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 own Response.Listener or a utility listener such as InputStreamResponseListener.

      The method returns when the complete event is fired.

      Specified by:
      send in interface Request
      Returns:
      a ContentResponse for this request
      Throws:
      InterruptedException - if send thread is interrupted
      TimeoutException - if send times out
      ExecutionException - if execution fails
      See Also:
    • 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.

      Specified by:
      send in interface Request
      Parameters:
      listener - the listener that receives response events
    • getResponseListeners

      protected List<Response.ResponseListener> getResponseListeners()
    • getPushListener

    • getTrailers

      public Supplier<HttpFields> getTrailers()
    • getUpgradeProtocol

      public String getUpgradeProtocol()
    • abort

      public boolean abort(Throwable cause)
      Description copied from interface: Request
      Attempts to abort the send of this request.
      Specified by:
      abort in interface Request
      Parameters:
      cause - the abort cause, must not be null
      Returns:
      whether the abort succeeded
    • getAbortCause

      public Throwable getAbortCause()
      Specified by:
      getAbortCause in interface Request
      Returns:
      the abort cause passed to Request.abort(Throwable), or null if this request has not been aborted
    • toString

      public String toString()
      Overrides:
      toString in class Object