Class CacheControlHeaderParser


  • @Internal
    @Contract(threading=IMMUTABLE)
    class CacheControlHeaderParser
    extends java.lang.Object
    A parser for the HTTP Cache-Control header that can be used to extract information about caching directives.

    This class is thread-safe and has a singleton instance (INSTANCE).

    The parseResponse(Iterator) method takes an HTTP header and returns a ResponseCacheControl object containing the relevant caching directives. The header can be either a FormattedHeader object, which contains a pre-parsed CharArrayBuffer, or a plain Header object, in which case the value will be parsed and stored in a new CharArrayBuffer.

    This parser only supports two directives: "max-age" and "s-maxage". If either of these directives are present in the header, their values will be parsed and stored in the ResponseCacheControl object. If both directives are present, the value of "s-maxage" takes precedence.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static char EQUAL_CHAR  
      static CacheControlHeaderParser INSTANCE
      The singleton instance of this parser.
      private static org.slf4j.Logger LOG
      The logger for this class.
      private static org.apache.hc.core5.util.Tokenizer.Delimiter TOKEN_DELIMS
      The set of characters that can delimit a token in the header.
      private org.apache.hc.core5.util.Tokenizer tokenParser
      The token parser used to extract values from the header.
      private static org.apache.hc.core5.util.Tokenizer.Delimiter VALUE_DELIMS
      The set of characters that can delimit a value in the header.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected CacheControlHeaderParser()
      Constructs a new instance of this parser.
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
        The logger for this class.
      • TOKEN_DELIMS

        private static final org.apache.hc.core5.util.Tokenizer.Delimiter TOKEN_DELIMS
        The set of characters that can delimit a token in the header.
      • VALUE_DELIMS

        private static final org.apache.hc.core5.util.Tokenizer.Delimiter VALUE_DELIMS
        The set of characters that can delimit a value in the header.
      • tokenParser

        private final org.apache.hc.core5.util.Tokenizer tokenParser
        The token parser used to extract values from the header.
    • Constructor Detail

      • CacheControlHeaderParser

        protected CacheControlHeaderParser()
        Constructs a new instance of this parser.
    • Method Detail

      • parse

        public void parse​(java.util.Iterator<org.apache.hc.core5.http.Header> headerIterator,
                          java.util.function.BiConsumer<java.lang.String,​java.lang.String> consumer)
      • parseResponse

        public final ResponseCacheControl parseResponse​(java.util.Iterator<org.apache.hc.core5.http.Header> headerIterator)
        Parses the specified response header and returns a new ResponseCacheControl instance containing the relevant caching directives.

        The returned ResponseCacheControl instance will contain the values for "max-age" and "s-maxage" caching directives parsed from the input header. If the input header does not contain any caching directives or if the directives are malformed, the returned ResponseCacheControl instance will have default values for "max-age" and "s-maxage" (-1).

        Parameters:
        headerIterator - the header to parse, cannot be null
        Returns:
        a new ResponseCacheControl instance containing the relevant caching directives parsed from the response header
        Throws:
        java.lang.IllegalArgumentException - if the input header is null
      • parse

        public final ResponseCacheControl parse​(org.apache.hc.core5.http.HttpResponse response)
      • parseRequest

        public final RequestCacheControl parseRequest​(java.util.Iterator<org.apache.hc.core5.http.Header> headerIterator)
        Parses the specified request header and returns a new RequestCacheControl instance containing the relevant caching directives.
        Parameters:
        headerIterator - the header to parse, cannot be null
        Returns:
        a new RequestCacheControl instance containing the relevant caching directives parsed from the request header
        Throws:
        java.lang.IllegalArgumentException - if the input header is null
      • parse

        public final RequestCacheControl parse​(org.apache.hc.core5.http.HttpRequest request)
      • parseSeconds

        private static long parseSeconds​(java.lang.String name,
                                         java.lang.String value)