Class WebResponseCompressingInterceptor

java.lang.Object
io.fluxzero.sdk.web.WebResponseCompressingInterceptor
All Implemented Interfaces:
DispatchInterceptor

public class WebResponseCompressingInterceptor extends Object implements 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:
  • 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:
      modifySerializedMessage in interface DispatchInterceptor
      Parameters:
      response - the serialized response message
      message - the original message
      type - the type of message being dispatched
      topic - 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 the Accept-Encoding header.
      Returns:
      true if GZIP compression is accepted, false otherwise
    • 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 the Content-Encoding used.
      Parameters:
      response - the message to compress
      Returns:
      a new SerializedMessage with 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:
      interceptDispatch in interface DispatchInterceptor
      Parameters:
      message - the message to be dispatched
      messageType - the type of the message
      topic - the dispatch topic
      Returns:
      the unmodified message