Class HttpBase<T>

java.lang.Object
jodd.http.HttpBase<T>
Direct Known Subclasses:
HttpRequest, HttpResponse

public abstract class HttpBase<T> extends Object
Base class for HttpRequest and HttpResponse.
  • Field Details

  • Constructor Details

    • HttpBase

      public HttpBase()
  • Method Details

    • _this

      protected T _this()
    • httpVersion

      public String httpVersion()
      Returns HTTP version string. By default it's "HTTP/1.1".
    • httpVersion

      public T httpVersion(String httpVersion)
      Sets the HTTP version string. Must be formed like "HTTP/1.1".
    • capitalizeHeaderKeys

      public boolean capitalizeHeaderKeys()
      Returns whether header keys should be strict or not, when they are modified by changing them to PascalCase.
      See Also:
    • capitalizeHeaderKeys

      public T capitalizeHeaderKeys(boolean capitalizeHeaderKeys)
      Sets headers behavior.
      See Also:
    • header

      public String header(String name)
      Returns value of header parameter. If multiple headers with the same names exist, the first value will be returned. Returns null if header doesn't exist.
    • headers

      public List<String> headers(String name)
      Returns all values for given header name.
    • headerRemove

      public void headerRemove(String name)
      Removes all header parameters for given name.
    • headersClear

      public T headersClear()
      Clears all headers.
    • header

      public T header(String name, String value)
      Adds header parameter. If a header with the same name exist, it will not be overwritten, but the new header with the same name is going to be added. The order of header parameters is preserved. Also detects 'Content-Type' header and extracts media type and charset values.
    • header

      public T header(Map<String,String> headerMap)
      Adds many header parameters at once.
      See Also:
    • headerOverwrite

      public T headerOverwrite(String name, String value)
      Sets the header by overwriting it.
      See Also:
    • _header

      protected T _header(String name, String value, boolean overwrite)
      Adds or sets header parameter.
      See Also:
    • _headerRaw

      protected void _headerRaw(String name, String value, boolean overwrite)
      Internal direct header setting.
    • header

      public T header(String name, int value)
      Adds int value as header parameter,
      See Also:
    • header

      public T header(String name, long millis)
      Adds date value as header parameter.
      See Also:
    • headerNames

      public Collection<String> headerNames()
      Returns collection of all header names. Depends on capitalizeHeaderKeys() flag.
    • tokenAuthentication

      public String tokenAuthentication()
      Returns Bearer token or null if not set.
    • charset

      public String charset()
      Returns charset, as defined by 'Content-Type' header. If not set, returns null - indicating the default charset (ISO-8859-1).
    • charset

      public T charset(String charset)
      Defines just content type charset. Setting this value to null will remove the charset information from the header.
    • mediaType

      public String mediaType()
      Returns media type, as defined by 'Content-Type' header. If not set, returns null - indicating the default media type, depending on request/response.
    • mediaType

      public T mediaType(String mediaType)
      Defines just content media type. Setting this value to null will not have any effects.
    • contentType

      public String contentType()
      Returns full "Content-Type" header. It consists of media type and charset.
    • contentType

      public T contentType(String contentType)
      Sets full "Content-Type" header. Both media type and charset are overridden.
    • contentType

      public T contentType(String mediaType, String charset)
      Sets "Content-Type" header by defining media-type and/or charset parameter. This method may be used to update media-type and/or charset by passing non-null value for changes.

      Important: if Content-Type header has some other parameters, they will be removed!

    • connectionKeepAlive

      public T connectionKeepAlive(boolean keepAlive)
      Defines "Connection" header as "Keep-Alive" or "Close". Existing value is overwritten.
    • isConnectionPersistent

      public boolean isConnectionPersistent()
      Returns true if connection is persistent. If "Connection" header does not exist, returns true for HTTP 1.1 and false for HTTP 1.0. If "Connection" header exist, checks if it is equal to "Close".

      In HTTP 1.1, all connections are considered persistent unless declared otherwise. Under HTTP 1.0, there is no official specification for how keepalive operates.

    • contentLength

      public String contentLength()
      Returns full "Content-Length" header or null if not set. Returned value is raw and unchecked, exactly the same as it was specified or received. It may be even invalid.
    • contentLength

      public T contentLength(int value)
      Sets the full "Content-Length" header.
    • contentEncoding

      public String contentEncoding()
      Returns "Content-Encoding" header.
    • accept

      public String accept()
      Returns "Accept" header.
    • accept

      public T accept(String encodings)
      Sets "Accept" header.
    • acceptEncoding

      public String acceptEncoding()
      Returns "Accept-Encoding" header.
    • acceptEncoding

      public T acceptEncoding(String encodings)
      Sets "Accept-Encoding" header.
    • initForm

      protected void initForm()
      Initializes form.
    • wrapFormValue

      protected Object wrapFormValue(Object value)
      Wraps non-Strings form values with uploadable content. Detects invalid types and throws an exception. So all uploadable values are of the same type.
    • form

      public T form(String name, Object value)
      Adds the form parameter. Existing parameter will not be overwritten.
    • formOverwrite

      public T formOverwrite(String name, Object value)
      Sets form parameter by overwriting.
    • form

      public T form(String name, Object value, Object... parameters)
      Sets many form parameters at once.
    • form

      public T form(Map<String,Object> formMap)
      Sets many form parameters at once.
    • form

      public HttpMultiMap<?> form()
      Return map of form parameters. Note that all uploadable values are wrapped with Uploadable.
    • formEncoding

      public T formEncoding(String encoding)
      Defines encoding for forms parameters. Default value is copied from HttpBase.Defaults.formEncoding. It is overridden by charset value.
    • cookies

      public Cookie[] cookies()
      Parses cookie information from the header.
    • bodyRaw

      public String bodyRaw()
      Returns raw body as received or set (always in ISO-8859-1 encoding). If body content is a text, use bodyText() to get it converted. Returns null if body is not specified!
    • bodyBytes

      public byte[] bodyBytes()
      Returns raw body bytes. Returns empty array if body is not specified.
    • bodyText

      public String bodyText()
      Returns body content as text. If charset parameter of "Content-Type" header is defined, body string charset is converted, otherwise the same raw body content is returned. Never returns null.
    • body

      public T body(String body)
      Sets raw body content and discards all form parameters. Important: body string is in RAW format, meaning, ISO-8859-1 encoding. Also sets "Content-Length" parameter. However, "Content-Type" is not set and it is expected from user to set this one.
    • bodyText

      public T bodyText(String body, String mediaType, String charset)
      Defines body text and content type (as media type and charset). Body string will be converted to raw body string and "Content-Type" header will be set.
    • bodyText

      public T bodyText(String body, String mediaType)
      Defines body text content that will be encoded in default body encoding.
    • bodyText

      public T bodyText(String body)
      Defines body text content that will be encoded as default body media type in default body encoding if missing.
    • body

      public T body(byte[] content, String contentType)
      Sets raw body content and discards form parameters. Also sets "Content-Length" and "Content-Type" parameter.
      See Also:
    • isFormMultipart

      protected boolean isFormMultipart()
      Returns true if form contains Uploadable.
    • formBuffer

      protected Buffer formBuffer()
      Creates form buffer and sets few headers.
    • resolveFormEncoding

      protected String resolveFormEncoding()
      Resolves form encodings.
    • toString

      public String toString()
      Returns string representation of this request or response.
      Overrides:
      toString in class Object
    • toString

      public String toString(boolean fullResponse)
      Returns full request/response, or just headers. Useful for debugging.
    • toByteArray

      public byte[] toByteArray()
      Returns byte array of request or response.
    • buffer

      protected abstract Buffer buffer(boolean full)
      Creates buffer ready to be consumed. Buffer can, optionally, contains just headers.
    • populateHeaderAndBody

      protected void populateHeaderAndBody(Buffer target, Buffer formBuffer, boolean fullRequest)
    • sendTo

      public void sendTo(OutputStream out) throws IOException
      Sends request or response to output stream.
      Throws:
      IOException
    • readHeaders

      protected void readHeaders(BufferedReader reader)
      Parses headers.
    • readBody

      protected void readBody(BufferedReader reader)
      Parses body.