Class HttpByteArrayCacheEntrySerializer
- java.lang.Object
-
- org.apache.hc.client5.http.impl.cache.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
HttpByteArrayCacheEntrySerializer.NoAgeCacheValidityPolicy
Cache validity policy that always returns an age ofTimeValue.ZERO_MILLISECONDS
.private static class
HttpByteArrayCacheEntrySerializer.SimpleHttpResponseWriter
Writer for SimpleHttpResponse.
-
Field Summary
Fields Modifier and Type Field Description private static int
BUFFER_SIZE
static HttpByteArrayCacheEntrySerializer
INSTANCE
private static java.lang.String
SC_CACHE_ENTRY_PREFIX
private static java.lang.String
SC_CACHE_ENTRY_PRESERVE_PREFIX
private static java.lang.String
SC_HEADER_NAME_NO_CONTENT
private static java.lang.String
SC_HEADER_NAME_REQUEST_DATE
private static java.lang.String
SC_HEADER_NAME_RESPONSE_DATE
private static java.lang.String
SC_HEADER_NAME_STORAGE_KEY
private static java.lang.String
SC_HEADER_NAME_VARIANT_MAP_KEY
private static java.lang.String
SC_HEADER_NAME_VARIANT_MAP_VALUE
-
Constructor Summary
Constructors Constructor Description HttpByteArrayCacheEntrySerializer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.private static void
copyBytes(org.apache.hc.core5.http.io.SessionInputBuffer srcBuf, java.io.InputStream src, java.io.OutputStream dest)
Copy bytes from the given source buffer and input stream to the given output stream until end-of-file is reached.HttpCacheStorageEntry
deserialize(byte[] serializedObject)
Deserializes a cache entry from its serialized representation.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.private static java.lang.String
getCachePseudoHeaderAndRemove(org.apache.hc.core5.http.HttpResponse response, java.lang.String name)
Get the string value for a single metadata pseudo-header, and remove it from the response object.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.private static java.time.Instant
getCachePseudoHeaderDateAndRemove(org.apache.hc.core5.http.HttpResponse response, java.lang.String name)
Get the date value for a single metadata pseudo-header, and remove it from the response object.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.private static java.util.Map<java.lang.String,java.lang.String>
getVariantMapPseudoHeadersAndRemove(org.apache.hc.core5.http.HttpResponse response)
Get the variant map metadata pseudo-header, and remove it from the response object.protected java.io.InputStream
makeByteArrayInputStream(byte[] bytes)
Helper method to make a new ByteArrayInputStream.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.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.byte[]
serialize(HttpCacheStorageEntry httpCacheEntry)
Serializes the given entry.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.
-
-
-
Field Detail
-
INSTANCE
public static final HttpByteArrayCacheEntrySerializer INSTANCE
-
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
-
BUFFER_SIZE
private static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
serialize
public byte[] serialize(HttpCacheStorageEntry httpCacheEntry) throws ResourceIOException
Description copied from interface:HttpCacheEntrySerializer
Serializes the given entry.- Specified by:
serialize
in interfaceHttpCacheEntrySerializer<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 interfaceHttpCacheEntrySerializer<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 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 fromname
- 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 fromname
- 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 fromname
- 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 fromname
- 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 sourcesrc
- Unbuffered input sourcedest
- Output destination- Throws:
java.io.IOException
- if an I/O error occurs
-
-