Package jodd.http

Class HttpRequest

    • Field Detail

      • protocol

        protected java.lang.String protocol
      • host

        protected java.lang.String host
      • port

        protected int port
      • method

        protected java.lang.String method
      • fragment

        protected java.lang.String fragment
      • path

        protected java.lang.String path
      • queryEncoding

        protected java.lang.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
    • Constructor Detail

      • HttpRequest

        public HttpRequest()
    • Method Detail

      • initRequest

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

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

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

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

        public HttpRequest protocol​(java.lang.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 java.lang.String fragment()
        Returns HTTP fragment, if specified; otherwise returns null.
      • set

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

        public static HttpRequest create​(java.lang.String method,
                                         java.lang.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​(java.lang.String destination)
        Builds a CONNECT request.
      • get

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

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

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

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

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

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

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

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

        public java.lang.String method()
        Returns request method.
      • method

        public HttpRequest method​(java.lang.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 java.lang.String path()
        Returns request path, without the query.
      • path

        public HttpRequest path​(java.lang.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:
        query()
      • 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​(java.lang.String name,
                                 java.lang.String value)
        Adds query parameter.
      • query

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

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

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

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

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

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

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

        public HttpRequest queryEncoding​(java.lang.String encoding)
        Defines encoding for query parameters.
      • hostUrl

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

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

        public HttpRequest tokenAuthentication​(java.lang.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:
        HttpConnection.setTimeout(int)
      • timeout

        public int timeout()
        Returns read timeout (SO_TIMEOUT) in milliseconds. Negative value means that default value is used.
        See Also:
        timeout(int)
      • 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:
        connectionTimeout(int)
      • 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.
      • connectionProvider

        public HttpConnectionProvider connectionProvider()
        Returns http connection provider that was used for creating current http connection. If null, default connection provider will be used.
      • 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.
      • readFrom

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

        public static HttpRequest readFrom​(java.io.InputStream in,
                                           java.lang.String encoding)
      • contentTypeJson

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

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

        public java.util.concurrent.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​(java.util.function.Function<HttpResponse,​R> responseHandler)
        Syntax sugar.
      • sendAndReceive

        public void sendAndReceive​(java.util.function.Consumer<HttpResponse> responseHandler)
        Syntax sugar.