I need to add a custom interceptor to HttpClientBuilder (for pre-emptive authentication).
Normally I would just call builder.addInterceptorLast(new MyCustomInterceptor()) in overridden createHttpClient() method of HttpWebConnection;
But HtmlUnit sets theHttpProcessor to HttpClientBuilder. And that discards any individually added interceptors.
The simplest option is to declare getHttpRequestInterceptors() as protected.
It would then be possible to override it adding necessary interceptors to the list.
In case you'd like to have more control over the interceptors (i.e. prohibit removing HtmlUnit's own ones) you could add new methods like:
protected List<HttpRequestInterceptor> getAdditionalRequestInterceptors(final WebRequest webRequest) {
return Collections.emptyList();
}
protected List<HttpResponseInterceptor> getAdditionalResponseInterceptors(final WebRequest webRequest) {
return Collections.emptyList();
}
and integrate them like this:
private void configureHttpProcessorBuilder(final HttpClientBuilder builder, final WebRequest webRequest) {
final HttpProcessorBuilder b = HttpProcessorBuilder.create();
// ...
b.add(new RequestAuthCache());
for (HttpRequestInterceptor extra : getAdditionalRequestInterceptors(webRequest)) {
b.add(extra);
}
b.add(new ResponseProcessCookies());
for (HttpResponseInterceptor extra : getAdditionalResponseInterceptors(webRequest)) {
b.add(extra);
}
builder.setHttpProcessor(b.build());
}