Skip to main content

Custom Call Handlers

The RestCallHandler interface provides the ability to provide custom handling of requests.

RestClient.BuildercallHandler(Class<? extends RestCallHandler>)RestCallHandlerrun(HttpHost,HttpRequest,HttpContext)
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 RestClient class and overriding the RestClient.run(HttpHost,HttpRequest,HttpContext) method or by defining your own HttpRequestExecutor. Using this interface is often simpler though.