Skip to content

Commit 0a73a46

Browse files
micheldavidMike DaCosta
and
Mike DaCosta
authored
fix: restore the thread's interrupted status after catching InterruptedException (#1005) (#1006)
* fix: reset the thread's interrupt bit after catching InterruptedException (#1005) * - Updated code comment to: Mark thread as interrupted since we cannot throw InterruptedException here. - Also setting thread interrupted in HttpBackOffUnsuccessfulResponseHandler. - Added tests for both HttpBackOffIOExceptionHandler and HttpBackOffUnsuccessfulResponseHandler. * Fix bad merge * formatted files with "mvn clean com.coveo:fmt-maven-plugin:format" Co-authored-by: Mike DaCosta <[email protected]>
1 parent 2fd2f2b commit 0a73a46

File tree

40 files changed

+465
-384
lines changed

40 files changed

+465
-384
lines changed

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

+43-51
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,15 @@
3838
/**
3939
* Thread-safe HTTP transport based on the Apache HTTP Client library.
4040
*
41-
* <p>
42-
* Implementation is thread-safe, as long as any parameter modification to the
43-
* {@link #getHttpClient() Apache HTTP Client} is only done at initialization time. For maximum
44-
* efficiency, applications should use a single globally-shared instance of the HTTP transport.
45-
* </p>
41+
* <p>Implementation is thread-safe, as long as any parameter modification to the {@link
42+
* #getHttpClient() Apache HTTP Client} is only done at initialization time. For maximum efficiency,
43+
* applications should use a single globally-shared instance of the HTTP transport.
4644
*
47-
* <p>
48-
* Default settings are specified in {@link #newDefaultHttpClient()}. Use the
49-
* {@link #ApacheHttpTransport(HttpClient)} constructor to override the Apache HTTP Client used.
50-
* Please read the <a
45+
* <p>Default settings are specified in {@link #newDefaultHttpClient()}. Use the {@link
46+
* #ApacheHttpTransport(HttpClient)} constructor to override the Apache HTTP Client used. Please
47+
* read the <a
5148
* href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html">Apache HTTP
5249
* Client connection management tutorial</a> for more complex configuration options.
53-
* </p>
5450
*
5551
* @since 1.30
5652
* @author Yaniv Inbar
@@ -72,41 +68,38 @@ public ApacheHttpTransport() {
7268
/**
7369
* Constructor that allows an alternative Apache HTTP client to be used.
7470
*
75-
* <p>
76-
* Note that in the previous version, we overrode several settings. However, we are no longer able
77-
* to do so.
78-
* </p>
71+
* <p>Note that in the previous version, we overrode several settings. However, we are no longer
72+
* able to do so.
73+
*
74+
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that
7975
*
80-
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that</p>
8176
* <ul>
82-
* <li>HTTP version is set to 1.1.</li>
83-
* <li>Redirects are disabled (google-http-client handles redirects).</li>
84-
* <li>Retries are disabled (google-http-client handles retries).</li>
77+
* <li>HTTP version is set to 1.1.
78+
* <li>Redirects are disabled (google-http-client handles redirects).
79+
* <li>Retries are disabled (google-http-client handles retries).
8580
* </ul>
8681
*
8782
* @param httpClient Apache HTTP client to use
88-
*
8983
* @since 1.30
9084
*/
9185
public ApacheHttpTransport(HttpClient httpClient) {
9286
this.httpClient = httpClient;
9387
}
9488

