Skip to content
Permalink
Browse files
fix: restore the thread's interrupted status after catching Interrupt…
…edException (#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 <mikedacosta@google.com>
  • Loading branch information
micheldavid and Mike DaCosta committed Jun 19, 2020
1 parent 2fd2f2b commit 0a73a4628b6ec4420db6b9cdbcc68899f3807c5b
Showing with 465 additions and 384 deletions.
  1. +43 −51 ...http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ApacheHttpTransport.java
  2. +1 −3 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/ContentEntity.java
  3. +0 −2 google-http-client-apache-v2/src/main/java/com/google/api/client/http/apache/v2/package-info.java
  4. +30 −26 ...-client-apache-v2/src/test/java/com/google/api/client/http/apache/v2/ApacheHttpTransportTest.java
  5. +16 −18 google-http-client-findbugs/src/main/java/com/google/api/client/findbugs/package-info.java
  6. +6 −9 google-http-client-gson/src/test/java/com/google/api/client/json/gson/GsonParserTest.java
  7. +2 −2 google-http-client-jackson2/src/main/java/com/google/api/client/json/jackson2/JacksonFactory.java
  8. +6 −9 google-http-client-jackson2/src/test/java/com/google/api/client/json/jackson2/JacksonParserTest.java
  9. +6 −9 google-http-client-test/src/main/java/com/google/api/client/test/json/AbstractJsonParserTest.java
  10. +4 −4 google-http-client-xml/src/main/java/com/google/api/client/xml/Xml.java
  11. +2 −0 google-http-client/src/main/java/com/google/api/client/http/HttpBackOffIOExceptionHandler.java
  12. +2 −1 ...-http-client/src/main/java/com/google/api/client/http/HttpBackOffUnsuccessfulResponseHandler.java
  13. +1 −0 google-http-client/src/main/java/com/google/api/client/http/HttpIOExceptionHandler.java
  14. +2 −1 google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java
  15. +6 −4 google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java
  16. +3 −3 google-http-client/src/main/java/com/google/api/client/http/MultipartContent.java
  17. +4 −4 google-http-client/src/main/java/com/google/api/client/http/UriTemplate.java
  18. +13 −10 google-http-client/src/main/java/com/google/api/client/http/UrlEncodedParser.java
  19. +2 −2 google-http-client/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java
  20. +2 −2 google-http-client/src/main/java/com/google/api/client/json/JsonString.java
  21. +2 −4 google-http-client/src/main/java/com/google/api/client/json/webtoken/DerEncoder.java
  22. +31 −16 google-http-client/src/main/java/com/google/api/client/json/webtoken/JsonWebSignature.java
  23. +3 −5 google-http-client/src/main/java/com/google/api/client/json/webtoken/JsonWebToken.java
  24. +3 −1 google-http-client/src/main/java/com/google/api/client/util/PemReader.java
  25. +29 −30 google-http-client/src/main/java/com/google/api/client/util/escape/CharEscapers.java
  26. +21 −25 google-http-client/src/main/java/com/google/api/client/util/escape/PercentEscaper.java
  27. +27 −27 google-http-client/src/main/java/com/google/api/client/util/store/FileDataStoreFactory.java
  28. +2 −1 google-http-client/src/test/java/com/google/api/client/http/ConsumingInputStreamTest.java
  29. +33 −0 google-http-client/src/test/java/com/google/api/client/http/HttpBackOffIOExpcetionHandlerTest.java
  30. +34 −0 ...p-client/src/test/java/com/google/api/client/http/HttpBackOffUnsuccessfulResponseHandlerTest.java
  31. +1 −2 google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java
  32. +44 −36 google-http-client/src/test/java/com/google/api/client/http/HttpRequestTracingTest.java
  33. +13 −9 google-http-client/src/test/java/com/google/api/client/http/HttpResponseTest.java
  34. +46 −34 google-http-client/src/test/java/com/google/api/client/http/MultipartContentTest.java
  35. +0 −1 google-http-client/src/test/java/com/google/api/client/http/OpenCensusUtilsTest.java
  36. +19 −20 google-http-client/src/test/java/com/google/api/client/json/webtoken/JsonWebSignatureTest.java
  37. +3 −5 google-http-client/src/test/java/com/google/api/client/util/DateTimeTest.java
  38. +1 −4 google-http-client/src/test/java/com/google/api/client/util/GenericDataTest.java
  39. +0 −1 google-http-client/src/test/java/com/google/api/client/util/IOUtilsTest.java
  40. +2 −3 google-http-client/src/test/java/com/google/api/client/util/escape/PercentEscaperTest.java
@@ -38,19 +38,15 @@
/**
* 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>
* <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
* <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.
* </p>
*
* @since 1.30
* @author Yaniv Inbar
@@ -72,41 +68,38 @@ public ApacheHttpTransport() {
/**
* 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>
* <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
*
* <p>If you choose to provide your own Apache HttpClient implementation, be sure that</p>
* <ul>
* <li>HTTP version is set to 1.1.</li>
* <li>Redirects are disabled (google-http-client handles redirects).</li>
* <li>Retries are disabled (google-http-client handles retries).</li>
* <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.
* Creates a new instance of the Apache HTTP client that is used by the {@link
* #ApacheHttpTransport()} constructor.
*
* <p>Settings:
*
* <p>
* Settings:
* </p>
* <ul>
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.</li>
* <li><The retry mechanism is turned off using
* {@link HttpClientBuilder#disableRedirectHandling}.</li>
* <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>.</li>
* <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
@@ -117,20 +110,19 @@ public static HttpClient newDefaultHttpClient() {
}

/**
* Creates a new Apache HTTP client builder that is used by the
* {@link #ApacheHttpTransport()} constructor.
* Creates a new Apache HTTP client builder that is used by the {@link #ApacheHttpTransport()}
* constructor.
*
* <p>Settings:
*
* <p>
* Settings:
* </p>
* <ul>
* <li>The client connection manager is set to {@link PoolingHttpClientConnectionManager}.</li>
* <li><The retry mechanism is turned off using
* {@link HttpClientBuilder#disableRedirectHandling}.</li>
* <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>.</li>
* <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
@@ -139,14 +131,14 @@ public static HttpClient newDefaultHttpClient() {
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();
.useSystemProperties()
.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory())
.setMaxConnTotal(200)
.setMaxConnPerRoute(20)
.setConnectionTimeToLive(-1, TimeUnit.MILLISECONDS)
.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()))
.disableRedirectHandling()
.disableAutomaticRetries();
}

@Override
@@ -21,9 +21,7 @@
import java.io.OutputStream;
import org.apache.http.entity.AbstractHttpEntity;

/**
* @author Yaniv Inbar
*/
/** @author Yaniv Inbar */
final class ContentEntity extends AbstractHttpEntity {

/** Content length or less than zero if not known. */
@@ -18,6 +18,4 @@
* @since 1.30
* @author Yaniv Inbar
*/

package com.google.api.client.http.apache.v2;

@@ -125,7 +125,8 @@ private void subtestUnsupportedRequestsWithContent(ApacheHttpRequest request, St
fail("expected " + IllegalStateException.class);
} catch (IllegalStateException e) {
// expected
assertEquals(e.getMessage(),
assertEquals(
e.getMessage(),
"Apache HTTP client does not support " + method + " requests with content.");
}
}
@@ -141,16 +142,18 @@ private void execute(ApacheHttpRequest request) throws IOException {
@Test
public void testRequestShouldNotFollowRedirects() throws IOException {
final AtomicInteger requestsAttempted = new AtomicInteger(0);
HttpRequestExecutor requestExecutor = new HttpRequestExecutor() {
@Override
public HttpResponse execute(HttpRequest request, HttpClientConnection connection,
HttpContext context) throws IOException, HttpException {
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null);
response.addHeader("location", "https://google.com/path");
requestsAttempted.incrementAndGet();
return response;
}
};
HttpRequestExecutor requestExecutor =
new HttpRequestExecutor() {
@Override
public HttpResponse execute(
HttpRequest request, HttpClientConnection connection, HttpContext context)
throws IOException, HttpException {
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 302, null);
response.addHeader("location", "https://google.com/path");
requestsAttempted.incrementAndGet();
return response;
}
};
HttpClient client = HttpClients.custom().setRequestExecutor(requestExecutor).build();
ApacheHttpTransport transport = new ApacheHttpTransport(client);
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
@@ -162,17 +165,21 @@ public HttpResponse execute(HttpRequest request, HttpClientConnection connection
@Test
public void testRequestCanSetHeaders() {
final AtomicBoolean interceptorCalled = new AtomicBoolean(false);
HttpClient client = HttpClients.custom().addInterceptorFirst(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
Header header = request.getFirstHeader("foo");
assertNotNull("Should have found header", header);
assertEquals("bar", header.getValue());
interceptorCalled.set(true);
throw new IOException("cancelling request");
}
}).build();
HttpClient client =
HttpClients.custom()
.addInterceptorFirst(
new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
Header header = request.getFirstHeader("foo");
assertNotNull("Should have found header", header);
assertEquals("bar", header.getValue());
interceptorCalled.set(true);
throw new IOException("cancelling request");
}
})
.build();

