Package com.shri.restinpeace.interceptor
Class RequestContext
java.lang.Object
com.shri.restinpeace.interceptor.RequestContext
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 ClassesModifier and TypeClassDescriptionstatic enumHow much extra diagnostic detailtoCurlCommand(CurlVerbosity)askscurlitself to print, viacurl's own flags - RIP doesn't invent its own verbosity scheme, it just picks which standardcurlflag(s) to include. -
Constructor Summary
ConstructorsConstructorDescriptionRequestContext(HTTPMethod httpMethod, String url) Creates the context for a single call. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a header to be sent with the request.getAttribute(String key) Reads back an attribute previously stashed viasetAttribute(java.lang.String, java.lang.Object).getBody()Returns this call's request body - the exact bytes RIP itself is about to send for a@Bodyparameter (a rawStringverbatim, a POJO serialized through the same configuredObjectMapper), 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 viaaddHeader(java.lang.String, java.lang.String).Returns the headers added so far viaaddHeader(String, String).Returns the HTTP method of the request being made.getUrl()Returns the fully resolved URL of the request being made.voidsetAttribute(String key, Object value) Lets an interceptor stash arbitrary per-call state inbeforeRequestand read it back inafterResponse(e.g. a start timestamp to compute request duration).voidSets this call's request body - called internally by RIP itself, beforebeforeRequestruns, for a@Bodyvalue only (@FormUrlEncoded/@Multipartbodies aren't captured here - neither has one single serialized string representation the way a JSON/raw-string@Bodydoes).Renders this call as acurlcommand 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.toCurlCommand(RequestContext.CurlVerbosity verbosity) Same astoCurlCommand(), with an extracurlflag (or flags) for the requested verbosity level - useful when the plain reproduction doesn't explain a failure and the wire-level detailcurlitself can print (request/response headers, a raw dump of everything on the wire, orcurl's own internal engine tracing) is what's actually needed.
-
Constructor Details
-
RequestContext
Creates the context for a single call.- Parameters:
httpMethod- the HTTP method of the request being madeurl- the fully resolved URL of the request being made
-
-
Method Details
-
getHttpMethod
Returns the HTTP method of the request being made.- Returns:
- the HTTP method
-
getUrl
Returns the fully resolved URL of the request being made.- Returns:
- the URL
-
addHeader
Adds a header to be sent with the request. Only effective when called frombeforeRequest- the request has already been sent by the timeafterResponseruns.- Parameters:
name- the header namevalue- the header value
-
getHeaders
Returns the headers added so far viaaddHeader(String, String).- Returns:
- the headers
-
setBody
Sets this call's request body - called internally by RIP itself, beforebeforeRequestruns, for a@Bodyvalue only (@FormUrlEncoded/@Multipartbodies aren't captured here - neither has one single serialized string representation the way a JSON/raw-string@Bodydoes). AStringvalue is used verbatim; a POJO value is the exact JSON RIP itself sends, produced by the same configuredObjectMapper. Calling this from an interceptor has no effect on the request actually sent - by the timebeforeRequestruns, the body this call sends is already fixed - so treatgetBody()as read-only in practice, the same waygetUrl()already is.- Parameters:
body- this call's request body, ornullfor a method with no@Bodyparameter (or one that received anullargument)
-
getBody
Returns this call's request body - the exact bytes RIP itself is about to send for a@Bodyparameter (a rawStringverbatim, a POJO serialized through the same configuredObjectMapper), 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 viaaddHeader(java.lang.String, java.lang.String).- Returns:
- this call's request body, or
nullfor a method with no@Bodyparameter, one that received anullargument, or one using@FormUrlEncoded/@Multipartinstead (neither is captured here)
-
setAttribute
Lets an interceptor stash arbitrary per-call state inbeforeRequestand read it back inafterResponse(e.g. a start timestamp to compute request duration).- Parameters:
key- the attribute keyvalue- the attribute value
-
getAttribute
Reads back an attribute previously stashed viasetAttribute(java.lang.String, java.lang.Object).- Parameters:
key- the attribute key- Returns:
- the attribute value, or
nullif none was set
-
toCurlCommand
Renders this call as acurlcommand 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 totoCurlCommand(CurlVerbosity.NONE).Most useful from
afterResponseon an error status, or from acatchblock forRestInPeaceHttpException- by then the body (if any) is already fixed, so the rendered command always matches what was actually sent.- Returns:
- the equivalent
curlcommand
-
toCurlCommand
Same astoCurlCommand(), with an extracurlflag (or flags) for the requested verbosity level - useful when the plain reproduction doesn't explain a failure and the wire-level detailcurlitself can print (request/response headers, a raw dump of everything on the wire, orcurl's own internal engine tracing) is what's actually needed. SeeRequestContext.CurlVerbosity's own javadoc for what each level adds, and an important caveat aboutRequestContext.CurlVerbosity.VV/RequestContext.CurlVerbosity.VVV/RequestContext.CurlVerbosity.VVVVin particular.- Parameters:
verbosity- how much extra diagnostic detail the rendered command askscurlto print -RequestContext.CurlVerbosity.NONEfor none- Returns:
- the equivalent
curlcommand
-