Class RequestContext

java.lang.Object
com.shri.restinpeace.interceptor.RequestContext

public final class RequestContext extends Object
Per-call state passed to a RequestInterceptor's beforeRequest/afterResponse, sharing one instance across both so an interceptor can correlate state between them (e.g. a start timestamp for measuring request duration, via setAttribute(java.lang.String, java.lang.Object)).
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    How much extra diagnostic detail toCurlCommand(CurlVerbosity) asks curl itself to print, via curl's own flags - RIP doesn't invent its own verbosity scheme, it just picks which standard curl flag(s) to include.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RequestContext(HTTPMethod httpMethod, String url)
    Creates the context for a single call.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addHeader(String name, String value)
    Adds a header to be sent with the request.
    Reads back an attribute previously stashed via setAttribute(java.lang.String, java.lang.Object).
    Returns this call's request body - the exact bytes RIP itself is about to send for a @Body parameter (a raw String verbatim, a POJO serialized through the same configured ObjectMapper), for an interceptor that needs the literal outgoing body - e.g. a request-signing interceptor (AWS SigV4, an HMAC webhook signature, OAuth1) computing a signature over it before adding the result as a header via addHeader(java.lang.String, java.lang.String).
    Returns the headers added so far via addHeader(String, String).
    Returns the HTTP method of the request being made.
    Returns the fully resolved URL of the request being made.
    void
    setAttribute(String key, Object value)
    Lets an interceptor stash arbitrary per-call state in beforeRequest and read it back in afterResponse (e.g. a start timestamp to compute request duration).
    void
    Sets this call's request body - called internally by RIP itself, before beforeRequest runs, for a @Body value only (@FormUrlEncoded/@Multipart bodies aren't captured here - neither has one single serialized string representation the way a JSON/raw-string @Body does).
    Renders this call as a curl command reproducing it as closely as possible - the exact method, URL, headers, and body (if any) RIP is about to send - for pasting into a bug report or a terminal to reproduce a failure outside the JVM.
    Same as toCurlCommand(), with an extra curl flag (or flags) for the requested verbosity level - useful when the plain reproduction doesn't explain a failure and the wire-level detail curl itself can print (request/response headers, a raw dump of everything on the wire, or curl's own internal engine tracing) is what's actually needed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RequestContext

      public RequestContext(HTTPMethod httpMethod, String url)
      Creates the context for a single call.
      Parameters:
      httpMethod - the HTTP method of the request being made
      url - the fully resolved URL of the request being made
  • Method Details

    • getHttpMethod

      public HTTPMethod getHttpMethod()
      Returns the HTTP method of the request being made.
      Returns:
      the HTTP method
    • getUrl

      public String getUrl()
      Returns the fully resolved URL of the request being made.
      Returns:
      the URL
    • addHeader

      public void addHeader(String name, String value)
      Adds a header to be sent with the request. Only effective when called from beforeRequest - the request has already been sent by the time afterResponse runs.
      Parameters:
      name - the header name
      value - the header value
    • getHeaders

      public Map<String,String> getHeaders()
      Returns the headers added so far via addHeader(String, String).
      Returns:
      the headers
    • setBody

      public void setBody(String body)
      Sets this call's request body - called internally by RIP itself, before beforeRequest runs, for a @Body value only (@FormUrlEncoded/@Multipart bodies aren't captured here - neither has one single serialized string representation the way a JSON/raw-string @Body does). A String value is used verbatim; a POJO value is the exact JSON RIP itself sends, produced by the same configured ObjectMapper. Calling this from an interceptor has no effect on the request actually sent - by the time beforeRequest runs, the body this call sends is already fixed - so treat getBody() as read-only in practice, the same way getUrl() already is.
      Parameters:
      body - this call's request body, or null for a method with no @Body parameter (or one that received a null argument)
    • getBody

      public String getBody()
      Returns this call's request body - the exact bytes RIP itself is about to send for a @Body parameter (a raw String verbatim, a POJO serialized through the same configured ObjectMapper), for an interceptor that needs the literal outgoing body - e.g. a request-signing interceptor (AWS SigV4, an HMAC webhook signature, OAuth1) computing a signature over it before adding the result as a header via addHeader(java.lang.String, java.lang.String).
      Returns:
      this call's request body, or null for a method with no @Body parameter, one that received a null argument, or one using @FormUrlEncoded/@Multipart instead (neither is captured here)
    • setAttribute

      public void setAttribute(String key, Object value)
      Lets an interceptor stash arbitrary per-call state in beforeRequest and read it back in afterResponse (e.g. a start timestamp to compute request duration).
      Parameters:
      key - the attribute key
      value - the attribute value
    • getAttribute

      public Object getAttribute(String key)
      Reads back an attribute previously stashed via setAttribute(java.lang.String, java.lang.Object).
      Parameters:
      key - the attribute key
      Returns:
      the attribute value, or null if none was set
    • toCurlCommand

      public String toCurlCommand()
      Renders this call as a curl command reproducing it as closely as possible - the exact method, URL, headers, and body (if any) RIP is about to send - for pasting into a bug report or a terminal to reproduce a failure outside the JVM. Equivalent to toCurlCommand(CurlVerbosity.NONE).

      Most useful from afterResponse on an error status, or from a catch block for RestInPeaceHttpException - by then the body (if any) is already fixed, so the rendered command always matches what was actually sent.

      Returns:
      the equivalent curl command
    • toCurlCommand

      public String toCurlCommand(RequestContext.CurlVerbosity verbosity)
      Same as toCurlCommand(), with an extra curl flag (or flags) for the requested verbosity level - useful when the plain reproduction doesn't explain a failure and the wire-level detail curl itself can print (request/response headers, a raw dump of everything on the wire, or curl's own internal engine tracing) is what's actually needed. See RequestContext.CurlVerbosity's own javadoc for what each level adds, and an important caveat about RequestContext.CurlVerbosity.VV/RequestContext.CurlVerbosity.VVV/RequestContext.CurlVerbosity.VVVV in particular.
      Parameters:
      verbosity - how much extra diagnostic detail the rendered command asks curl to print - RequestContext.CurlVerbosity.NONE for none
      Returns:
      the equivalent curl command