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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classCache validity policy that always returns an age ofTimeValue.ZERO_MILLISECONDS.private static classWriter for SimpleHttpResponse. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intstatic final HttpByteArrayCacheEntrySerializerprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final Stringprivate static final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddMetadataPseudoHeaders(org.apache.hc.core5.http.HttpResponse httpResponse, HttpCacheStorageEntry httpCacheEntry) Modify the given response to add our own cache metadata as pseudo-headers.private static voidcopyBytes(org.apache.hc.core5.http.io.SessionInputBuffer srcBuf, InputStream src, OutputStream dest) Copy bytes from the given source buffer and input stream to the given output stream until end-of-file is reached.deserialize(byte[] serializedObject) Deserializes a cache entry from its serialized representation.private static voidescapeHeaders(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 StringgetCachePseudoHeaderAndRemove(org.apache.hc.core5.http.HttpResponse response, String name) Get the string value for a single metadata pseudo-header, and remove it from the response object.private static booleangetCachePseudoHeaderBooleanAndRemove(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.private static InstantgetCachePseudoHeaderDateAndRemove(org.apache.hc.core5.http.HttpResponse response, String name) Get the date value for a single metadata pseudo-header, and remove it from the response object.private static StringgetOptionalCachePseudoHeaderAndRemove(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.getVariantMapPseudoHeadersAndRemove(org.apache.hc.core5.http.HttpResponse response) Get the variant map metadata pseudo-header, and remove it from the response object.protected InputStreammakeByteArrayInputStream(byte[] bytes) Helper method to make a new ByteArrayInputStream.protected org.apache.hc.core5.http.impl.io.AbstractMessageParser<org.apache.hc.core5.http.ClassicHttpResponse> 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 voidunescapeHeaders(org.apache.hc.core5.http.HttpResponse httpResponse) Modify the given response to remove escaping from any header names we escaped before saving.
-
Field Details
-
INSTANCE
-
SC_CACHE_ENTRY_PREFIX
- See Also:
-
SC_HEADER_NAME_STORAGE_KEY
- See Also:
-
SC_HEADER_NAME_RESPONSE_DATE
- See Also:
-
SC_HEADER_NAME_REQUEST_DATE
- See Also:
-
SC_HEADER_NAME_NO_CONTENT
- See Also:
-
SC_HEADER_NAME_VARIANT_MAP_KEY
- See Also:
-
SC_HEADER_NAME_VARIANT_MAP_VALUE
- See Also:
-
SC_CACHE_ENTRY_PRESERVE_PREFIX
- See Also:
-
BUFFER_SIZE
private static final int BUFFER_SIZE- See Also:
-
-
Constructor Details
-
HttpByteArrayCacheEntrySerializer
public HttpByteArrayCacheEntrySerializer()
-
-
Method Details
-
serialize
Description copied from interface:HttpCacheEntrySerializerSerializes the given entry.- Specified by:
serializein interfaceHttpCacheEntrySerializer<byte[]>- Parameters:
httpCacheEntry- cache entry- Returns:
- serialized representation of the cache entry
- Throws:
ResourceIOException
-
deserialize
Description copied from interface:HttpCacheEntrySerializerDeserializes a cache entry from its serialized representation.- Specified by:
deserializein 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
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 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 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 fromname- 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 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, 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 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 sourcesrc- Unbuffered input sourcedest- Output destination- Throws:
IOException- if an I/O error occurs
-