ApacheHttpTransport transport = new ApacheHttpTransport(client);
ApacheHttpRequest request = transport.buildRequest("GET", "https://google.com");
@@ -224,10 +231,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
GenericUrl testUrl = new GenericUrl("http://localhost/foo//bar");
testUrl.setPort(server.getAddress().getPort());
com.google.api.client.http.HttpResponse response =
transport
.createRequestFactory()
.buildGetRequest(testUrl)
.execute();
transport.createRequestFactory().buildGetRequest(testUrl).execute();
assertEquals(200, response.getStatusCode());
assertEquals("/foo//bar", response.parseAsString());
}
@@ -15,28 +15,26 @@
/**
* Findbugs package which supports custom Google APIs Client library findbugs Plugins.
*
* Usage on pom.xml:
* <p>Usage on pom.xml:
*
* <pre>
&lt;plugin&gt;
&lt;groupId>org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId>findbugs-maven-plugin&lt;/artifactId&gt;
...
&lt;configuration&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;com.google.http-client&lt;/groupId&gt;
&lt;artifactId&gt;google-http-client-findbugs&lt;/artifactId&gt;
&lt;version&gt;${project.http.version}&lt;/version&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/configuration&gt;
...
&lt;/plugin&gt;
* &lt;plugin&gt;
* &lt;groupId>org.codehaus.mojo&lt;/groupId&gt;
* &lt;artifactId>findbugs-maven-plugin&lt;/artifactId&gt;
* ...
* &lt;configuration&gt;
* &lt;plugins&gt;
* &lt;plugin&gt;
* &lt;groupId&gt;com.google.http-client&lt;/groupId&gt;
* &lt;artifactId&gt;google-http-client-findbugs&lt;/artifactId&gt;
* &lt;version&gt;${project.http.version}&lt;/version&gt;
* &lt;/plugin&gt;
* &lt;/plugins&gt;
* &lt;/configuration&gt;
* ...
* &lt;/plugin&gt;
* </pre>
*
* @author Eyal Peled
*/