9589
/**
96-
* Creates a new instance of the Apache HTTP client that is used by the
97-
* {@link #ApacheHttpTransport()} constructor.
90+
* Creates a new instance of the Apache HTTP client that is used by the {@link
91+
* #ApacheHttpTransport()} constructor.
92+
*
93+
* <p>Settings:
9894
*
99-
* <p>
100-
* Settings:
101-
* </p>
10295
* <ul>
103-
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.</li>
104-
* <li><The retry mechanism is turned off using
105-
* {@link HttpClientBuilder#disableRedirectHandling}.</li>
106-
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with
107-
* {@link ProxySelector#getDefault()}, which uses the proxy settings from <a
108-
* href="https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
109-
* properties</a>.</li>
96+
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
97+
* <li><The retry mechanism is turned off using {@link
98+
* HttpClientBuilder#disableRedirectHandling}.
99+
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
100+
* ProxySelector#getDefault()}, which uses the proxy settings from <a
101+
* href="https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
102+
* properties</a>.
110103
* </ul>
111104
*
112105
* @return new instance of the Apache HTTP client
@@ -117,20 +110,19 @@ public static HttpClient newDefaultHttpClient() {
117110
}
118111

119112
/**
120-
* Creates a new Apache HTTP client builder that is used by the
121-
* {@link #ApacheHttpTransport()} constructor.
113+
* Creates a new Apache HTTP client builder that is used by the {@link #ApacheHttpTransport()}
114+
* constructor.
115+
*
116+
* <p>Settings:
122117
*
123-
* <p>
124-
* Settings:
125-
* </p>
126118
* <ul>
127-
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.</li>
128-
* <li><The retry mechanism is turned off using
129-
* {@link HttpClientBuilder#disableRedirectHandling}.</li>
130-
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with
131-
* {@link ProxySelector#getDefault()}, which uses the proxy settings from <a
132-
* href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
133-
* properties</a>.</li>
119+
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.
120+
* <li><The retry mechanism is turned off using {@link
121+
* HttpClientBuilder#disableRedirectHandling}.
122+
* <li>The route planner uses {@link SystemDefaultRoutePlanner} with {@link
123+
* ProxySelector#getDefault()}, which uses the proxy settings from <a
124+
* href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
125+
* properties</a>.
134126
* </ul>
135127
*
136128
* @return new instance of the Apache HTTP client
@@ -139,14 +131,14 @@ public static HttpClient newDefaultHttpClient() {
139131
public static HttpClientBuilder newDefaultHttpClientBuilder() {
140132

141133
return HttpClientBuilder.create()
142-
.useSystemProperties()
143-
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
144-
.setMaxConnTotal(200)
145-
.setMaxConnPerRoute(20)
146-
.setConnectionTimeToLive(-1, TimeUnit.MILLISECONDS)
147-
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
148-
.disableRedirectHandling()
149-
.disableAutomaticRetries();
134+
.useSystemProperties()
135+
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
136+
.setMaxConnTotal(200)
137+
.setMaxConnPerRoute(20)
138+
.setConnectionTimeToLive(-1, TimeUnit.MILLISECONDS)
139+
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
140+
.disableRedirectHandling()
141+
.disableAutomaticRetries();
150142
}
151143

152144
@Override

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
import java.io.OutputStream;
2222
import org.apache.http.entity.AbstractHttpEntity;
2323

24-
/**
25-
* @author Yaniv Inbar
26-
*/
24+
/** @author Yaniv Inbar */
2725
final class ContentEntity extends AbstractHttpEntity {
2826

2927
/** Content length or less than zero if not known. */

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

-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@
1818
* @since 1.30
1919
* @author Yaniv Inbar
2020
*/
21-
2221
package com.google.api.client.http.apache.v2;
23-

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

+30-26
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, St
125125
fail("expected " + IllegalStateException.class);
126126
} catch (IllegalStateException e) {
127127
// expected
128-
assertEquals(e.getMessage(),
128+
assertEquals(
129+
e.getMessage(),
129130
"Apache HTTP client does not support " + method + " requests with content.");
130131
}
131132
}
@@ -141,16 +142,18 @@ private void execute(ApacheHttpRequest request) throws IOException {
141142
@Test
142143
public void testRequestShouldNotFollowRedirects() throws IOException {
143144
final AtomicInteger requestsAttempted = new AtomicInteger(0);
144-
HttpRequestExecutor requestExecutor = new HttpRequestExecutor() {
145-
@Override
146-
public HttpResponse execute(HttpRequest request, HttpClientConnection connection,
147-
HttpContext context) throws IOException, HttpException {
148-
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null);
149-
response.addHeader("location", "https://google.com/path");
150-
requestsAttempted.incrementAndGet();
151-
return response;
152-
}
153-
};
145+
HttpRequestExecutor requestExecutor =
146+
new HttpRequestExecutor() {
147+
@Override
148+
public HttpResponse execute(
149+
HttpRequest request, HttpClientConnection connection, HttpContext context)
150+
throws IOException, HttpException {
151+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null);
152+
response.addHeader("location", "https://google.com/path");
153+
requestsAttempted.incrementAndGet();
154+
return response;
155+
}
156+
};
154157
HttpClient client = HttpClients.custom().setRequestExecutor(requestExecutor).build();
155158
ApacheHttpTransport transport = new ApacheHttpTransport(client);
156159
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
@@ -162,17 +165,21 @@ public HttpResponse execute(HttpRequest request, HttpClientConnection connection
162165
@Test
163166
public void testRequestCanSetHeaders() {
164167
final AtomicBoolean interceptorCalled = new AtomicBoolean(false);
165-
HttpClient client = HttpClients.custom().addInterceptorFirst(new HttpRequestInterceptor() {
166-
@Override
167-
public void process(HttpRequest request, HttpContext context)
168-
throws HttpException, IOException {
169-
Header header = request.getFirstHeader("foo");
170-
assertNotNull("Should have found header", header);
171-
assertEquals("bar", header.getValue());
172-
interceptorCalled.set(true);
173-
throw new IOException("cancelling request");
174-
}
175-
}).build();
168+
HttpClient client =
169+
HttpClients.custom()
170+
.addInterceptorFirst(
171+
new HttpRequestInterceptor() {
172+
@Override
173+
public void process(HttpRequest request, HttpContext context)
174+
throws HttpException, IOException {
175+
Header header = request.getFirstHeader("foo");
176+
assertNotNull("Should have found header", header);
177+
assertEquals("bar", header.getValue());
178+
interceptorCalled.set(true);
179+
throw new IOException("cancelling request");
180+
}
181+
})
182+
.build();
176183

177184
ApacheHttpTransport transport = new ApacheHttpTransport(client);
178185
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
@@ -224,10 +231,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
224231
GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar");
225232
testUrl.setPort(server.getAddress().getPort());
226233
com.google.api.client.http.HttpResponse response =
227-
transport
228-
.createRequestFactory()
229-
.buildGetRequest(testUrl)
230-
.execute();
234+
transport.createRequestFactory().buildGetRequest(testUrl).execute();
231235
assertEquals(200, response.getStatusCode());
232236
assertEquals("/foo//bar", response.parseAsString());
233237
}

google-http-client-findbugs/src/main/java/com/google/api/client/findbugs/package-info.java

+16-18
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,26 @@
1515
/**
1616
* Findbugs package which supports custom Google APIs Client library findbugs Plugins.
1717
*
18-
* Usage on pom.xml:
18+
* <p>Usage on pom.xml:
1919
*
2020
* <pre>
21-
&lt;plugin&gt;
22-
&lt;groupId>org.codehaus.mojo&lt;/groupId&gt;
23-
&lt;artifactId>findbugs-maven-plugin&lt;/artifactId&gt;
24-
...
25-
&lt;configuration&gt;
26-
&lt;plugins&gt;
27-
&lt;plugin&gt;
28-
&lt;groupId&gt;com.google.http-client&lt;/groupId&gt;
29-
&lt;artifactId&gt;google-http-client-findbugs&lt;/artifactId&gt;
30-
&lt;version&gt;${project.http.version}&lt;/version&gt;
31-
&lt;/plugin&gt;
32-
&lt;/plugins&gt;
33-
&lt;/configuration&gt;
34-
...
35-
&lt;/plugin&gt;
21+
* &lt;plugin&gt;
22+
* &lt;groupId>org.codehaus.mojo&lt;/groupId&gt;
23+
* &lt;artifactId>findbugs-maven-plugin&lt;/artifactId&gt;
24+
* ...
25+
* &lt;configuration&gt;
26+
* &lt;plugins&gt;
27+
* &lt;plugin&gt;
28+
* &lt;groupId&gt;com.google.http-client&lt;/groupId&gt;
29+
* &lt;artifactId&gt;google-http-client-findbugs&lt;/artifactId&gt;
30+
* &lt;version&gt;${project.http.version}&lt;/version&gt;
31+
* &lt;/plugin&gt;
32+
* &lt;/plugins&gt;
33+
* &lt;/configuration&gt;
34+
* ...
35+
* &lt;/plugin&gt;
3636
* </pre>
3737
*
3838
* @author Eyal Peled
3939
*/
40-
4140
package com.google.api.client.findbugs;
42-

google-http-client-gson/src/test/java/com/google/api/client/json/gson/GsonParserTest.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
/**
22
* Copyright 2019 Google LLC
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
76
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
7+
* <p>https://www.apache.org/licenses/LICENSE-2.0
98
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
1412
* limitations under the License.
1513
*/
16-
1714
package com.google.api.client.json.gson;
1815

1916
import com.google.api.client.json.JsonFactory;

google-http-client-jackson2/src/main/java/com/google/api/client/json/jackson2/JacksonFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
/**
3030
* Low-level JSON library implementation based on Jackson 2.
3131
*
32-
* <p>Implementation is thread-safe. For maximum efficiency,
33-
* applications should use a single globally-shared instance of the JSON factory.
32+
* <p>Implementation is thread-safe. For maximum efficiency, applications should use a single
33+
* globally-shared instance of the JSON factory.
3434
*
3535
* @since 1.11
3636
* @author Yaniv Inbar

google-http-client-jackson2/src/test/java/com/google/api/client/json/jackson2/JacksonParserTest.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
/**
22
* Copyright 2019 Google LLC
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
76
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
7+
* <p>https://www.apache.org/licenses/LICENSE-2.0
98
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
1412
* limitations under the License.
1513
*/
16-
1714
package com.google.api.client.json.jackson2;
1815

1916
import com.google.api.client.json.JsonFactory;

google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonParserTest.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
/**
22
* Copyright 2019 Google LLC
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
76
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
7+
* <p>https://www.apache.org/licenses/LICENSE-2.0
98
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
1412
* limitations under the License.
1513
*/
16-
1714
package com.google.api.client.test.json;
1815

1916
import com.google.api.client.json.GenericJson;

google-http-client-xml/src/main/java/com/google/api/client/xml/Xml.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ public boolean stopAfterEndTag(String namespace, String localName) {
195195
/**
196196
* Parses an XML element using the given XML pull parser into the given destination object.
197197
*
198-
* <p>Requires the current event be {@link XmlPullParser#START_TAG} (skipping any initial
199-
* {@link XmlPullParser#START_DOCUMENT}) of the element being parsed. At normal parsing
200-
* completion, the current event will either be {@link XmlPullParser#END_TAG} of the element being
201-
* parsed, or the {@link XmlPullParser#START_TAG} of the requested {@code atom:entry}.
198+
* <p>Requires the current event be {@link XmlPullParser#START_TAG} (skipping any initial {@link
199+
* XmlPullParser#START_DOCUMENT}) of the element being parsed. At normal parsing completion, the
200+
* current event will either be {@link XmlPullParser#END_TAG} of the element being parsed, or the
201+
* {@link XmlPullParser#START_TAG} of the requested {@code atom:entry}.
202202
*
203203
* @param parser XML pull parser
204204
* @param destination optional destination object to parser into or {@code null} to ignore XML

0 commit comments

Comments
 (0)