Class WebResponseCompressingInterceptor
java.lang.Object
io.fluxzero.sdk.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-Encodingheader - 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 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 booleanChecks whether the current request accepts GZIP compression.protected SerializedMessagecompress(SerializedMessage response) Applies GZIP compression to the response payload and updates the metadata to indicate theContent-Encodingused.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 booleanshouldCompress(SerializedMessage serializedMessage, WebResponse response) Determines whether the given serialized message should be compressed based on specific criteria.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface 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:
modifySerializedMessagein 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-Encodingheader.- Returns:
trueif GZIP compression is accepted,falseotherwise
-
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-Encodingused.- Parameters:
response- the message to compress- Returns:
- a new
SerializedMessagewith compressed payload and updated headers
-
interceptDispatch
No-op for message interception. This interceptor only modifies serialized messages.- Specified by:
interceptDispatchin interfaceDispatchInterceptor- Parameters:
message- the message to be dispatchedmessageType- the type of the messagetopic- the dispatch topic- Returns:
- the unmodified message
-