/** * Consume the message by discarding the message body. * <p> * This method reads the entire message body from the networks and black holes * all the traffic. This has the benefit of keeping the underlying TCP connection * open for connection pooling. * <p> */ default void consume() { body().drop().aggregate(1) .thenApply(buffer -> { buffer.delegate().release(); return null; }); }
private static String bytesToString(ByteStream body) { try { return new String(body.aggregate(100000).get().content(), UTF_8); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } }
@Test public void contentAggregationOverflow() throws ExecutionException, InterruptedException { ByteStream stream = new ByteStream(Flux.just(buf1, buf2, buf3)); Throwable cause = stream.aggregate(2) .handle((result, throwable) -> throwable) .get(); assertThat(cause, instanceOf(ContentOverflowException.class)); }
private static String bytesToString(ByteStream body) throws Exception { return new String(body.aggregate(100000).get().content(), UTF_8); }
@Test(dataProvider = "emptyBodyResponses") public void convertsToStreamingHttpResponseWithEmptyBody(HttpResponse response) throws ExecutionException, InterruptedException { LiveHttpResponse streaming = response.stream(); byte[] result = streaming.body().aggregate(1000) .get() .content(); assertThat(result.length, is(0)); }
@Test public void aggregatesContent() throws ExecutionException, InterruptedException { ByteStream stream = new ByteStream(Flux.just(buf1, buf2, buf3)); Buffer aggregated = stream.aggregate(100).get(); assertThat(decodeUtf8String(aggregated), is("abc")); }
/** * Aggregates content stream and converts this request to a {@link HttpRequest}. * <p> * Returns a {@link Eventual} that eventually produces a * {@link HttpRequest}. The resulting full request object has the same * request line, headers, and content as this request. * <p> * The content stream is aggregated asynchronously. The stream may be connected * to a network socket or some other content producer. Once aggregated, a * HttpRequest object is emitted on the returned {@link Eventual}. * <p> * A sole {@code maxContentBytes} argument is a backstop defence against excessively * long content streams. The {@code maxContentBytes} should be set to a sensible * value according to your application requirements and heap size. When the content * size stream exceeds the {@code maxContentBytes}, a @{link ContentOverflowException} * is emitted on the returned observable. * * @param maxContentBytes maximum expected content size * @return a {@link Eventual} */ public Eventual<HttpRequest> aggregate(int maxContentBytes) { return Eventual.from( body.aggregate(maxContentBytes) .thenApply(it -> new HttpRequest.Builder(this, decodeAndRelease(it)).build()) ); }
/** * Aggregates content stream and converts this response to a {@link HttpResponse}. * <p> * Returns a {@link Eventual <HttpResponse>} that eventually produces a * {@link HttpResponse}. The resulting full response object has the same * response line, headers, and content as this response. * <p> * The content stream is aggregated asynchronously. The stream may be connected * to a network socket or some other content producer. Once aggregated, a * HttpResponse object is emitted on the returned {@link Eventual}. * <p> * A sole {@code maxContentBytes} argument is a backstop defence against excessively * long content streams. The {@code maxContentBytes} should be set to a sensible * value according to your application requirements and heap size. When the content * size stream exceeds the {@code maxContentBytes}, a @{link ContentOverflowException} * is emitted on the returned observable. * * @param maxContentBytes maximum expected content size * @return a {@link Eventual} */ public Eventual<HttpResponse> aggregate(int maxContentBytes) { return Eventual.from(body.aggregate(maxContentBytes)) .map(it -> new HttpResponse.Builder(this, decodeAndRelease(it)) .disableValidation() .build() ); }