HTTPCLIENT-2103: ManagedHttpClientConnectionFactory provides a fluent builder

This commit is contained in:
Carter Kozak 2020-08-04 15:06:34 -04:00 committed by Oleg Kalnichevski
parent 9bc49cc439
commit 1a70490aac
2 changed files with 73 additions and 1 deletions

View File

@ -104,7 +104,12 @@ public class ProxyClient {
final CharCodingConfig charCodingConfig,
final RequestConfig requestConfig) {
super();
this.connFactory = connFactory != null ? connFactory : new ManagedHttpClientConnectionFactory(h1Config, charCodingConfig, null, null);
this.connFactory = connFactory != null
? connFactory
: ManagedHttpClientConnectionFactory.builder()
.http1Config(h1Config)
.charCodingConfig(charCodingConfig)
.build();
this.requestConfig = requestConfig != null ? requestConfig : RequestConfig.DEFAULT;
this.httpProcessor = new DefaultHttpProcessor(
new RequestTargetHost(), new RequestClientConnControl(), new RequestUserAgent());

View File

@ -139,4 +139,71 @@ public class ManagedHttpClientConnectionFactory implements HttpConnectionFactory
return conn;
}
/**
* Create a new {@link Builder}.
*
* @since 5.1
*/
public static Builder builder() {
return new Builder();
}
/**
* Builder for {@link ManagedHttpClientConnectionFactory}.
*
* @since 5.1
*/
public static final class Builder {
private Http1Config http1Config;
private CharCodingConfig charCodingConfig;
private ContentLengthStrategy incomingContentLengthStrategy;
private ContentLengthStrategy outgoingContentLengthStrategy;
private HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory;
private HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory;
private Builder() {}
public Builder http1Config(final Http1Config http1Config) {
this.http1Config = http1Config;
return this;
}
public Builder charCodingConfig(final CharCodingConfig charCodingConfig) {
this.charCodingConfig = charCodingConfig;
return this;
}
public Builder incomingContentLengthStrategy(final ContentLengthStrategy incomingContentLengthStrategy) {
this.incomingContentLengthStrategy = incomingContentLengthStrategy;
return this;
}
public Builder outgoingContentLengthStrategy(final ContentLengthStrategy outgoingContentLengthStrategy) {
this.outgoingContentLengthStrategy = outgoingContentLengthStrategy;
return this;
}
public Builder requestWriterFactory(
final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory) {
this.requestWriterFactory = requestWriterFactory;
return this;
}
public Builder responseParserFactory(
final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
this.responseParserFactory = responseParserFactory;
return this;
}
public ManagedHttpClientConnectionFactory build() {
return new ManagedHttpClientConnectionFactory(
http1Config,
charCodingConfig,
requestWriterFactory,
responseParserFactory,
incomingContentLengthStrategy,
outgoingContentLengthStrategy);
}
}
}