Class HttpByteArrayCacheEntrySerializer

  • All Implemented Interfaces:
    HttpCacheEntrySerializer<byte[]>

    @Experimental
    public class HttpByteArrayCacheEntrySerializer
    extends java.lang.Object
    implements HttpCacheEntrySerializer<byte[]>
    Cache serializer and deserializer that uses an HTTP-like format. Existing libraries for reading and writing HTTP are used, and metadata is encoded into HTTP pseudo-headers for storage.
    • Field Detail

      • SC_CACHE_ENTRY_PREFIX

        private static final java.lang.String SC_CACHE_ENTRY_PREFIX
        See Also:
        Constant Field Values
      • SC_HEADER_NAME_STORAGE_KEY

        private static final java.lang.String SC_HEADER_NAME_STORAGE_KEY
        See Also:
        Constant Field Values
      • SC_HEADER_NAME_RESPONSE_DATE

        private static final java.lang.String SC_HEADER_NAME_RESPONSE_DATE
        See Also:
        Constant Field Values
      • SC_HEADER_NAME_REQUEST_DATE

        private static final java.lang.String SC_HEADER_NAME_REQUEST_DATE
        See Also:
        Constant Field Values
      • SC_HEADER_NAME_NO_CONTENT

        private static final java.lang.String SC_HEADER_NAME_NO_CONTENT
        See Also:
        Constant Field Values
      • SC_HEADER_NAME_VARIANT_MAP_KEY

        private static final java.lang.String SC_HEADER_NAME_VARIANT_MAP_KEY
        See Also:
        Constant Field Values
      • SC_HEADER_NAME_VARIANT_MAP_VALUE

        private static final java.lang.String SC_HEADER_NAME_VARIANT_MAP_VALUE
        See Also:
        Constant Field Values
      • SC_CACHE_ENTRY_PRESERVE_PREFIX

        private static final java.lang.String SC_CACHE_ENTRY_PRESERVE_PREFIX
        See Also:
        Constant Field Values
    • Constructor Detail

      • HttpByteArrayCacheEntrySerializer

        public HttpByteArrayCacheEntrySerializer()
    • Method Detail

      • makeHttpResponseWriter

        protected org.apache.hc.core5.http.impl.io.AbstractMessageWriter<SimpleHttpResponse> makeHttpResponseWriter​(org.apache.hc.core5.http.io.SessionOutputBuffer outputBuffer)
        Helper method to make a new HTTP response writer.

        Useful to override for testing.

        Parameters:
        outputBuffer - Output buffer to write to
        Returns:
        HTTP response writer to write to
      • makeByteArrayInputStream

        protected java.io.InputStream makeByteArrayInputStream​(byte[] bytes)
        Helper method to make a new ByteArrayInputStream.

        Useful to override for testing.

        Parameters:
        bytes - Bytes to read from the stream
        Returns:
        Stream to read the bytes from
      • makeHttpResponseParser

        protected org.apache.hc.core5.http.impl.io.AbstractMessageParser<org.apache.hc.core5.http.ClassicHttpResponse> makeHttpResponseParser()
        Helper method to make a new HTTP Response parser.

        Useful to override for testing.

        Returns:
        HTTP response parser
      • escapeHeaders

        private static void escapeHeaders​(org.apache.hc.core5.http.HttpResponse httpResponse)
        Modify the given response to escape any header names that start with the prefix we use for our own pseudo-headers, prefixing them with an escape sequence we can use to recover them later.
        Parameters:
        httpResponse - HTTP response object to escape headers in
        See Also:
        for the corresponding un-escaper.
      • unescapeHeaders

        private void unescapeHeaders​(org.apache.hc.core5.http.HttpResponse httpResponse)
        Modify the given response to remove escaping from any header names we escaped before saving.
        Parameters:
        httpResponse - HTTP response object to un-escape headers in
        See Also:
        for the corresponding escaper
      • addMetadataPseudoHeaders

        private void addMetadataPseudoHeaders​(org.apache.hc.core5.http.HttpResponse httpResponse,
                                              HttpCacheStorageEntry httpCacheEntry)
        Modify the given response to add our own cache metadata as pseudo-headers.
        Parameters:
        httpResponse - HTTP response object to add pseudo-headers to
      • getCachePseudoHeaderAndRemove

        private static java.lang.String getCachePseudoHeaderAndRemove​(org.apache.hc.core5.http.HttpResponse response,
                                                                      java.lang.String name)
                                                               throws ResourceIOException
        Get the string value for a single metadata pseudo-header, and remove it from the response object.
        Parameters:
        response - Response object to get and remove the pseudo-header from
        name - Name of metadata pseudo-header
        Returns:
        Value for metadata pseudo-header
        Throws:
        ResourceIOException - if the given pseudo-header is not found
      • getOptionalCachePseudoHeaderAndRemove

        private static java.lang.String getOptionalCachePseudoHeaderAndRemove​(org.apache.hc.core5.http.HttpResponse response,
                                                                              java.lang.String name)
        Get the string value for a single metadata pseudo-header if it exists, and remove it from the response object.
        Parameters:
        response - Response object to get and remove the pseudo-header from
        name - Name of metadata pseudo-header
        Returns:
        Value for metadata pseudo-header, or null if it does not exist
      • getCachePseudoHeaderDateAndRemove

        private static java.time.Instant getCachePseudoHeaderDateAndRemove​(org.apache.hc.core5.http.HttpResponse response,
                                                                           java.lang.String name)
                                                                    throws ResourceIOException
        Get the date value for a single metadata pseudo-header, and remove it from the response object.
        Parameters:
        response - Response object to get and remove the pseudo-header from
        name - Name of metadata pseudo-header
        Returns:
        Value for metadata pseudo-header
        Throws:
        ResourceIOException - if the given pseudo-header is not found, or contains invalid data
      • getCachePseudoHeaderBooleanAndRemove

        private static boolean getCachePseudoHeaderBooleanAndRemove​(org.apache.hc.core5.http.ClassicHttpResponse response,
                                                                    java.lang.String name)
        Get the boolean value for a single metadata pseudo-header, and remove it from the response object.
        Parameters:
        response - Response object to get and remove the pseudo-header from
        name - Name of metadata pseudo-header
        Returns:
        Value for metadata pseudo-header
      • getVariantMapPseudoHeadersAndRemove

        private static java.util.Map<java.lang.String,​java.lang.String> getVariantMapPseudoHeadersAndRemove​(org.apache.hc.core5.http.HttpResponse response)
                                                                                                           throws ResourceIOException
        Get the variant map metadata pseudo-header, and remove it from the response object.
        Parameters:
        response - Response object to get and remove the pseudo-header from
        Returns:
        Extracted variant map
        Throws:
        ResourceIOException - if the given pseudo-header is not found, or contains invalid data
      • copyBytes

        private static void copyBytes​(org.apache.hc.core5.http.io.SessionInputBuffer srcBuf,
                                      java.io.InputStream src,
                                      java.io.OutputStream dest)
                               throws java.io.IOException
        Copy bytes from the given source buffer and input stream to the given output stream until end-of-file is reached.
        Parameters:
        srcBuf - Buffered input source
        src - Unbuffered input source
        dest - Output destination
        Throws:
        java.io.IOException - if an I/O error occurs