package com.google.api.client.findbugs;

@@ -1,19 +1,16 @@
/**
* 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
* <p>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
*
* https://www.apache.org/licenses/LICENSE-2.0
* <p>https://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
* <p>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.json.gson;

import com.google.api.client.json.JsonFactory;
@@ -29,8 +29,8 @@
/**
* Low-level JSON library implementation based on Jackson 2.
*
* <p>Implementation is thread-safe. For maximum efficiency,
* applications should use a single globally-shared instance of the JSON factory.
* <p>Implementation is thread-safe. For maximum efficiency, applications should use a single
* globally-shared instance of the JSON factory.
*
* @since 1.11
* @author Yaniv Inbar
@@ -1,19 +1,16 @@
/**
* 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
* <p>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
*
* https://www.apache.org/licenses/LICENSE-2.0
* <p>https://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
* <p>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.json.jackson2;

import com.google.api.client.json.JsonFactory;
@@ -1,19 +1,16 @@
/**
* 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
* <p>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
*
* https://www.apache.org/licenses/LICENSE-2.0
* <p>https://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
* <p>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.test.json;

import com.google.api.client.json.GenericJson;
@@ -195,10 +195,10 @@ public boolean stopAfterEndTag(String namespace, String localName) {
/**
* Parses an XML element using the given XML pull parser into the given destination object.
*
* <p>Requires the current event be {@link XmlPullParser#START_TAG} (skipping any initial
* {@link XmlPullParser#START_DOCUMENT}) of the element being parsed. At normal parsing
* completion, the current event will either be {@link XmlPullParser#END_TAG} of the element being
* parsed, or the {@link XmlPullParser#START_TAG} of the requested {@code atom:entry}.
* <p>Requires the current event be {@link XmlPullParser#START_TAG} (skipping any initial {@link
* XmlPullParser#START_DOCUMENT}) of the element being parsed. At normal parsing completion, the
* current event will either be {@link XmlPullParser#END_TAG} of the element being parsed, or the
* {@link XmlPullParser#START_TAG} of the requested {@code atom:entry}.
*
* @param parser XML pull parser
* @param destination optional destination object to parser into or {@code null} to ignore XML

0 comments on commit 0a73a46

Please sign in to comment.