|
| 1 | +package software.amazon.awssdk.crt.http; |
| 2 | + |
| 3 | +import software.amazon.awssdk.crt.CrtRuntimeException; |
| 4 | + |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | + |
| 7 | +/** |
| 8 | + * Manages a Pool of HTTP/1.1 Streams. Creates and manages HTTP/1.1 connections |
| 9 | + * under the hood. Will grab a connection from HttpClientConnectionManager to |
| 10 | + * make request on it, and will return it back until the request finishes. |
| 11 | + */ |
| 12 | +public class Http1StreamManager implements AutoCloseable { |
| 13 | + |
| 14 | + private HttpClientConnectionManager connectionManager = null; |
| 15 | + |
| 16 | + /** |
| 17 | + * Factory function for Http1StreamManager instances |
| 18 | + * |
| 19 | + * @param options the connection manager options configure to connection manager under the hood |
| 20 | + * @return a new instance of an Http1StreamManager |
| 21 | + */ |
| 22 | + public static Http1StreamManager create(HttpClientConnectionManagerOptions options) { |
| 23 | + return new Http1StreamManager(options); |
| 24 | + } |
| 25 | + |
| 26 | + private Http1StreamManager(HttpClientConnectionManagerOptions options) { |
| 27 | + this.connectionManager = HttpClientConnectionManager.create(options); |
| 28 | + } |
| 29 | + |
| 30 | + public CompletableFuture<Void> getShutdownCompleteFuture() { |
| 31 | + return this.connectionManager.getShutdownCompleteFuture(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Request an HTTP/1.1 HttpStream from StreamManager. |
| 36 | + * |
| 37 | + * @param request HttpRequest. The Request to make to the Server. |
| 38 | + * @param streamHandler HttpStreamResponseHandler. The Stream Handler to be called from the Native EventLoop |
| 39 | + * @return A future for a HttpStream that will be completed when the stream is |
| 40 | + * acquired. |
| 41 | + * @throws CrtRuntimeException Exception happens from acquiring stream. |
| 42 | + */ |
| 43 | + public CompletableFuture<HttpStream> acquireStream(HttpRequest request, |
| 44 | + HttpStreamResponseHandler streamHandler) { |
| 45 | + CompletableFuture<HttpStream> completionFuture = new CompletableFuture<>(); |
| 46 | + HttpClientConnectionManager connManager = this.connectionManager; |
| 47 | + this.connectionManager.acquireConnection().whenComplete((conn, throwable) -> { |
| 48 | + if (throwable != null) { |
| 49 | + completionFuture.completeExceptionally(throwable); |
| 50 | + } else { |
| 51 | + try { |
| 52 | + HttpStream stream = conn.makeRequest(request, new HttpStreamResponseHandler() { |
| 53 | + @Override |
| 54 | + public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, |
| 55 | + HttpHeader[] nextHeaders) { |
| 56 | + streamHandler.onResponseHeaders(stream, responseStatusCode, blockType, nextHeaders); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onResponseHeadersDone(HttpStream stream, int blockType) { |
| 61 | + streamHandler.onResponseHeadersDone(stream, blockType); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public int onResponseBody(HttpStream stream, byte[] bodyBytesIn) { |
| 66 | + return streamHandler.onResponseBody(stream, bodyBytesIn); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onResponseComplete(HttpStream stream, int errorCode) { |
| 71 | + streamHandler.onResponseComplete(stream, errorCode); |
| 72 | + /* Release the connection back */ |
| 73 | + connManager.releaseConnection(conn); |
| 74 | + } |
| 75 | + }); |
| 76 | + completionFuture.complete(stream); |
| 77 | + /* Active the stream for user */ |
| 78 | + try { |
| 79 | + stream.activate(); |
| 80 | + } catch (CrtRuntimeException e) { |
| 81 | + /* If activate failed, complete callback will not be invoked */ |
| 82 | + streamHandler.onResponseComplete(stream, e.errorCode); |
| 83 | + /* Release the connection back */ |
| 84 | + connManager.releaseConnection(conn); |
| 85 | + } |
| 86 | + } catch (Exception ex) { |
| 87 | + connManager.releaseConnection(conn); |
| 88 | + completionFuture.completeExceptionally(ex); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + return completionFuture; |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * Request an HTTP/1.1 HttpStream from StreamManager. |
| 98 | + * |
| 99 | + * @param request HttpRequestBase. The Request to make to the Server. |
| 100 | + * @param streamHandler HttpStreamBaseResponseHandler. The Stream Handler to be called from the Native EventLoop |
| 101 | + * @return A future for a HttpStreamBase that will be completed when the stream is |
| 102 | + * acquired. |
| 103 | + * @throws CrtRuntimeException Exception happens from acquiring stream. |
| 104 | + */ |
| 105 | + public CompletableFuture<HttpStreamBase> acquireStream(HttpRequestBase request, |
| 106 | + HttpStreamBaseResponseHandler streamHandler) { |
| 107 | + CompletableFuture<HttpStreamBase> completionFuture = new CompletableFuture<>(); |
| 108 | + HttpClientConnectionManager connManager = this.connectionManager; |
| 109 | + this.connectionManager.acquireConnection().whenComplete((conn, throwable) -> { |
| 110 | + if (throwable != null) { |
| 111 | + completionFuture.completeExceptionally(throwable); |
| 112 | + } else { |
| 113 | + try { |
| 114 | + HttpStreamBase stream = conn.makeRequest(request, new HttpStreamBaseResponseHandler() { |
| 115 | + @Override |
| 116 | + public void onResponseHeaders(HttpStreamBase stream, int responseStatusCode, int blockType, |
| 117 | + HttpHeader[] nextHeaders) { |
| 118 | + streamHandler.onResponseHeaders(stream, responseStatusCode, blockType, nextHeaders); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onResponseHeadersDone(HttpStreamBase stream, int blockType) { |
| 123 | + streamHandler.onResponseHeadersDone(stream, blockType); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int onResponseBody(HttpStreamBase stream, byte[] bodyBytesIn) { |
| 128 | + return streamHandler.onResponseBody(stream, bodyBytesIn); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onResponseComplete(HttpStreamBase stream, int errorCode) { |
| 133 | + streamHandler.onResponseComplete(stream, errorCode); |
| 134 | + /* Release the connection back */ |
| 135 | + connManager.releaseConnection(conn); |
| 136 | + } |
| 137 | + }); |
| 138 | + completionFuture.complete(stream); |
| 139 | + /* Active the stream for user */ |
| 140 | + try { |
| 141 | + stream.activate(); |
| 142 | + } catch (CrtRuntimeException e) { |
| 143 | + /* If activate failed, complete callback will not be invoked */ |
| 144 | + streamHandler.onResponseComplete(stream, e.errorCode); |
| 145 | + /* Release the connection back */ |
| 146 | + connManager.releaseConnection(conn); |
| 147 | + } |
| 148 | + } catch (Exception ex) { |
| 149 | + connManager.releaseConnection(conn); |
| 150 | + completionFuture.completeExceptionally(ex); |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | + return completionFuture; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void close() { |
| 159 | + this.connectionManager.close(); |
| 160 | + } |
| 161 | +} |
0 commit comments