Skip to main content

Extending RestClient

Subclass RestClient to change how every request is executed, or plug in a RestCallHandler for the same job without subclassing.

When to use it​

Use this when you need to intercept every call a client makes, for example to log it, sign it, or route it to a different host. For per-request changes, an interceptor or request builder is usually enough.

Example​

The following example overrides the primary run method.

import java.io.*;
import org.apache.http.*;
import org.apache.http.protocol.*;
import org.apache.juneau.rest.client.classic.*;

public class MyRestClient extends RestClient {

// Must provide this constructor!
public MyRestClient(RestClient.Builder<?> builder) {
super(builder);
}

@Override
public HttpResponse run(HttpHost target, HttpRequest request, HttpContext context) throws IOException {
// Perform special handling of requests, then run the default behavior.
return super.run(target, request, context);
}

public static void main(String[] args) throws Exception {
// Instantiate your client.
try (MyRestClient client = RestClient.create().json().build(MyRestClient.class)) {
System.out.println("Built " + client.getClass().getSimpleName());
}
}
}

The RestRequest and RestResponse objects can also be extended and integrated by overriding the createRequest(URI, String, boolean) and createResponse(RestRequest, HttpResponse, Parser) methods.

Options​

Extension pointUse it to
run(HttpHost, HttpRequest, HttpContext)Wrap or replace the execution of every call
createRequest(URI, String, boolean)Return your own RestRequest subclass
createResponse(RestRequest, HttpResponse, Parser)Return your own RestResponse subclass
RestCallHandlerReplace call execution without subclassing the client

Custom call handlers​

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

RestClient.BuildercallHandler(Class<? extends RestCallHandler>)RestCallHandlerrun(HttpHost,HttpRequest,HttpContext)
import java.io.*;
import org.apache.http.*;
import org.apache.http.client.methods.*;
import org.apache.http.protocol.*;
import org.apache.juneau.rest.client.classic.*;

// 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);
}

public static void main(String[] args) throws Exception {
// Create a client that uses our custom handler.
try (RestClient client = RestClient.create().json().callHandler(MyRestCallHandler.class).build()) {
System.out.println("Built RestClient");
}
}
}

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.

See also​