Package io.fluxcapacitor.javaclient.web
Class WebResponseCompressingInterceptor
java.lang.Object
io.fluxcapacitor.javaclient.web.WebResponseCompressingInterceptor
- All Implemented Interfaces:
DispatchInterceptor
A
DispatchInterceptor
that applies GZIP compression to outgoing WebResponse
messages based on request
headers and response size.
This interceptor checks whether the originating WebRequest
includes an Accept-Encoding
header with
gzip
. If so, and if the response body exceeds a configurable minimum size threshold, it compresses the
response payload and adds the appropriate Content-Encoding
metadata.
Compression is skipped in the following cases:
- The request was not a
WEBREQUEST
- The request was made over a WebSocket
- The response already includes a
Content-Encoding
header - The response is smaller than the configured minimum byte length
By default, the compression threshold is set to 2048 bytes.
- See Also:
-
Field Summary
Fields inherited from interface io.fluxcapacitor.javaclient.publishing.DispatchInterceptor
noOp
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new interceptor with a default compression threshold of 2048 bytes.WebResponseCompressingInterceptor
(int minimumLength) Creates a new interceptor with a custom compression threshold. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
Checks whether the current request accepts GZIP compression.protected SerializedMessage
compress
(SerializedMessage response) Applies GZIP compression to the response payload and updates the metadata to indicate theContent-Encoding
used.interceptDispatch
(Message message, MessageType messageType, String topic) No-op for message interception.modifySerializedMessage
(SerializedMessage response, Message message, MessageType type, String topic) Compresses the given response message using GZIP if the request supports compression and the response meets compression criteria.protected boolean
shouldCompress
(SerializedMessage response) Determines whether the given serialized message should be compressed based on specific criteria.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.javaclient.publishing.DispatchInterceptor
andThen, monitorDispatch
-
Constructor Details
-
WebResponseCompressingInterceptor
public WebResponseCompressingInterceptor()Creates a new interceptor with a default compression threshold of 2048 bytes. -
WebResponseCompressingInterceptor
public WebResponseCompressingInterceptor(int minimumLength) Creates a new interceptor with a custom compression threshold.- Parameters:
minimumLength
- minimum payload size (in bytes) for compression to apply
-
-
Method Details
-
modifySerializedMessage
public SerializedMessage modifySerializedMessage(SerializedMessage response, Message message, MessageType type, String topic) Compresses the given response message using GZIP if the request supports compression and the response meets compression criteria.- Specified by:
modifySerializedMessage
in interfaceDispatchInterceptor
- Parameters:
response
- the serialized response messagemessage
- the original messagetype
- the type of message being dispatchedtopic
- the dispatch topic- Returns:
- a potentially compressed
SerializedMessage
-
acceptCompression
protected boolean acceptCompression()Checks whether the current request accepts GZIP compression. This is determined by inspecting theAccept-Encoding
header.- Returns:
true
if GZIP compression is accepted,false
otherwise
-
shouldCompress
Determines whether the given serialized message should be compressed based on specific criteria.A message will not be compressed if:
- The "X-Compression" header explicitly disables compression.
- The Content-Type of the message indicates that it is a media type (image, video, audio, or application/octet-stream).
- The "Content-Encoding" header is already present in the message.
- The HTTP status code of the response is not 200.
- The size of the message's payload is below the predefined threshold.
-
compress
Applies GZIP compression to the response payload and updates the metadata to indicate theContent-Encoding
used.- Parameters:
response
- the message to compress- Returns:
- a new
SerializedMessage
with compressed payload and updated headers
-
interceptDispatch
No-op for message interception. This interceptor only modifies serialized messages.- Specified by:
interceptDispatch
in interfaceDispatchInterceptor
- Parameters:
message
- the message to be dispatchedmessageType
- the type of the messagetopic
- the dispatch topic- Returns:
- the unmodified message
-