Class HttpByteArrayCacheEntrySerializer

java.lang.Object
org.apache.hc.client5.http.impl.cache.HttpByteArrayCacheEntrySerializer
All Implemented Interfaces:
HttpCacheEntrySerializer<byte[]>

@Experimental public class HttpByteArrayCacheEntrySerializer extends 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 Details

  • Constructor Details

    • HttpByteArrayCacheEntrySerializer

      public HttpByteArrayCacheEntrySerializer()
  • Method Details

    • serialize

      public byte[] serialize(HttpCacheStorageEntry httpCacheEntry) throws ResourceIOException
      Description copied from interface: HttpCacheEntrySerializer
      Serializes the given entry.
      Specified by:
      serialize in interface HttpCacheEntrySerializer<byte[]>
      Parameters:
      httpCacheEntry - cache entry
      Returns:
      serialized representation of the cache entry
      Throws:
      ResourceIOException
    • deserialize

      public HttpCacheStorageEntry deserialize(byte[] serializedObject) throws ResourceIOException
      Description copied from interface: HttpCacheEntrySerializer
      Deserializes a cache entry from its serialized representation.
      Specified by:
      deserialize in interface HttpCacheEntrySerializer<byte[]>
      Parameters:
      serializedObject - serialized representation of the cache entry
      Returns:
      cache entry
      Throws:
      ResourceIOException
    • 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 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:
    • 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:
    • 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 String getCachePseudoHeaderAndRemove(org.apache.hc.core5.http.HttpResponse response, 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 String getOptionalCachePseudoHeaderAndRemove(org.apache.hc.core5.http.HttpResponse response, 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 Instant getCachePseudoHeaderDateAndRemove(org.apache.hc.core5.http.HttpResponse response, 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, 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 Map<String,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, InputStream src, OutputStream dest) throws 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:
      IOException - if an I/O error occurs