/
ApacheHttpTransport.java
195 lines (182 loc) · 6.89 KB
/
ApacheHttpTransport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.http.apache.v2;
import com.google.api.client.http.HttpMethods;
import com.google.api.client.http.HttpTransport;
import java.io.IOException;
import java.net.ProxySelector;
import java.util.concurrent.TimeUnit;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpOptions;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpTrace;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
/**
* Thread-safe HTTP transport based on the Apache HTTP Client library.
*
* <p>Implementation is thread-safe, as long as any parameter modification to the {@link
* #getHttpClient() Apache HTTP Client} is only done at initialization time. For maximum efficiency,
* applications should use a single globally-shared instance of the HTTP transport.
*
* <p>Default settings are specified in {@link #newDefaultHttpClient()}. Use the {@link
* #ApacheHttpTransport(HttpClient)} constructor to override the Apache HTTP Client used. Please
* read the <a
* href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html">Apache HTTP
* Client connection management tutorial</a> for more complex configuration options.
*
* @since 1.30
* @author Yaniv Inbar
*/
public final class ApacheHttpTransport extends HttpTransport {
/** Apache HTTP client. */
private final HttpClient httpClient;
/**
* Constructor that uses {@link #newDefaultHttpClient()} for the Apache HTTP client.
*
* @since 1.30
*/
public ApacheHttpTransport() {
this(newDefaultHttpClient());
}
/**
* Constructor that allows an alternative Apache HTTP client to be used.
*
* <p>Note that in the previous version, we overrode several settings. However, we are no longer
* able to do so.
*
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
*
* <ul>
* <li>HTTP version is set to 1.1.
* <li>Redirects are disabled (google-http-client handles redirects).
* <li>Retries are disabled (google-http-client handles retries).
* </ul>
*
* @param httpClient Apache HTTP client to use
* @since 1.30
*/
public ApacheHttpTransport(HttpClient httpClient) {
this.httpClient = httpClient;
}
/**
* Creates a new instance of the Apache HTTP client that is used by the {@link
* #ApacheHttpTransport()} constructor.
*
* <p>Settings:
*
* <ul>
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
* <li><The retry mechanism is turned off using {@link
* HttpClientBuilder#disableRedirectHandling}.
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
* ProxySelector#getDefault()}, which uses the proxy settings from <a
* href="https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
* properties</a>.
* </ul>
*
* @return new instance of the Apache HTTP client
* @since 1.30
*/
public static HttpClient newDefaultHttpClient() {
return newDefaultHttpClientBuilder().build();
}
/**
* Creates a new Apache HTTP client builder that is used by the {@link #ApacheHttpTransport()}
* constructor.
*
* <p>Settings:
*
* <ul>
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
* <li><The retry mechanism is turned off using {@link
* HttpClientBuilder#disableRedirectHandling}.
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
* ProxySelector#getDefault()}, which uses the proxy settings from <a
* href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
* properties</a>.
* </ul>
*
* @return new instance of the Apache HTTP client
* @since 1.31
*/
public static HttpClientBuilder newDefaultHttpClientBuilder() {
return HttpClientBuilder.create()
.useSystemProperties()
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
.setMaxConnTotal(200)
.setMaxConnPerRoute(20)
.setConnectionTimeToLive(-1, TimeUnit.MILLISECONDS)
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
.disableRedirectHandling()
.disableAutomaticRetries();
}
@Override
public boolean supportsMethod(String method) {
return true;
}
@Override
protected ApacheHttpRequest buildRequest(String method, String url) {
HttpRequestBase requestBase;
if (method.equals(HttpMethods.DELETE)) {
requestBase = new HttpDelete(url);
} else if (method.equals(HttpMethods.GET)) {
requestBase = new HttpGet(url);
} else if (method.equals(HttpMethods.HEAD)) {
requestBase = new HttpHead(url);
} else if (method.equals(HttpMethods.PATCH)) {
requestBase = new HttpPatch(url);
} else if (method.equals(HttpMethods.POST)) {
requestBase = new HttpPost(url);
} else if (method.equals(HttpMethods.PUT)) {
requestBase = new HttpPut(url);
} else if (method.equals(HttpMethods.TRACE)) {
requestBase = new HttpTrace(url);
} else if (method.equals(HttpMethods.OPTIONS)) {
requestBase = new HttpOptions(url);
} else {
requestBase = new HttpExtensionMethod(method, url);
}
return new ApacheHttpRequest(httpClient, requestBase);
}
/**
* Shuts down the connection manager and releases allocated resources. This closes all
* connections, whether they are currently used or not.
*
* @since 1.30
*/
@Override
public void shutdown() throws IOException {
if (httpClient instanceof CloseableHttpClient) {
((CloseableHttpClient) httpClient).close();
}
}
/**
* Returns the Apache HTTP client.
*
* @since 1.30
*/
public HttpClient getHttpClient() {
return httpClient;
}
}