Interface RequestInterceptor

All Known Implementing Classes:
CorrelationIdInterceptor, HeaderInterceptor, LoggingInterceptor, MetricsInterceptor, RedactingLoggingInterceptor

public interface RequestInterceptor
Global hook into every request/response made through RIP, registered via RIP.addInterceptor(RequestInterceptor).

Both methods are observers, not a retry pipeline: beforeRequest can add headers (or abort the call by throwing) before it goes out, and afterResponse is notified once the response is back, but neither can cause the request to be re-sent. A retry policy needs a different mechanism than a passive interceptor and isn't supported here.

When several interceptors are registered, they run "onion"-style: beforeRequest runs in registration order, but afterResponse runs in the reverse order, so the first interceptor registered wraps every other one and is the last to see the response - the same pairing used by OkHttp, Servlet filters, and most middleware chains. Register an interceptor first if it needs to bracket everything else's work (e.g. a timer measuring total call overhead); register it last if it needs to sit closest to the actual network call (e.g. a timer measuring only network latency).

  • Method Details

    • beforeRequest

      default void beforeRequest(RequestContext context)
      Called before the request is sent. Can add headers via RequestContext.addHeader(java.lang.String, java.lang.String), or abort the call by throwing.
      Parameters:
      context - the request being made
    • shortCircuit

      default ShortCircuitResponse shortCircuit(RequestContext context)
      Called after every registered interceptor's beforeRequest has run (in the same FIFO registration order as beforeRequest itself) - returning a non-null ShortCircuitResponse skips the network call entirely and uses it as this call's response instead, decoded exactly as a real one would be (including throwing RestInPeaceHttpException for a non-2xx status). The first interceptor (in that same FIFO order) to return non-null wins; every interceptor's afterResponse still runs afterward, exactly as it would for a real response. Enables a feature-flag bypass, a canary short-circuit, or a lightweight record/replay mode built on the interceptor chain instead of a real network dependency.

      A short-circuited response still goes through this client's own caching/retry configuration exactly like a real one would (e.g. it may get cached if it carries cacheable headers, or retried if its status matches @Retry#retryOnStatus() - the latter simply re-invokes this method again instead of a real network call, so it's harmless if wasteful).

      Parameters:
      context - the request that would otherwise be sent
      Returns:
      a synthetic response to short-circuit with, or null to let the request proceed normally
    • afterResponse

      default void afterResponse(RequestContext context, int status, Object body)
      Called once the response is back.
      Parameters:
      context - the request that was made
      status - the HTTP response status code
      body - the response body - a String, a deserialized object, or null for a void-returning method