Class HttpHeaders

All Implemented Interfaces:
Cloneable, Map<String,Object>

public class HttpHeaders extends GenericData
Stores HTTP headers used in an HTTP request or response, as defined in Header Field Definitions.

null is not allowed as a name or value of a header. Names are case-insensitive.

Implementation is not thread-safe.

Since:
1.0
Author:
Yaniv Inbar
  • Constructor Details

    • HttpHeaders

      public HttpHeaders()
  • Method Details

    • clone

      public HttpHeaders clone()
      Description copied from class: GenericData
      Makes a "deep" clone of the generic data, in which the clone is completely independent of the original.
      Overrides:
      clone in class GenericData
    • set

      public HttpHeaders set(String fieldName, Object value)
      Description copied from class: GenericData
      Sets the given field value (may be null) for the given field name. Any existing value for the field will be overwritten. It may be more slightly more efficient than GenericData.put(String, Object) because it avoids accessing the field's original value.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Overrides:
      set in class GenericData
    • getAccept

      public final String getAccept()
      Returns the first "Accept" header or null for none.
      Since:
      1.5
    • setAccept

      public HttpHeaders setAccept(String accept)
      Sets the "Accept" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getAcceptEncoding

      public final String getAcceptEncoding()
      Returns the first "Accept-Encoding" header or null for none.
      Since:
      1.5
    • setAcceptEncoding

      public HttpHeaders setAcceptEncoding(String acceptEncoding)
      Sets the "Accept-Encoding" header or null for none.

      By default, this is "gzip".

      Since:
      1.5
    • getAuthorization

      public final String getAuthorization()
      Returns the first "Authorization" header or null for none.
      Since:
      1.5
    • getAuthorizationAsList

      public final List<String> getAuthorizationAsList()
      Returns all "Authorization" headers or null for none.
      Since:
      1.13
    • setAuthorization

      public HttpHeaders setAuthorization(String authorization)
      Sets the "Authorization" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • setAuthorization

      public HttpHeaders setAuthorization(List<String> authorization)
      Sets the "Authorization" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.13
    • getCacheControl

      public final String getCacheControl()
      Returns the first "Cache-Control" header or null for none.
      Since:
      1.5
    • setCacheControl

      public HttpHeaders setCacheControl(String cacheControl)
      Sets the "Cache-Control" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getContentEncoding

      public final String getContentEncoding()
      Returns the first "Content-Encoding" header or null for none.
      Since:
      1.5
    • setContentEncoding

      public HttpHeaders setContentEncoding(String contentEncoding)
      Sets the "Content-Encoding" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getContentLength

      public final Long getContentLength()
      Returns the first "Content-Length" header or null for none.
      Since:
      1.5
    • setContentLength

      public HttpHeaders setContentLength(Long contentLength)
      Sets the "Content-Length" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getContentMD5

      public final String getContentMD5()
      Returns the first "Content-MD5" header or null for none.
      Since:
      1.5
    • setContentMD5

      public HttpHeaders setContentMD5(String contentMD5)
      Sets the "Content-MD5" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getContentRange

      public final String getContentRange()
      Returns the first "Content-Range" header or null for none.
      Since:
      1.5
    • setContentRange

      public HttpHeaders setContentRange(String contentRange)
      Sets the "Content-Range" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getContentType

      public final String getContentType()
      Returns the first "Content-Type" header or null for none.
      Since:
      1.5
    • setContentType

      public HttpHeaders setContentType(String contentType)
      Sets the "Content-Type" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getCookie

      public final String getCookie()
      Returns the first "Cookie" header or null for none.

      See Cookie Specification.

      Since:
      1.6
    • setCookie

      public HttpHeaders setCookie(String cookie)
      Sets the "Cookie" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.6
    • getDate

      public final String getDate()
      Returns the first "Date" header or null for none.
      Since:
      1.5
    • setDate

      public HttpHeaders setDate(String date)
      Sets the "Date" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getETag

      public final String getETag()
      Returns the first "ETag" header or null for none.
      Since:
      1.5
    • setETag

      public HttpHeaders setETag(String etag)
      Sets the "ETag" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getExpires

      public final String getExpires()
      Returns the first "Expires" header or null for none.
      Since:
      1.5
    • setExpires

      public HttpHeaders setExpires(String expires)
      Sets the "Expires" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getIfModifiedSince

      public final String getIfModifiedSince()
      Returns the first "If-Modified-Since" header or null for none.
      Since:
      1.5
    • setIfModifiedSince

      public HttpHeaders setIfModifiedSince(String ifModifiedSince)
      Sets the "If-Modified-Since" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getIfMatch

      public final String getIfMatch()
      Returns the first "If-Match" header or null for none.
      Since:
      1.5
    • setIfMatch

      public HttpHeaders setIfMatch(String ifMatch)
      Sets the "If-Match" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getIfNoneMatch

      public final String getIfNoneMatch()
      Returns the first "If-None-Match" header or null for none.
      Since:
      1.5
    • setIfNoneMatch

      public HttpHeaders setIfNoneMatch(String ifNoneMatch)
      Sets the "If-None-Match" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getIfUnmodifiedSince

      public final String getIfUnmodifiedSince()
      Returns the first "If-Unmodified-Since" header or null for none.
      Since:
      1.5
    • setIfUnmodifiedSince

      public HttpHeaders setIfUnmodifiedSince(String ifUnmodifiedSince)
      Sets the "If-Unmodified-Since" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getIfRange

      public final String getIfRange()
      Returns the first "If-Range" header or null for none.
      Since:
      1.14
    • setIfRange

      public HttpHeaders setIfRange(String ifRange)
      Sets the "If-Range" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.14
    • getLastModified

      public final String getLastModified()
      Returns the first "Last-Modified" header or null for none.
      Since:
      1.5
    • setLastModified

      public HttpHeaders setLastModified(String lastModified)
      Sets the "Last-Modified" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getLocation

      public final String getLocation()
      Returns the first "Location" header or null for none.
      Since:
      1.5
    • setLocation

      public HttpHeaders setLocation(String location)
      Sets the "Location" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getMimeVersion

      public final String getMimeVersion()
      Returns the first "MIME-Version" header or null for none.
      Since:
      1.5
    • setMimeVersion

      public HttpHeaders setMimeVersion(String mimeVersion)
      Sets the "MIME-Version" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getRange

      public final String getRange()
      Returns the first "Range" header or null for none.
      Since:
      1.5
    • setRange

      public HttpHeaders setRange(String range)
      Sets the "Range" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getRetryAfter

      public final String getRetryAfter()
      Returns the first "Retry-After" header or null for none.
      Since:
      1.5
    • setRetryAfter

      public HttpHeaders setRetryAfter(String retryAfter)
      Sets the "Retry-After" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getUserAgent

      public final String getUserAgent()
      Returns the first "User-Agent" header or null for none.
      Since:
      1.5
    • setUserAgent

      public HttpHeaders setUserAgent(String userAgent)
      Sets the "User-Agent" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • getAuthenticate

      public final String getAuthenticate()
      Returns the first "WWW-Authenticate" header or null for none.
      Since:
      1.5
    • getAuthenticateAsList

      public final List<String> getAuthenticateAsList()
      Returns all "WWW-Authenticate" headers or null for none.
      Since:
      1.16
    • setAuthenticate

      public HttpHeaders setAuthenticate(String authenticate)
      Sets the "WWW-Authenticate" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.5
    • addWarning

      public HttpHeaders addWarning(String warning)
      Adds the "Warning" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.28
    • getWarning

      public final List<String> getWarning()
      Returns all "Warning" headers or null for none.
      Since:
      1.28
    • getAge

      public final Long getAge()
      Returns the first "Age" header or null for none.
      Since:
      1.14
    • setAge

      public HttpHeaders setAge(Long age)
      Sets the "Age" header or null for none.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.14
    • setBasicAuthentication

      public HttpHeaders setBasicAuthentication(String username, String password)
      Sets the authorization header as specified in Basic Authentication Scheme.

      Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

      Since:
      1.2
    • serializeHeadersForMultipartRequests

      public static void serializeHeadersForMultipartRequests(HttpHeaders headers, StringBuilder logbuf, Logger logger, Writer writer) throws IOException
      Serializes headers to an Writer for Multi-part requests.
      Parameters:
      headers - HTTP headers
      logbuf - log buffer or null for none
      logger - logger or null for none. Logger must be specified if log buffer is specified
      writer - Writer where HTTP headers will be serialized to or null for none
      Throws:
      IOException
      Since:
      1.9
    • fromHttpResponse

      public final void fromHttpResponse(LowLevelHttpResponse response, StringBuilder logger) throws IOException
      Puts all headers of the LowLevelHttpResponse into this HttpHeaders object.
      Parameters:
      response - Response from which the headers are copied
      logger - StringBuilder to which logging output is added or null to disable logging
      Throws:
      IOException
      Since:
      1.10
    • getFirstHeaderStringValue

      public String getFirstHeaderStringValue(String name)
      Returns the first header string value for the given header name.
      Parameters:
      name - header name (may be any case)
      Returns:
      first header string value or null if not found
      Since:
      1.13
    • getHeaderStringValues

      public List<String> getHeaderStringValues(String name)
      Returns an unmodifiable list of the header string values for the given header name.
      Parameters:
      name - header name (may be any case)
      Returns:
      header string values or empty if not found
      Since:
      1.13
    • fromHttpHeaders

      public final void fromHttpHeaders(HttpHeaders headers)
      Puts all headers of the HttpHeaders object into this HttpHeaders object.
      Parameters:
      headers - HttpHeaders from where the headers are taken
      Since:
      1.10