Class Request

  • Direct Known Subclasses:
    OAuthRequest

    public class Request
    extends java.lang.Object
    Represents an HTTP Request object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEFAULT_CONTENT_TYPE  
    • Constructor Summary

      Constructors 
      Constructor Description
      Request​(Verb verb, java.lang.String url)
      Creates a new Http Request
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addBodyParameter​(java.lang.String key, java.lang.String value)
      Add a body Parameter (for POST/ PUT Requests)
      void addHeader​(java.lang.String key, java.lang.String value)
      Add an HTTP Header to the Request
      void addPayload​(byte[] payload)
      Overloaded version for byte arrays
      void addPayload​(java.lang.String payload)
      Add body payload.
      void addQuerystringParameter​(java.lang.String key, java.lang.String value)
      Add a QueryString parameter
      java.lang.String getBodyContents()
      Returns the body of the request
      ParameterList getBodyParams()
      Obtains a ParameterList of the body parameters.
      java.lang.String getCharset()
      Returns the connection charset.
      java.lang.String getCompleteUrl()
      Returns the complete url (host + resource + encoded querystring parameters).
      java.util.Map<java.lang.String,​java.lang.String> getHeaders()
      Returns the connection headers as a Map
      ParameterList getQueryStringParams()
      Get a ParameterList with the query string parameters.
      java.lang.String getSanitizedUrl()
      Returns the URL without the port and the query string part.
      java.lang.String getUrl()
      Obtains the URL of the HTTP Request.
      Verb getVerb()
      Returns the HTTP Verb
      Response send()  
      Response send​(RequestTuner tuner)
      Execute the request and return a Response
      void setCharset​(java.lang.String charsetName)
      Set the charset of the body of the request
      void setConnectionKeepAlive​(boolean connectionKeepAlive)
      Sets whether the underlying Http Connection is persistent or not.
      void setConnectTimeout​(int duration, java.util.concurrent.TimeUnit unit)
      Sets the connect timeout for the underlying HttpURLConnection
      void setFollowRedirects​(boolean followRedirects)
      Sets whether the underlying Http Connection follows redirects or not.
      void setReadTimeout​(int duration, java.util.concurrent.TimeUnit unit)
      Sets the read timeout for the underlying HttpURLConnection
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • DEFAULT_CONTENT_TYPE

        public static final java.lang.String DEFAULT_CONTENT_TYPE
        See Also:
        Constant Field Values
    • Constructor Detail

      • Request

        public Request​(Verb verb,
                       java.lang.String url)
        Creates a new Http Request
        Parameters:
        verb - Http Verb (GET, POST, etc)
        url - url with optional querystring parameters.
    • Method Detail

      • send

        public Response send​(RequestTuner tuner)
        Execute the request and return a Response
        Returns:
        Http Response
        Throws:
        java.lang.RuntimeException - if the connection cannot be created.
      • getCompleteUrl

        public java.lang.String getCompleteUrl()
        Returns the complete url (host + resource + encoded querystring parameters).
        Returns:
        the complete url.
      • addHeader

        public void addHeader​(java.lang.String key,
                              java.lang.String value)
        Add an HTTP Header to the Request
        Parameters:
        key - the header name
        value - the header value
      • addBodyParameter

        public void addBodyParameter​(java.lang.String key,
                                     java.lang.String value)
        Add a body Parameter (for POST/ PUT Requests)
        Parameters:
        key - the parameter name
        value - the parameter value
      • addQuerystringParameter

        public void addQuerystringParameter​(java.lang.String key,
                                            java.lang.String value)
        Add a QueryString parameter
        Parameters:
        key - the parameter name
        value - the parameter value
      • addPayload

        public void addPayload​(java.lang.String payload)
        Add body payload. This method is used when the HTTP body is not a form-url-encoded string, but another thing. Like for example XML. Note: The contents are not part of the OAuth signature
        Parameters:
        payload - the body of the request
      • addPayload

        public void addPayload​(byte[] payload)
        Overloaded version for byte arrays
        Parameters:
        payload -
      • getUrl

        public java.lang.String getUrl()
        Obtains the URL of the HTTP Request.
        Returns:
        the original URL of the HTTP Request
      • getSanitizedUrl

        public java.lang.String getSanitizedUrl()
        Returns the URL without the port and the query string part.
        Returns:
        the OAuth-sanitized URL
      • getBodyContents

        public java.lang.String getBodyContents()
        Returns the body of the request
        Returns:
        form encoded string
        Throws:
        OAuthException - if the charset chosen is not supported
      • getVerb

        public Verb getVerb()
        Returns the HTTP Verb
        Returns:
        the verb
      • getHeaders

        public java.util.Map<java.lang.String,​java.lang.String> getHeaders()
        Returns the connection headers as a Map
        Returns:
        map of headers
      • getCharset

        public java.lang.String getCharset()
        Returns the connection charset. Defaults to Charset defaultCharset if not set
        Returns:
        charset
      • setConnectTimeout

        public void setConnectTimeout​(int duration,
                                      java.util.concurrent.TimeUnit unit)
        Sets the connect timeout for the underlying HttpURLConnection
        Parameters:
        duration - duration of the timeout
        unit - unit of time (milliseconds, seconds, etc)
      • setReadTimeout

        public void setReadTimeout​(int duration,
                                   java.util.concurrent.TimeUnit unit)
        Sets the read timeout for the underlying HttpURLConnection
        Parameters:
        duration - duration of the timeout
        unit - unit of time (milliseconds, seconds, etc)
      • setCharset

        public void setCharset​(java.lang.String charsetName)
        Set the charset of the body of the request
        Parameters:
        charsetName - name of the charset of the request
      • setConnectionKeepAlive

        public void setConnectionKeepAlive​(boolean connectionKeepAlive)
        Sets whether the underlying Http Connection is persistent or not.
        Parameters:
        connectionKeepAlive -
      • setFollowRedirects

        public void setFollowRedirects​(boolean followRedirects)
        Sets whether the underlying Http Connection follows redirects or not. Defaults to true (follow redirects)
        Parameters:
        followRedirects -
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object