/** * Converts this request into a streaming form (HttpRequest). * * Converts this request into a HttpRequest object which represents the HTTP request as a * stream of bytes. * * @return A streaming HttpRequest object. */ public HttpRequest toStreamingRequest() { HttpRequest.Builder streamingBuilder = new HttpRequest.Builder(this) .clientAddress(clientAddress); if (this.body.length == 0) { return streamingBuilder.body(Observable.empty()).build(); } else { return streamingBuilder.body(Observable.just(Unpooled.copiedBuffer(body))).build(); } }
@Test public void convertsToStreamingHttpRequest() throws Exception { HttpRequest fullRequest = new HttpRequest.Builder(POST, "/foo/bar").body("foobar", UTF_8) .version(HTTP_1_1) .header("HeaderName", "HeaderValue") .cookies(requestCookie("CookieName", "CookieValue")) .build(); LiveHttpRequest streaming = fullRequest.stream(); assertThat(streaming.method(), is(HttpMethod.POST)); assertThat(streaming.url(), is(url("/foo/bar").build())); assertThat(streaming.version(), is(HTTP_1_1)); assertThat(streaming.headers(), containsInAnyOrder( header("Content-Length", "6"), header("HeaderName", "HeaderValue"), header("Cookie", "CookieName=CookieValue"))); assertThat(streaming.cookies(), contains(requestCookie("CookieName", "CookieValue"))); StepVerifier.create(streaming.aggregate(0x10000).map(it -> it.bodyAs(UTF_8))) .expectNext("foobar") .verifyComplete(); }
/** * 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()) ); }
/** * Return a new {@link Builder} that will inherit properties from this request. * This allows a new request to be made that will be identical to this one except for the properties * overridden by the builder methods. * * @return new builder based on this request */ public Builder newBuilder() { return new Builder(this); }
/** * Creates a request with the POST method. * * @param uri URI * @return {@link HttpRequest.Builder} */ public static Builder post(String uri) { return new Builder(POST, uri); }
/** * Creates a request with the HEAD method. * * @param uri URI * @return {@link HttpRequest.Builder} */ public static Builder head(String uri) { return new Builder(HEAD, uri); }
/** * Return a new {@link HttpRequest.Builder} that will inherit properties from this request. * This allows a new request to be made that will be identical to this one except for the properties * overridden by the builder methods. * * @return new builder based on this request */ public Builder newBuilder() { return new Builder(this); }
/** * Creates a request with the PUT method. * * @param uri URI * @return {@link HttpRequest.Builder} */ public static Builder put(String uri) { return new Builder(PUT, uri); }
/** * Creates a request with the GET method. * * @param uri URI * @return {@link HttpRequest.Builder} */ public static Builder get(String uri) { return new Builder(GET, uri); }
/** * Creates a request with the PATCH method. * * @param uri URI * @return {@code this} */ public static Builder patch(String uri) { return new Builder(PATCH, uri); }
/** * Creates a request with the PUT method. * * @param uri URI * @return {@code this} */ public static Builder put(String uri) { return new Builder(PUT, uri); }
/** * Creates a request with the HEAD method. * * @param uri URI * @return {@code this} */ public static Builder head(String uri) { return new Builder(HEAD, uri); }
/** * Creates a request with the POST method. * * @param uri URI * @return {@code this} */ public static Builder post(String uri) { return new Builder(POST, uri); }
/** * Creates a request with the GET method. * * @param uri URI * @return {@code this} */ public static Builder get(String uri) { return new Builder(GET, uri); }
@DataProvider(name = "emptyBodyRequests") private Object[][] emptyBodyRequests() { return new Object[][]{ {get("/foo/bar").build()}, {new HttpRequest.Builder(POST, "/foo/bar").body(null, UTF_8).build()}, {new HttpRequest.Builder(POST, "/foo/bar").body("", UTF_8).build()}, {new HttpRequest.Builder(POST, "/foo/bar").body(null, UTF_8, true).build()}, {new HttpRequest.Builder(POST, "/foo/bar").body("", UTF_8, true).build()}, {new HttpRequest.Builder(POST, "/foo/bar").body(null, true).build()}, {new HttpRequest.Builder(POST, "/foo/bar").body(new byte[0], true).build()}, }; }
/** * Creates a request with the DELETE method. * * @param uri URI * @return {@code this} */ public static Builder delete(String uri) { return new Builder(DELETE, uri); }
/** * Creates a request with the PATCH method. * * @param uri URI * @return {@link HttpRequest.Builder} */ public static Builder patch(String uri) { return new Builder(PATCH, uri); }
/** * Creates a request with the DELETE method. * * @param uri URI * @return {@link HttpRequest.Builder} */ public static Builder delete(String uri) { return new Builder(DELETE, uri); }