|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package co.elastic.clients.transport.rest_client; |
| 21 | + |
| 22 | +import org.apache.http.HttpException; |
| 23 | +import org.apache.http.HttpResponse; |
| 24 | +import org.apache.http.nio.ContentDecoder; |
| 25 | +import org.apache.http.nio.IOControl; |
| 26 | +import org.apache.http.nio.protocol.HttpAsyncResponseConsumer; |
| 27 | +import org.apache.http.protocol.HttpContext; |
| 28 | +import org.elasticsearch.client.HttpAsyncResponseConsumerFactory; |
| 29 | +import org.elasticsearch.client.RequestOptions; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +/** |
| 34 | + * A response consumer that will propagate Errors as RuntimeExceptions to avoid crashing the IOReactor. |
| 35 | + */ |
| 36 | +public class SafeResponseConsumer<T> implements HttpAsyncResponseConsumer<T> { |
| 37 | + |
| 38 | + private final HttpAsyncResponseConsumer<T> delegate; |
| 39 | + |
| 40 | + /** |
| 41 | + * A consumer factory that safely wraps the one provided by {@code RequestOptions.DEFAULT}. |
| 42 | + */ |
| 43 | + public static final HttpAsyncResponseConsumerFactory DEFAULT_FACTORY = () -> new SafeResponseConsumer<>( |
| 44 | + RequestOptions.DEFAULT.getHttpAsyncResponseConsumerFactory().createHttpAsyncResponseConsumer() |
| 45 | + ); |
| 46 | + |
| 47 | + /** |
| 48 | + * Same as {@code RequestOptions.DEFAULT} with a safe consumer factory |
| 49 | + */ |
| 50 | + public static final RequestOptions DEFAULT_REQUEST_OPTIONS; |
| 51 | + |
| 52 | + static { |
| 53 | + RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); |
| 54 | + builder.setHttpAsyncResponseConsumerFactory(DEFAULT_FACTORY); |
| 55 | + DEFAULT_REQUEST_OPTIONS = builder.build(); |
| 56 | + } |
| 57 | + |
| 58 | + public SafeResponseConsumer(HttpAsyncResponseConsumer<T> delegate) { |
| 59 | + this.delegate = delegate; |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + private static <T extends Throwable> void throwUnchecked(Throwable thr) throws T { |
| 64 | + throw (T) thr; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void responseReceived(HttpResponse response) throws IOException, HttpException { |
| 69 | + try { |
| 70 | + delegate.responseReceived(response); |
| 71 | + } catch(Exception e) { |
| 72 | + throwUnchecked(e); |
| 73 | + } catch(Throwable e) { |
| 74 | + throw new RuntimeException("Error receiving response", e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void consumeContent(ContentDecoder decoder, IOControl ioControl) throws IOException { |
| 80 | + try { |
| 81 | + delegate.consumeContent(decoder, ioControl); |
| 82 | + } catch(Exception e) { |
| 83 | + throwUnchecked(e); |
| 84 | + } catch(Throwable e) { |
| 85 | + throw new RuntimeException("Error consuming content", e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void responseCompleted(HttpContext context) { |
| 91 | + try { |
| 92 | + delegate.responseCompleted(context); |
| 93 | + } catch(Exception e) { |
| 94 | + throwUnchecked(e); |
| 95 | + } catch(Throwable e) { |
| 96 | + throw new RuntimeException("Error completing response", e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void failed(Exception ex) { |
| 102 | + try { |
| 103 | + delegate.failed(ex); |
| 104 | + } catch(Exception e) { |
| 105 | + throwUnchecked(e); |
| 106 | + } catch(Throwable e) { |
| 107 | + throw new RuntimeException("Error handling failure", e); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Exception getException() { |
| 113 | + return delegate.getException(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public T getResult() { |
| 118 | + return delegate.getResult(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean isDone() { |
| 123 | + return delegate.isDone(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void close() throws IOException { |
| 128 | + delegate.close(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean cancel() { |
| 133 | + return delegate.cancel(); |
| 134 | + } |
| 135 | +} |
0 commit comments