Interface ProxyListener


  • public interface ProxyListener
    Hooks to intercept, change and observe the proxying of requests from a client to a target and back.

    Register listeners when constructor the router with the CrankerRouterBuilder.withProxyListeners(List) method.

    Note: the default implementation of each method is a no-op operation, so you can just override the events you are interested in.

    • Method Detail

      • onBeforeProxyToTarget

        default void onBeforeProxyToTarget​(ProxyInfo info,
                                           io.muserver.Headers requestHeadersToTarget)
                                    throws javax.ws.rs.WebApplicationException
        This is called before sending a request to the target service
        Parameters:
        info - Info about the request and response. Note that duration, bytes sent and bytes received will contain values as the the current point in time.
        requestHeadersToTarget - The headers that will be sent to the client. Modify this object in order to change the headers sent to the target server.
        Throws:
        javax.ws.rs.WebApplicationException - Throw a web application exception to send an error to the client rather than proxying the request.
      • onBeforeRespondingToClient

        default void onBeforeRespondingToClient​(ProxyInfo info)
                                         throws javax.ws.rs.WebApplicationException

        This is called before sending the response to the client.

        The response info contains the response objects with the HTTP status and headers that will be sent to the client. You are able to change these values at this point in the lifecycle.

        Parameters:
        info - Info about the request and response. Note that duration, bytes sent and bytes received will contain values as the the current point in time.
        Throws:
        javax.ws.rs.WebApplicationException - Throw a web application exception to send an error to the client rather than proxying the response. This allows you reject responses from services that you deem to be invalid.
      • onComplete

        default void onComplete​(ProxyInfo proxyInfo)
        This is called after a response has been completed.

        Note that this is called even if the response was not completed successfully (for example if a browser was closed before a response was complete). If the proxying was not successful, then ProxyInfo.errorIfAny() will not be null.

        Parameters:
        proxyInfo - Information about the response.
      • onAfterProxyToTargetHeadersSent

        default void onAfterProxyToTargetHeadersSent​(ProxyInfo info,
                                                     io.muserver.Headers headers)
                                              throws javax.ws.rs.WebApplicationException
        This is called if async method which used to send request headers has already called. Because the operation of send header is async so this callback does not mean headers has sent complete
        Parameters:
        info - Info about the request and response.
        headers - The headers that has sent to the target
        Throws:
        javax.ws.rs.WebApplicationException - Throw a web application exception to send an error to the client rather than proxying the response. At this stage, the header maybe already sent to target but because this exception has been thrown so will stop send or receive data from target
      • onAfterTargetToProxyHeadersReceived

        default void onAfterTargetToProxyHeadersReceived​(ProxyInfo info,
                                                         int status,
                                                         io.muserver.Headers headers)
                                                  throws javax.ws.rs.WebApplicationException
        This is called if response headers has already received from target
        Parameters:
        info - Info about the request and response.
        status - Response status
        headers - The headers that has sent to the target
        Throws:
        javax.ws.rs.WebApplicationException - Throw a web application exception to send an error to the client rather than keep getting data from target.
      • onRequestBodyChunkSentToTarget

        default void onRequestBodyChunkSentToTarget​(ProxyInfo info,
                                                    ByteBuffer chunk)
        Called when a chunk of request body data is sent to the target This will be called many times if the body has been fragmented
        Parameters:
        info - Info about the request and response.
        chunk - Request body data which already been sent to target successfully.
      • onRequestBodySentToTarget

        default void onRequestBodySentToTarget​(ProxyInfo info)
        Called when the full request body has been received to the target
        Parameters:
        info - Info about the request and response.
      • onResponseBodyChunkReceivedFromTarget

        default void onResponseBodyChunkReceivedFromTarget​(ProxyInfo info,
                                                           ByteBuffer chunk)
        Called when a chunk of response body data is received from the target This will be called many times if the body has been fragmented
        Parameters:
        info - Info about the request and response.
        chunk - Response body data received from the target.
      • onResponseBodyChunkReceived

        default void onResponseBodyChunkReceived​(ProxyInfo info)
        Called when the full response body has been received from the target
        Parameters:
        info - Info about the request and response. proxying the request.