Skip to content

Commit

Permalink
fix: restore the thread's interrupted status after catching Interrupt…
Browse files Browse the repository at this point in the history
…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 0a73a46
Show file tree
Hide file tree
Showing 40 changed files with 465 additions and 384 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@
* @since 1.30
* @author Yaniv Inbar
*/

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

Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 0a73a46

Please sign in to comment.