Skip to content

Commit 173a814

Browse files
authored
Expose the default HttpClientBuilder so users can customize further (#713)
1 parent 3ef5405 commit 173a814

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,29 +123,53 @@ public ApacheHttpTransport(HttpClient httpClient) {
123123
* @since 1.30
124124
*/
125125
public static HttpClient newDefaultHttpClient() {
126+
return newDefaultHttpClientBuilder().build();
127+
}
128+
129+
/**
130+
* Creates a new Apache HTTP client builder that is used by the
131+
* {@link #ApacheHttpTransport()} constructor.
132+
*
133+
* <p>
134+
* Settings:
135+
* </p>
136+
* <ul>
137+
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.</li>
138+
* <li>The socket buffer size is set to 8192 using {@link SocketConfig}.</li>
139+
* <li><The retry mechanism is turned off using
140+
* {@link HttpClientBuilder#disableRedirectHandling}.</li>
141+
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with
142+
* {@link ProxySelector#getDefault()}, which uses the proxy settings from <a
143+
* href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
144+
* properties</a>.</li>
145+
* </ul>
146+
*
147+
* @return new instance of the Apache HTTP client
148+
* @since 1.31
149+
*/
150+
public static HttpClientBuilder newDefaultHttpClientBuilder() {
126151
// Set socket buffer sizes to 8192
127152
SocketConfig socketConfig =
128-
SocketConfig.custom()
129-
.setRcvBufSize(8192)
130-
.setSndBufSize(8192)
131-
.build();
153+
SocketConfig.custom()
154+
.setRcvBufSize(8192)
155+
.setSndBufSize(8192)
156+
.build();
132157

133158
PoolingHttpClientConnectionManager connectionManager =
134-
new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS);
159+
new PoolingHttpClientConnectionManager(-1, TimeUnit.MILLISECONDS);
135160
// Disable the stale connection check (previously configured in the HttpConnectionParams
136161
connectionManager.setValidateAfterInactivity(-1);
137162

138163
return HttpClientBuilder.create()
139-
.useSystemProperties()
140-
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
141-
.setDefaultSocketConfig(socketConfig)
142-
.setMaxConnTotal(200)
143-
.setMaxConnPerRoute(20)
144-
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
145-
.setConnectionManager(connectionManager)
146-
.disableRedirectHandling()
147-
.disableAutomaticRetries()
148-
.build();
164+
.useSystemProperties()
165+
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
166+
.setDefaultSocketConfig(socketConfig)
167+
.setMaxConnTotal(200)
168+
.setMaxConnPerRoute(20)
169+
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
170+
.setConnectionManager(connectionManager)
171+
.disableRedirectHandling()
172+
.disableAutomaticRetries();
149173
}
150174

151175
@Override

0 commit comments

Comments
 (0)