|
38 | 38 | import org.apache.http.client.protocol.HttpClientContext;
|
39 | 39 | import org.apache.http.client.utils.URIBuilder;
|
40 | 40 | import org.apache.http.concurrent.FutureCallback;
|
| 41 | +import org.apache.http.conn.ConnectTimeoutException; |
41 | 42 | import org.apache.http.impl.auth.BasicScheme;
|
42 | 43 | import org.apache.http.impl.client.BasicAuthCache;
|
43 | 44 | import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
|
47 | 48 |
|
48 | 49 | import java.io.Closeable;
|
49 | 50 | import java.io.IOException;
|
| 51 | +import java.net.SocketTimeoutException; |
50 | 52 | import java.net.URI;
|
51 | 53 | import java.net.URISyntaxException;
|
52 | 54 | import java.util.ArrayList;
|
@@ -218,7 +220,8 @@ public Response performRequest(String method, String endpoint, Map<String, Strin
|
218 | 220 | HttpEntity entity, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
|
219 | 221 | Header... headers) throws IOException {
|
220 | 222 | SyncResponseListener listener = new SyncResponseListener(maxRetryTimeoutMillis);
|
221 |
| - performRequestAsync(method, endpoint, params, entity, httpAsyncResponseConsumerFactory, listener, headers); |
| 223 | + performRequestAsyncNoCatch(method, endpoint, params, entity, httpAsyncResponseConsumerFactory, |
| 224 | + listener, headers); |
222 | 225 | return listener.get();
|
223 | 226 | }
|
224 | 227 |
|
@@ -293,43 +296,50 @@ public void performRequestAsync(String method, String endpoint, Map<String, Stri
|
293 | 296 | HttpEntity entity, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
|
294 | 297 | ResponseListener responseListener, Header... headers) {
|
295 | 298 | try {
|
296 |
| - Objects.requireNonNull(params, "params must not be null"); |
297 |
| - Map<String, String> requestParams = new HashMap<>(params); |
298 |
| - //ignore is a special parameter supported by the clients, shouldn't be sent to es |
299 |
| - String ignoreString = requestParams.remove("ignore"); |
300 |
| - Set<Integer> ignoreErrorCodes; |
301 |
| - if (ignoreString == null) { |
302 |
| - if (HttpHead.METHOD_NAME.equals(method)) { |
303 |
| - //404 never causes error if returned for a HEAD request |
304 |
| - ignoreErrorCodes = Collections.singleton(404); |
305 |
| - } else { |
306 |
| - ignoreErrorCodes = Collections.emptySet(); |
307 |
| - } |
| 299 | + performRequestAsyncNoCatch(method, endpoint, params, entity, httpAsyncResponseConsumerFactory, |
| 300 | + responseListener, headers); |
| 301 | + } catch (Exception e) { |
| 302 | + responseListener.onFailure(e); |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + void performRequestAsyncNoCatch(String method, String endpoint, Map<String, String> params, |
| 307 | + HttpEntity entity, HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory, |
| 308 | + ResponseListener responseListener, Header... headers) { |
| 309 | + Objects.requireNonNull(params, "params must not be null"); |
| 310 | + Map<String, String> requestParams = new HashMap<>(params); |
| 311 | + //ignore is a special parameter supported by the clients, shouldn't be sent to es |
| 312 | + String ignoreString = requestParams.remove("ignore"); |
| 313 | + Set<Integer> ignoreErrorCodes; |
| 314 | + if (ignoreString == null) { |
| 315 | + if (HttpHead.METHOD_NAME.equals(method)) { |
| 316 | + //404 never causes error if returned for a HEAD request |
| 317 | + ignoreErrorCodes = Collections.singleton(404); |
308 | 318 | } else {
|
309 |
| - String[] ignoresArray = ignoreString.split(","); |
310 |
| - ignoreErrorCodes = new HashSet<>(); |
311 |
| - if (HttpHead.METHOD_NAME.equals(method)) { |
312 |
| - //404 never causes error if returned for a HEAD request |
313 |
| - ignoreErrorCodes.add(404); |
314 |
| - } |
315 |
| - for (String ignoreCode : ignoresArray) { |
316 |
| - try { |
317 |
| - ignoreErrorCodes.add(Integer.valueOf(ignoreCode)); |
318 |
| - } catch (NumberFormatException e) { |
319 |
| - throw new IllegalArgumentException("ignore value should be a number, found [" + ignoreString + "] instead", e); |
320 |
| - } |
| 319 | + ignoreErrorCodes = Collections.emptySet(); |
| 320 | + } |
| 321 | + } else { |
| 322 | + String[] ignoresArray = ignoreString.split(","); |
| 323 | + ignoreErrorCodes = new HashSet<>(); |
| 324 | + if (HttpHead.METHOD_NAME.equals(method)) { |
| 325 | + //404 never causes error if returned for a HEAD request |
| 326 | + ignoreErrorCodes.add(404); |
| 327 | + } |
| 328 | + for (String ignoreCode : ignoresArray) { |
| 329 | + try { |
| 330 | + ignoreErrorCodes.add(Integer.valueOf(ignoreCode)); |
| 331 | + } catch (NumberFormatException e) { |
| 332 | + throw new IllegalArgumentException("ignore value should be a number, found [" + ignoreString + "] instead", e); |
321 | 333 | }
|
322 | 334 | }
|
323 |
| - URI uri = buildUri(pathPrefix, endpoint, requestParams); |
324 |
| - HttpRequestBase request = createHttpRequest(method, uri, entity); |
325 |
| - setHeaders(request, headers); |
326 |
| - FailureTrackingResponseListener failureTrackingResponseListener = new FailureTrackingResponseListener(responseListener); |
327 |
| - long startTime = System.nanoTime(); |
328 |
| - performRequestAsync(startTime, nextHost(), request, ignoreErrorCodes, httpAsyncResponseConsumerFactory, |
329 |
| - failureTrackingResponseListener); |
330 |
| - } catch (Exception e) { |
331 |
| - responseListener.onFailure(e); |
332 | 335 | }
|
| 336 | + URI uri = buildUri(pathPrefix, endpoint, requestParams); |
| 337 | + HttpRequestBase request = createHttpRequest(method, uri, entity); |
| 338 | + setHeaders(request, headers); |
| 339 | + FailureTrackingResponseListener failureTrackingResponseListener = new FailureTrackingResponseListener(responseListener); |
| 340 | + long startTime = System.nanoTime(); |
| 341 | + performRequestAsync(startTime, nextHost(), request, ignoreErrorCodes, httpAsyncResponseConsumerFactory, |
| 342 | + failureTrackingResponseListener); |
333 | 343 | }
|
334 | 344 |
|
335 | 345 | private void performRequestAsync(final long startTime, final HostTuple<Iterator<HttpHost>> hostTuple, final HttpRequestBase request,
|
@@ -674,12 +684,30 @@ Response get() throws IOException {
|
674 | 684 | e.addSuppressed(exception);
|
675 | 685 | throw e;
|
676 | 686 | }
|
677 |
| - //try and leave the exception untouched as much as possible but we don't want to just add throws Exception clause everywhere |
| 687 | + /* |
| 688 | + * Wrap and rethrow whatever exception we received, copying the type |
| 689 | + * where possible so the synchronous API looks as much as possible |
| 690 | + * like the asynchronous API. We wrap the exception so that the caller's |
| 691 | + * signature shows up in any exception we throw. |
| 692 | + */ |
| 693 | + if (exception instanceof ResponseException) { |
| 694 | + throw new ResponseException((ResponseException) exception); |
| 695 | + } |
| 696 | + if (exception instanceof ConnectTimeoutException) { |
| 697 | + ConnectTimeoutException e = new ConnectTimeoutException(exception.getMessage()); |
| 698 | + e.initCause(exception); |
| 699 | + throw e; |
| 700 | + } |
| 701 | + if (exception instanceof SocketTimeoutException) { |
| 702 | + SocketTimeoutException e = new SocketTimeoutException(exception.getMessage()); |
| 703 | + e.initCause(exception); |
| 704 | + throw e; |
| 705 | + } |
678 | 706 | if (exception instanceof IOException) {
|
679 |
| - throw (IOException) exception; |
| 707 | + throw new IOException(exception.getMessage(), exception); |
680 | 708 | }
|
681 | 709 | if (exception instanceof RuntimeException){
|
682 |
| - throw (RuntimeException) exception; |
| 710 | + throw new RuntimeException(exception.getMessage(), exception); |
683 | 711 | }
|
684 | 712 | throw new RuntimeException("error while performing request", exception);
|
685 | 713 | }
|
|
0 commit comments