Skip to content

Commit 6b6b7e3

Browse files
committed
prepare for production
1 parent 1b17559 commit 6b6b7e3

File tree

6 files changed

+71
-77
lines changed

6 files changed

+71
-77
lines changed

google-http-client-apache-v3/src/main/java/com/google/api/client/http/apache/v3/ApacheHttpRequest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import com.google.api.client.http.LowLevelHttpResponse;
1919
import java.io.IOException;
2020
import java.util.concurrent.TimeUnit;
21-
import org.apache.hc.client5.http.config.RequestConfig;
2221
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
22+
import org.apache.hc.client5.http.config.RequestConfig;
2323
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
2424
import org.apache.hc.core5.util.Timeout;
2525

@@ -35,10 +35,7 @@ final class ApacheHttpRequest extends LowLevelHttpRequest {
3535
this.httpClient = httpClient;
3636
this.request = request;
3737
// disable redirects as google-http-client handles redirects
38-
this.requestConfig =
39-
RequestConfig.custom()
40-
.setRedirectsEnabled(false)
41-
;
38+
this.requestConfig = RequestConfig.custom().setRedirectsEnabled(false);
4239
}
4340

4441
@Override
@@ -48,23 +45,19 @@ public void addHeader(String name, String value) {
4845

4946
@Override
5047
public void setTimeout(int connectTimeout, int readTimeout) throws IOException {
51-
// TODO: these methods are deprecated - we will need a more up-to-date way of setting timeouts
52-
// on existing requests. Also, since we can't control the lower level socket configuration,
53-
// we indirectly set a read timeout via ResponseTimeout
48+
// TODO: find a way to not use the @Deprecated setConnectTimeout method
5449
requestConfig
5550
.setConnectTimeout(Timeout.of(connectTimeout, TimeUnit.MILLISECONDS))
51+
// ResponseTimeout behaves the same as 4.x's SocketTimeout
5652
.setResponseTimeout(Timeout.of(readTimeout, TimeUnit.MILLISECONDS));
5753
}
5854

5955
@Override
6056
public LowLevelHttpResponse execute() throws IOException {
6157
if (getStreamingContent() != null) {
62-
// Preconditions.checkState(
63-
// request instanceof HttpEntityEnclosingRequest,
64-
// "Apache HTTP client does not support %s requests with content.",
65-
// request.getRequestLine().getMethod());
66-
ContentEntity entity = new ContentEntity(getContentLength(), getStreamingContent(),
67-
getContentType(), getContentEncoding());
58+
ContentEntity entity =
59+
new ContentEntity(
60+
getContentLength(), getStreamingContent(), getContentType(), getContentEncoding());
6861
request.setEntity(entity);
6962
}
7063
request.setConfig(requestConfig.build());

google-http-client-apache-v3/src/main/java/com/google/api/client/http/apache/v3/ApacheHttpResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public String getHeaderValue(int index) {
102102
/**
103103
* Aborts execution of the request.
104104
*
105-
* @since 1.30
105+
* @since 1.44
106106
*/
107107
@Override
108108
public void disconnect() {

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

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.IOException;
2222
import java.net.ProxySelector;
2323
import java.net.URI;
24-
import java.sql.Time;
2524
import java.util.concurrent.TimeUnit;
2625
import org.apache.hc.client5.http.classic.HttpClient;
2726
import org.apache.hc.client5.http.classic.methods.HttpDelete;
@@ -31,17 +30,17 @@
3130
import org.apache.hc.client5.http.classic.methods.HttpPatch;
3231
import org.apache.hc.client5.http.classic.methods.HttpPost;
3332
import org.apache.hc.client5.http.classic.methods.HttpPut;
34-
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
3533
import org.apache.hc.client5.http.classic.methods.HttpTrace;
34+
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
3635
import org.apache.hc.client5.http.config.ConnectionConfig;
37-
import org.apache.hc.client5.http.impl.classic.HttpClients;
38-
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
39-
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
4036
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
4137
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
38+
import org.apache.hc.client5.http.impl.classic.HttpClients;
4239
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
40+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
4341
import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner;
44-
import org.apache.hc.core5.http.io.SocketConfig;
42+
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
43+
import org.apache.hc.core5.io.CloseMode;
4544

4645
/**
4746
* Thread-safe HTTP transport based on the Apache HTTP Client library.
@@ -51,12 +50,12 @@
5150
* applications should use a single globally-shared instance of the HTTP transport.
5251
*
5352
* <p>Default settings are specified in {@link #newDefaultHttpClient()}. Use the {@link
54-
* #ApacheHttpTransport(CloseableHttpClient)} constructor to override the Apache HTTP Client used. Please
55-
* read the <a
56-
* href="https://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/pdf/httpclient-tutorial.pdf">
57-
* Apache HTTP Client connection management tutorial</a> for more complex configuration options.
53+
* #ApacheHttpTransport(CloseableHttpClient)} constructor to override the Apache HTTP Client used.
54+
* Please read the <a
55+
* href="https://github.com/apache/httpcomponents-client/blob/f898f1aca38f77f62a007856a674629cae5a02e6/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientConfiguration.java">
56+
* Apache HTTP Client 5.x configuration example</a> for more complex configuration options.
5857
*
59-
* @since 1.30
58+
* @since 1.44
6059
*/
6160
public final class ApacheHttpTransport extends HttpTransport {
6261

@@ -69,22 +68,20 @@ public final class ApacheHttpTransport extends HttpTransport {
6968
/**
7069
* Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client.
7170
*
72-
* @since 1.30
71+
* @since 1.44
7372
*/
7473
public ApacheHttpTransport() {
7574
this(newDefaultHttpClient(), false);
7675
}
7776

7877
/**
78+
* {@link Beta} <br>
7979
* Constructor that allows an alternative Apache HTTP client to be used.
8080
*
81-
* <p>Note that in the previous version, we overrode several settings. However, we are no longer
82-
* able to do so.
83-
*
8481
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
8582
*
8683
* <ul>
87-
* <li>Redirects are disabled (google-http-client handles redirects).
84+
* <li>HTTP version is set to 1.1.
8885
* <li>Retries are disabled (google-http-client handles retries).
8986
* </ul>
9087
*
@@ -99,13 +96,10 @@ public ApacheHttpTransport(CloseableHttpClient httpClient) {
9996
* {@link Beta} <br>
10097
* Constructor that allows an alternative CLoseable Apache HTTP client to be used.
10198
*
102-
* <p>Note that in the previous version, we overrode several settings. However, we are no longer
103-
* able to do so.
104-
*
10599
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
106100
*
107101
* <ul>
108-
* <li>Redirects are disabled (google-http-client handles redirects).
102+
* <li>HTTP version is set to 1.1.
109103
* <li>Retries are disabled (google-http-client handles retries).
110104
* </ul>
111105
*
@@ -119,6 +113,7 @@ public ApacheHttpTransport(CloseableHttpClient httpClient, boolean isMtls) {
119113
}
120114

121115
/**
116+
* {@link Beta} <br>
122117
* Creates a new instance of the Apache HTTP client that is used by the {@link
123118
* #ApacheHttpTransport()} constructor.
124119
*
@@ -130,12 +125,12 @@ public ApacheHttpTransport(CloseableHttpClient httpClient, boolean isMtls) {
130125
* HttpClientBuilder#disableRedirectHandling}.
131126
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
132127
* ProxySelector#getDefault()}, which uses the proxy settings from <a
133-
* href="https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
128+
* href="https://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">system
134129
* properties</a>.
135130
* </ul>
136131
*
137132
* @return new instance of the Apache HTTP client
138-
* @since 1.30
133+
* @since 1.44
139134
*/
140135
public static CloseableHttpClient newDefaultHttpClient() {
141136
return newDefaultCloseableHttpClientBuilder().build();
@@ -149,26 +144,27 @@ public static CloseableHttpClient newDefaultHttpClient() {
149144
*
150145
* <ul>
151146
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
152-
* <li><The retry mechanism is turned off using {@link
147+
* <li>The retry mechanism is turned off using {@link
153148
* HttpClientBuilder#disableRedirectHandling}.
149+
* <li>Redirects are turned off using {@link HttpClientBuilder#disableAutomaticRetries()}.
154150
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
155151
* ProxySelector#getDefault()}, which uses the proxy settings from <a
156-
* href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
152+
* href="http://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html">system
157153
* properties</a>.
158154
* </ul>
159155
*
160156
* @return new instance of the Apache HTTP client
161-
* @since 1.31
157+
* @since 1.44
162158
*/
163159
public static HttpClientBuilder newDefaultCloseableHttpClientBuilder() {
164-
PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
165-
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
166-
.setMaxConnTotal(200)
167-
.setMaxConnPerRoute(20)
168-
.setDefaultConnectionConfig(ConnectionConfig.custom()
169-
.setTimeToLive(-1, TimeUnit.MILLISECONDS)
170-
.build())
171-
.build();
160+
PoolingHttpClientConnectionManager connectionManager =
161+
PoolingHttpClientConnectionManagerBuilder.create()
162+
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
163+
.setMaxConnTotal(200)
164+
.setMaxConnPerRoute(20)
165+
.setDefaultConnectionConfig(
166+
ConnectionConfig.custom().setTimeToLive(-1, TimeUnit.MILLISECONDS).build())
167+
.build();
172168

173169
return HttpClients.custom()
174170
.useSystemProperties()
@@ -209,20 +205,20 @@ protected ApacheHttpRequest buildRequest(String method, String url) {
209205
}
210206

211207
/**
212-
* Shuts down the connection manager and releases allocated resources. This closes all
208+
* Gracefully shuts down the connection manager and releases allocated resources. This closes all
213209
* connections, whether they are currently used or not.
214210
*
215-
* @since 1.30
211+
* @since 1.44
216212
*/
217213
@Override
218214
public void shutdown() throws IOException {
219-
httpClient.close();
215+
httpClient.close(CloseMode.GRACEFUL);
220216
}
221217

222218
/**
223219
* Returns the Apache HTTP client.
224220
*
225-
* @since 1.30
221+
* @since 1.44
226222
*/
227223
public HttpClient getHttpClient() {
228224
return httpClient;

google-http-client-apache-v3/src/main/java/com/google/api/client/http/apache/v3/ContentEntity.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
22-
import org.apache.hc.core5.http.ContentType;
2322
import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
2423

2524
final class ContentEntity extends AbstractHttpEntity {
@@ -34,7 +33,11 @@ final class ContentEntity extends AbstractHttpEntity {
3433
* @param contentLength content length or less than zero if not known
3534
* @param streamingContent streaming content
3635
*/
37-
ContentEntity(long contentLength, StreamingContent streamingContent, String contentType, String contentEncoding) {
36+
ContentEntity(
37+
long contentLength,
38+
StreamingContent streamingContent,
39+
String contentType,
40+
String contentEncoding) {
3841
super(contentType, contentEncoding, contentLength == -1);
3942
this.contentLength = contentLength;
4043
this.streamingContent = Preconditions.checkNotNull(streamingContent);
@@ -68,6 +71,5 @@ public void writeTo(OutputStream out) throws IOException {
6871
}
6972

7073
@Override
71-
public void close() throws IOException {
72-
}
74+
public void close() throws IOException {}
7375
}

google-http-client-apache-v3/src/test/java/com/google/api/client/http/apache/v3/ApacheHttpRequestTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,7 @@
1414

1515
package com.google.api.client.http.apache.v3;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertFalse;
19-
import static org.junit.Assert.assertTrue;
2017

21-
import com.google.api.client.http.ByteArrayContent;
22-
import com.google.api.client.http.HttpContent;
23-
import com.google.api.client.http.InputStreamContent;
24-
import com.google.api.client.testing.http.apache.MockHttpClient;
25-
import java.io.ByteArrayInputStream;
26-
import java.nio.charset.StandardCharsets;
27-
import java.util.Arrays;
28-
import org.junit.Test;
2918

3019
public class ApacheHttpRequestTest {
3120

google-http-client-apache-v3/src/test/java/com/google/api/client/http/apache/v3/ApacheHttpTransportTest.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,46 @@
2323
import com.google.auth.http.HttpCredentialsAdapter;
2424
import com.google.auth.oauth2.GoogleCredentials;
2525
import java.io.IOException;
26+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
27+
import org.apache.hc.client5.http.impl.classic.HttpClients;
2628
import org.junit.Test;
2729

28-
/**
29-
* Tests {@link ApacheHttpTransport}.
30-
*/
30+
/** Tests {@link ApacheHttpTransport}. */
3131
public class ApacheHttpTransportTest {
3232

33-
@Test
34-
public void testClientUsingApacheV3Transport() throws IOException {
33+
public void testRequest(HttpTransport transport) throws IOException {
3534
final String PROJECT_ID = System.getenv("PROJECT_ID");
36-
HttpTransport transport = new ApacheHttpTransport();
3735

3836
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
3937
GsonFactory jsonFactory = GsonFactory.getDefaultInstance();
4038
CloudResourceManager.Builder resourceManagerBuilder =
4139
new CloudResourceManager.Builder(
42-
transport, jsonFactory, new HttpCredentialsAdapter(credentials))
40+
transport, jsonFactory, new HttpCredentialsAdapter(credentials))
4341
.setApplicationName("Example Java App");
4442
CloudResourceManager cloudResourceManager = resourceManagerBuilder.build();
4543

4644
Projects projects = cloudResourceManager.projects();
47-
Get get = projects.get("projects/"+PROJECT_ID);
45+
Get get = projects.get("projects/" + PROJECT_ID);
4846
Project project = get.execute();
4947
System.out.println("Project display name: " + project.getDisplayName());
5048
}
5149

52-
// private static class MockHttpResponse extends BasicHttpResponse implements CloseableHttpResponse {
50+
@Test
51+
public void testClientUsingDefaultApacheV3Transport() throws IOException {
52+
HttpTransport transport = new ApacheHttpTransport();
53+
testRequest(transport);
54+
}
55+
56+
@Test
57+
public void testClientUsingApacheV3TransportWithCustomHttpClient() throws IOException {
58+
CloseableHttpClient client =
59+
HttpClients.custom().disableAutomaticRetries().disableRedirectHandling().build();
60+
HttpTransport transport = new ApacheHttpTransport(client);
61+
testRequest(transport);
62+
}
63+
64+
// private static class MockHttpResponse extends BasicHttpResponse implements
65+
// CloseableHttpResponse {
5366
// public MockHttpResponse() {
5467
// super(200, "OK");
5568
// }
@@ -86,7 +99,8 @@ transport, jsonFactory, new HttpCredentialsAdapter(credentials))
8699
//
87100
// private void checkHttpClient(HttpClient client) {
88101
// assertNotNull(client);
89-
// // TODO(chingor): Is it possible to test this effectively? The newer HttpClient implementations
102+
// // TODO(chingor): Is it possible to test this effectively? The newer HttpClient
103+
// implementations
90104
// // are read-only and we're testing that we built the client with the right configuration
91105
// }
92106
//

0 commit comments

Comments
 (0)