Class HttpRequest


public class HttpRequest extends HttpBase<HttpRequest>
HTTP request.
  • Field Details

    • protocol

      protected String protocol
    • host

      protected String host
    • port

      protected int port
    • method

      protected String method
    • fragment

      protected String fragment
    • path

      protected String path
    • query

      protected HttpMultiMap<String> query
    • queryEncoding

      protected String queryEncoding
    • trustAllCertificates

      private boolean trustAllCertificates
    • verifyHttpsHost

      private boolean verifyHttpsHost
    • timeout

      protected int timeout
    • connectTimeout

      protected int connectTimeout
    • followRedirects

      protected boolean followRedirects
    • maxRedirects

      protected int maxRedirects
    • httpConnection

      protected HttpConnection httpConnection
    • httpConnectionProvider

      protected HttpConnectionProvider httpConnectionProvider
  • Constructor Details

    • HttpRequest

      public HttpRequest()
  • Method Details

    • initRequest

      protected void initRequest()
      Prepares request on creation. Does the following:
      • adds "Connection: Close" header.
      • adds default headers
    • host

      public String host()
      Returns request host name.
    • host

      public HttpRequest host(String host)
      Sets request host name.
    • protocol

      public String protocol()
      Returns used protocol. By default it's "http".
    • protocol

      public HttpRequest protocol(String protocol)
      Defines protocol.
    • port

      public int port()
      Returns request port number. When port is not explicitly defined, returns default port for current protocol.
    • port

      public HttpRequest port(int port)
      Sets request port number.
    • fragment

      public String fragment()
      Returns HTTP fragment, if specified; otherwise returns null.
    • set

      public HttpRequest set(String destination)
      Sets the destination (method, host, port... ) at once.
    • create

      public static HttpRequest create(String method, String destination)
      Generic request builder, usually used when method is a variable. Otherwise, use one of the other static request builder methods.
    • connect

      public static HttpRequest connect(String destination)
      Builds a CONNECT request.
    • get

      public static HttpRequest get(String destination)
      Builds a GET request.
    • post

      public static HttpRequest post(String destination)
      Builds a POST request.
    • put

      public static HttpRequest put(String destination)
      Builds a PUT request.
    • patch

      public static HttpRequest patch(String destination)
      Builds a PATCH request.
    • delete

      public static HttpRequest delete(String destination)
      Builds a DELETE request.
    • head

      public static HttpRequest head(String destination)
      Builds a HEAD request.
    • trace

      public static HttpRequest trace(String destination)
      Builds a TRACE request.
    • options

      public static HttpRequest options(String destination)
      Builds an OPTIONS request.
    • method

      public String method()
      Returns request method.
    • method

      public HttpRequest method(String method)
      Specifies request method. It will be converted into uppercase. Does not validate if method is one of the HTTP methods.
    • method

      public HttpRequest method(jodd.net.HttpMethod httpMethod)
    • path

      public String path()
      Returns request path, without the query.
    • path

      public HttpRequest path(String path)
      Sets request path. Query string is allowed. Adds a slash if path doesn't start with one. Query will be stripped out from the path. Previous query is discarded.
      See Also:
    • multipart

      public HttpRequest multipart(boolean multipart)
      Forces multipart requests. When set to false, it will be detected if request should be multipart. By setting this to true we are forcing usage of multipart request.
    • cookies

      public HttpRequest cookies(Cookie... cookies)
      Sets cookies to the request.
    • query

      public HttpRequest query(String name, String value)
      Adds query parameter.
    • query

      public HttpRequest query(String name1, Object value1, Object... parameters)
      Adds many query parameters at once. Although it accepts objects, each value will be converted to string.
    • query

      public HttpRequest query(Map<String,String> queryMap)
      Adds all parameters from the provided map.
    • query

      public HttpMultiMap<String> query()
      Returns backend map of query parameters.
    • clearQueries

      public HttpRequest clearQueries()
      Clears all query parameters.
    • queryRemove

      public HttpRequest queryRemove(String name)
      Removes query parameters for given name.
    • queryString

      public HttpRequest queryString(String queryString)
      See Also:
    • queryString

      public HttpRequest queryString(String queryString, boolean decode)
      Sets query from provided query string. Previous query values are discarded.
    • queryString

      public String queryString()
      Generates query string. All values are URL encoded.
    • queryEncoding

      public HttpRequest queryEncoding(String encoding)
      Defines encoding for query parameters.
    • url

      public String url()
      Returns full URL path. Simply concatenates protocol, host, port, path and query string.
    • hostUrl

      public String hostUrl()
      Returns just host url, without path and query.
    • basicAuthentication

      public HttpRequest basicAuthentication(String username, String password)
      Enables basic authentication by adding required header.
    • tokenAuthentication

      public HttpRequest tokenAuthentication(String token)
      Enables token-based authentication.
    • trustAllCerts

      public HttpRequest trustAllCerts(boolean trust)
      Trusts all certificates, use with caution.
    • trustAllCertificates

      public boolean trustAllCertificates()
      Returns a flag if to trusts all certificates.
    • verifyHttpsHost

      public HttpRequest verifyHttpsHost(boolean verifyHttpsHost)
      Verifies HTTPS hosts.
    • verifyHttpsHost

      public boolean verifyHttpsHost()
      Returns a flag if to verify https hosts.
    • setHostHeader

      public HttpRequest setHostHeader()
      Sets 'Host' header from current host and port.
    • monitor

      public HttpRequest monitor(HttpProgressListener httpProgressListener)
      Registers listener that will monitor upload progress. Be aware that the whole size of the request is being monitored, not only the files content.
    • timeout

      public HttpRequest timeout(int milliseconds)
      Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). After establishing the connection, the client socket waits for response after sending the request. This is the elapsed time since the client has sent request to the server before server responds. Please note that this is not same as HTTP Error 408 which the server sends to the client. In other words its maximum period inactivity between two consecutive data packets arriving at client side after connection is established. A timeout value of zero is interpreted as an infinite timeout.
      See Also:
    • timeout

      public int timeout()
      Returns read timeout (SO_TIMEOUT) in milliseconds. Negative value means that default value is used.
      See Also:
    • connectionTimeout

      public HttpRequest connectionTimeout(int milliseconds)
      Defines the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). A timeout value of zero is interpreted as an infinite timeout.
    • connectionTimeout

      public int connectionTimeout()
      Returns socket connection timeout. Negative value means that default value is used.
      See Also:
    • followRedirects

      public HttpRequest followRedirects(boolean followRedirects)
      Defines if redirects responses should be followed. NOTE: when redirection is enabled, the original URL will NOT be preserved in the request!
    • isFollowRedirects

      public boolean isFollowRedirects()
      Returns true if redirects are followed.
    • maxRedirects

      public HttpRequest maxRedirects(int maxRedirects)
      Sets the max number of redirects, used when followRedirects is enabled.
    • maxRedirects

      public int maxRedirects()
      Returns max number of redirects, used when followRedirects is enabled.
    • withConnectionProvider

      public HttpRequest withConnectionProvider(HttpConnectionProvider httpConnectionProvider)
      Uses custom connection provider when opening the connection.
    • connectionProvider

      public HttpConnectionProvider connectionProvider()
      Returns http connection provider that was used for creating current http connection. If null, default connection provider will be used.
    • connection

      public HttpConnection connection()
      Returns HttpConnection that is going to be used for sending this request. Value is available ONLY after calling open() and before send().
    • open

      public HttpRequest open()
      Opens a new connection using either provided or default connection provider.
    • open

      public HttpRequest open(HttpConnectionProvider httpConnectionProvider)
      Opens a new connection using given HttpConnectionProvider.
    • open

      public HttpRequest open(HttpConnection httpConnection)
      Assignees provided HttpConnection for communication. It does not actually opens it until the sending.
    • keepAlive

      public HttpRequest keepAlive(HttpResponse httpResponse, boolean doContinue)
      Continues using the same keep-alive connection. Don't use any variant of open() when continuing the communication! First it checks if "Connection" header exist in the response and if it is equal to "Keep-Alive" value. Then it checks the "Keep-Alive" headers "max" parameter. If its value is positive, then the existing HttpConnection from the request will be reused. If max value is 1, connection will be sent with "Connection: Close" header, indicating its the last request. When new connection is created, the same HttpConnectionProvider that was used for creating initial connection is used for opening the new connection.
      Parameters:
      doContinue - set it to false to indicate the last connection
    • send

      public HttpResponse send()
      Opens connection if not already open, sends request, reads response and closes the request. If keep-alive mode is enabled connection will not be closed.
    • _reset

      private void _reset()
      Resets the request by resetting all additional values added during the sending.
    • _send

      private HttpResponse _send()
    • buffer

      protected Buffer buffer(boolean fullRequest)
      Prepares the request buffer.
      Specified by:
      buffer in class HttpBase<HttpRequest>
    • readFrom

      public static HttpRequest readFrom(InputStream in)
      Parses input stream and creates new HttpRequest object. Assumes input stream is in ISO_8859_1 encoding.
    • readFrom

      public static HttpRequest readFrom(InputStream in, String encoding)
    • contentTypeJson

      public HttpRequest contentTypeJson()
      Specifies JSON content type.
    • acceptJson

      public HttpRequest acceptJson()
      Accepts JSON content type.
    • sendAsync

      public CompletableFuture<HttpResponse> sendAsync()
      Sends http request asynchronously using common fork-join pool. Note that this is not the right non-blocking call (not a NIO), it is just a regular call that is operated in a separate thread.
    • sendAndReceive

      public <R> R sendAndReceive(Function<HttpResponse, R> responseHandler)
      Syntax sugar.
    • sendAndReceive

      public void sendAndReceive(Consumer<HttpResponse> responseHandler)
      Syntax sugar.