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
defaultInterceptors, 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 io.fluxzero.common.api.SerializedMessagecompress(io.fluxzero.common.api.SerializedMessage response) Applies GZIP compression to the response payload and updates the metadata to indicate theContent-Encodingused.interceptDispatch(Message message, io.fluxzero.common.MessageType messageType, String topic) No-op for message interception.io.fluxzero.common.api.SerializedMessagemodifySerializedMessage(io.fluxzero.common.api.SerializedMessage response, Message message, io.fluxzero.common.MessageType type, String topic) Compresses the given response message using GZIP if the request supports compression and the response meets compression criteria.protected booleanshouldCompress(io.fluxzero.common.api.SerializedMessage serializedMessage, WebResponse response) Determines whether the given serialized message should be compressed based on specific criteria.
-
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 io.fluxzero.common.api.SerializedMessage modifySerializedMessage(io.fluxzero.common.api.SerializedMessage response, Message message, io.fluxzero.common.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
protected boolean shouldCompress(io.fluxzero.common.api.SerializedMessage serializedMessage, WebResponse response) 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
protected io.fluxzero.common.api.SerializedMessage compress(io.fluxzero.common.api.SerializedMessage response) 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
public Message interceptDispatch(Message message, io.fluxzero.common.MessageType messageType, String topic) 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
-