Interface RequestInterceptor


public interface RequestInterceptor
Hook methods that can adjust Soklet's request processing flow.

A standard threadsafe implementation can be acquired via defaultInstance().

Author:
Mark Allen
  • Method Details

    • wrapRequest

      default void wrapRequest(@NonNull ServerType serverType, @NonNull Request request, @NonNull Consumer<Request> requestProcessor)
      Called before Soklet begins request processing, allowing the request to be wrapped or replaced.

      Routing happens after this callback, so changes to the HTTP method or path affect which Resource Method is selected.

      You must call requestProcessor.accept(...) exactly once before returning to advance processing. If you do not, Soklet logs the error and returns a 500 response.

      This method is not fail-fast. If an exception occurs when Soklet invokes this method, Soklet will catch it and surface separately via LifecycleObserver.didReceiveLogEvent(LogEvent) with type LogEventType.REQUEST_INTERCEPTOR_WRAP_REQUEST_FAILED.

      Parameters:
      serverType - the server type that received the request
      request - the request that was received
      requestProcessor - receives the request to use for subsequent processing
    • interceptRequest

      default void interceptRequest(@NonNull ServerType serverType, @NonNull Request request, @Nullable ResourceMethod resourceMethod, @NonNull Function<Request, MarshaledResponse> responseGenerator, @NonNull Consumer<MarshaledResponse> responseWriter)
      Intercepts request processing, allowing the request to be replaced and/or the response to be transformed.

      This method is not fail-fast. If an exception occurs when Soklet invokes this method, Soklet will catch it and surface separately via LifecycleObserver.didReceiveLogEvent(LogEvent) with type LogEventType.REQUEST_INTERCEPTOR_INTERCEPT_REQUEST_FAILED.

      You must call responseWriter.accept(...) exactly once before returning to send a response. If you do not, Soklet logs the error and returns a 500 response.

      This contract is synchronous: perform all request processing — including the responseWriter.accept(...) call — on the thread that invoked this method, before returning. Do not offload the responseGenerator or responseWriter invocations to another thread: Soklet's request lifecycle bookkeeping (for example, metrics correlation and timeout handling) is thread-affine to the request-handler thread.

      Parameters:
      serverType - the server type that received the request
      request - the request that was received
      resourceMethod - the Resource Method that will handle the request
      responseGenerator - function that performs standard request handling and returns a response
      responseWriter - receives the response to send to the client
    • defaultInstance

      Acquires a threadsafe RequestInterceptor instance with sensible defaults.

      The returned instance is guaranteed to be a JVM-wide singleton.

      Returns:
      a RequestInterceptor with default settings