{title:'Custom Call Handlers', created:'8.2.0', updated:'9.0.0'}

The {@link oajrc.RestCallHandler} interface provides the ability to provide custom handling of requests.

- {@link oajrc.RestClient.Builder}
  - {@link oajrc.RestClient.Builder#callHandler(Class) callHandler(Class<? extends RestCallHandler>)}
- {@link oajrc.RestCallHandler}
  - {@link oajrc.RestCallHandler#run(HttpHost,HttpRequest,HttpContext) run(HttpHost,HttpRequest,HttpContext)} returns HttpResponse

##### Example:

| // Our custom call handler. | public class MyRestCallHandler implements RestCallHandler { | | private final RestClient client; | | public MyRestCallHandler(RestClient client) { | this. client = client; | } | | @Override | public HttpResponse run(HttpHost target, HttpRequest request, HttpContext context) throws IOException { | // Insert any special handling here. | // The following is the default behavior: | if (target == null) | return client.execute((HttpUriRequest)request, context); | return client.execute(target, request, context); | } | } | | // Create a client that uses our custom handler. | RestClient client = RestClient() | .create() | .json() | .callHandler(MyCallHandler. class) | .build();

Note that there are other ways of accomplishing this such as extending the {@link oajrc.RestClient} class and overriding the {@link oajrc.RestClient#run(HttpHost,HttpRequest,HttpContext)} method or by defining your own {@link org.apache.http.protocol.HttpRequestExecutor}. Using this interface is often simpler though.