All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.rxjava.ext.web.client.HttpRequest Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR2
Show newest version
/*
 * Copyright 2014 Red Hat, Inc.
 *
 * Red Hat licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package io.vertx.rxjava.ext.web.client;

import rx.Observable;
import rx.Single;
import io.vertx.rx.java.RxHelper;
import io.vertx.rx.java.WriteStreamSubscriber;
import io.vertx.rx.java.SingleOnSubscribeAdapter;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.lang.rx.RxGen;
import io.vertx.lang.rx.TypeArg;
import io.vertx.lang.rx.MappingIterator;

/**
 * A client-side HTTP request.
 * 

* Instances are created by an {@link io.vertx.rxjava.ext.web.client.WebClient} instance, via one of the methods corresponding to the specific * HTTP methods such as {@link io.vertx.rxjava.ext.web.client.WebClient#get}, etc... *

* The request shall be configured prior sending, the request is immutable and when a mutator method * is called, a new request is returned allowing to expose the request in a public API and apply further customization. *

* After the request has been configured, the methods *

    *
  • {@link io.vertx.rxjava.ext.web.client.HttpRequest#send}
  • *
  • {@link io.vertx.rxjava.ext.web.client.HttpRequest#sendStream}
  • *
  • ()}
  • *
  • {@link io.vertx.rxjava.ext.web.client.HttpRequest#sendForm}
  • *
* can be called. * The sendXXX methods perform the actual request, they can be called multiple times to perform the same HTTP * request at different points in time. *

* The handler is called back with *

    *
  • an {@link io.vertx.rxjava.ext.web.client.HttpResponse} instance when the HTTP response has been received
  • *
  • a failure when the HTTP request failed (like a connection error) or when the HTTP response could * not be obtained (like connection or unmarshalling errors)
  • *
*

* Most of the time, this client will buffer the HTTP response fully unless a specific is used * such as . * *

* NOTE: This class has been automatically generated from the {@link io.vertx.ext.web.client.HttpRequest original} non RX-ified interface using Vert.x codegen. */ @RxGen(io.vertx.ext.web.client.HttpRequest.class) public class HttpRequest { @Override public String toString() { return delegate.toString(); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; HttpRequest that = (HttpRequest) o; return delegate.equals(that.delegate); } @Override public int hashCode() { return delegate.hashCode(); } public static final TypeArg __TYPE_ARG = new TypeArg<>( obj -> new HttpRequest((io.vertx.ext.web.client.HttpRequest) obj), HttpRequest::getDelegate ); private final io.vertx.ext.web.client.HttpRequest delegate; public final TypeArg __typeArg_0; public HttpRequest(io.vertx.ext.web.client.HttpRequest delegate) { this.delegate = delegate; this.__typeArg_0 = TypeArg.unknown(); } public HttpRequest(Object delegate, TypeArg typeArg_0) { this.delegate = (io.vertx.ext.web.client.HttpRequest)delegate; this.__typeArg_0 = typeArg_0; } public io.vertx.ext.web.client.HttpRequest getDelegate() { return delegate; } /** * Configure the request to use a new method value. * @param value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest method(io.vertx.core.http.HttpMethod value) { delegate.method(value); return this; } /** * Configure the request to use a new port value. * @param value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest port(int value) { delegate.port(value); return this; } /** * Configure the request to decode the response with the responseCodec. * @param responseCodec the response codec * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest as(io.vertx.rxjava.ext.web.codec.BodyCodec responseCodec) { io.vertx.rxjava.ext.web.client.HttpRequest ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance((io.vertx.ext.web.client.HttpRequest)delegate.as(responseCodec.getDelegate()), responseCodec.__typeArg_0); return ret; } /** * Configure the request to use a new host value. * @param value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest host(String value) { delegate.host(value); return this; } /** * Configure the request to use a virtual host value. *

* Usually the header host (:authority pseudo header for HTTP/2) is set from the request host value * since this host value resolves to the server IP address. *

* Sometimes you need to set a host header for an address that does not resolve to the server IP address. * The virtual host value overrides the value of the actual host header (:authority pseudo header * for HTTP/2). *

* The virtual host is also be used for SNI. * @param value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest virtualHost(String value) { delegate.virtualHost(value); return this; } /** * Configure the request to use a new request URI value. *

* When the uri has query parameters, they are set in the {@link io.vertx.rxjava.ext.web.client.HttpRequest#queryParams} multimap, overwritting * any parameters previously set. * @param value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest uri(String value) { delegate.uri(value); return this; } /** * Configure the request to add multiple HTTP headers . * @param headers The HTTP headers * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest putHeaders(io.vertx.rxjava.core.MultiMap headers) { delegate.putHeaders(headers.getDelegate()); return this; } /** * Configure the request to set a new HTTP header. * @param name the header name * @param value the header value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest putHeader(String name, String value) { delegate.putHeader(name, value); return this; } /** * @return The HTTP headers */ public io.vertx.rxjava.core.MultiMap headers() { if (cached_0 != null) { return cached_0; } io.vertx.rxjava.core.MultiMap ret = io.vertx.rxjava.core.MultiMap.newInstance((io.vertx.core.MultiMap)delegate.headers()); cached_0 = ret; return ret; } /** * Configure the request to perform HTTP Authentication. *

* Performs a generic authentication using the credentials provided by the user. For the sake of validation safety * it is recommended that is called to ensure that the credentials * are applicable to the HTTP Challenged received on a previous request that returned a 401 response code. * @param credentials the credentials to use. * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest authentication(io.vertx.rxjava.ext.auth.authentication.Credentials credentials) { delegate.authentication(credentials.getDelegate()); return this; } /** * Configure the request to perform basic access authentication. *

* In basic HTTP authentication, a request contains a header field of the form 'Authorization: Basic <credentials>', * where credentials is the base64 encoding of id and password joined by a colon. *

* In practical terms the arguments are converted to a object. * @param id the id * @param password the password * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest basicAuthentication(String id, String password) { delegate.basicAuthentication(id, password); return this; } /** * Configure the request to perform basic access authentication. *

* In basic HTTP authentication, a request contains a header field of the form 'Authorization: Basic <credentials>', * where credentials is the base64 encoding of id and password joined by a colon. *

* In practical terms the arguments are converted to a object. * @param id the id * @param password the password * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest basicAuthentication(io.vertx.rxjava.core.buffer.Buffer id, io.vertx.rxjava.core.buffer.Buffer password) { delegate.basicAuthentication(id.getDelegate(), password.getDelegate()); return this; } /** * Configure the request to perform bearer token authentication. *

* In OAuth 2.0, a request contains a header field of the form 'Authorization: Bearer <bearerToken>', * where bearerToken is the bearer token issued by an authorization server to access protected resources. *

* In practical terms the arguments are converted to a object. * @param bearerToken the bearer token * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest bearerTokenAuthentication(String bearerToken) { delegate.bearerTokenAuthentication(bearerToken); return this; } public io.vertx.rxjava.ext.web.client.HttpRequest ssl(Boolean value) { delegate.ssl(value); return this; } /** * Configures the amount of time in milliseconds after which if the request does not return any data within the timeout * period an {@link java.util.concurrent.TimeoutException} fails the request. *

* Setting zero or a negative value disables the timeout. * @param value The quantity of time in milliseconds. * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest timeout(long value) { delegate.timeout(value); return this; } /** * Add a query parameter to the request. * @param paramName the param name * @param paramValue the param value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest addQueryParam(String paramName, String paramValue) { delegate.addQueryParam(paramName, paramValue); return this; } /** * Set a query parameter to the request. * @param paramName the param name * @param paramValue the param value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest setQueryParam(String paramName, String paramValue) { delegate.setQueryParam(paramName, paramValue); return this; } /** * Set wether or not to follow the directs for the request. * @param value true if redirections should be followed * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest followRedirects(boolean value) { delegate.followRedirects(value); return this; } /** * Add an expectation that the response is valid according to the provided predicate. *

* Multiple predicates can be added. * @param predicate the predicate * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest expect(Function, io.vertx.rxjava.ext.web.client.predicate.ResponsePredicateResult> predicate) { delegate.expect(new Function,io.vertx.ext.web.client.predicate.ResponsePredicateResult>() { public io.vertx.ext.web.client.predicate.ResponsePredicateResult apply(io.vertx.ext.web.client.HttpResponse arg) { io.vertx.rxjava.ext.web.client.predicate.ResponsePredicateResult ret = predicate.apply(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)arg, TypeArg.unknown())); return ret.getDelegate(); } }); return this; } /** * Add an expectation that the response is valid according to the provided predicate. *

* Multiple predicates can be added. * @param predicate the predicate * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest expect(io.vertx.rxjava.ext.web.client.predicate.ResponsePredicate predicate) { delegate.expect(predicate.getDelegate()); return this; } /** * Return the current query parameters. * @return the current query parameters */ public io.vertx.rxjava.core.MultiMap queryParams() { io.vertx.rxjava.core.MultiMap ret = io.vertx.rxjava.core.MultiMap.newInstance((io.vertx.core.MultiMap)delegate.queryParams()); return ret; } /** * Copy this request * @return a copy of this request */ public io.vertx.rxjava.ext.web.client.HttpRequest copy() { io.vertx.rxjava.ext.web.client.HttpRequest ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance((io.vertx.ext.web.client.HttpRequest)delegate.copy(), __typeArg_0); return ret; } /** * Allow or disallow multipart mixed encoding when sending having files sharing the same * file name. *
* The default value is true. *
* Set to false if you want to achieve the behavior for HTML5. * @param allow true allows use of multipart mixed encoding * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest multipartMixed(boolean allow) { io.vertx.rxjava.ext.web.client.HttpRequest ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance((io.vertx.ext.web.client.HttpRequest)delegate.multipartMixed(allow), __typeArg_0); return ret; } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body stream. * @param body the body * @param handler */ public void sendStream(io.vertx.rxjava.core.streams.ReadStream body, Handler>> handler) { delegate.sendStream(body.getDelegate(), new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body stream. * @param body the body */ public void sendStream(io.vertx.rxjava.core.streams.ReadStream body) { sendStream(body, ar -> { }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body stream. * @param body the body * @return */ public Single> rxSendStream(io.vertx.rxjava.core.streams.ReadStream body) { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { sendStream(body, fut); })); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body stream. * @param body the body * @param handler */ public void sendStream(Observable body, Handler>> handler) { delegate.sendStream(io.vertx.rx.java.ReadStreamSubscriber.asReadStream(body,obj -> (io.vertx.core.buffer.Buffer)obj.getDelegate()).resume(), new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body stream. * @param body the body */ public void sendStream(Observable body) { sendStream(body, ar -> { }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body stream. * @param body the body * @return */ public Single> rxSendStream(Observable body) { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { sendStream(body, fut); })); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body buffer. * @param body the body * @param handler */ public void sendBuffer(io.vertx.rxjava.core.buffer.Buffer body, Handler>> handler) { delegate.sendBuffer(body.getDelegate(), new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body buffer. * @param body the body */ public void sendBuffer(io.vertx.rxjava.core.buffer.Buffer body) { sendBuffer(body, ar -> { }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body buffer. * @param body the body * @return */ public Single> rxSendBuffer(io.vertx.rxjava.core.buffer.Buffer body) { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { sendBuffer(body, fut); })); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body object encoded as json and the content type * set to application/json. * @param body the body * @param handler */ public void sendJsonObject(JsonObject body, Handler>> handler) { delegate.sendJsonObject(body, new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body object encoded as json and the content type * set to application/json. * @param body the body */ public void sendJsonObject(JsonObject body) { sendJsonObject(body, ar -> { }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body object encoded as json and the content type * set to application/json. * @param body the body * @return */ public Single> rxSendJsonObject(JsonObject body) { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { sendJsonObject(body, fut); })); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body object encoded as json and the content type * set to application/json. * @param body the body * @param handler */ public void sendJson(java.lang.Object body, Handler>> handler) { delegate.sendJson(body, new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body object encoded as json and the content type * set to application/json. * @param body the body */ public void sendJson(java.lang.Object body) { sendJson(body, ar -> { }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body object encoded as json and the content type * set to application/json. * @param body the body * @return */ public Single> rxSendJson(java.lang.Object body) { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { sendJson(body, fut); })); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body multimap encoded as form and the content type * set to application/x-www-form-urlencoded. *

* When the content type header is previously set to multipart/form-data it will be used instead. * @param body the body * @param handler */ public void sendForm(io.vertx.rxjava.core.MultiMap body, Handler>> handler) { delegate.sendForm(body.getDelegate(), new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body multimap encoded as form and the content type * set to application/x-www-form-urlencoded. *

* When the content type header is previously set to multipart/form-data it will be used instead. * @param body the body */ public void sendForm(io.vertx.rxjava.core.MultiMap body) { sendForm(body, ar -> { }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body multimap encoded as form and the content type * set to application/x-www-form-urlencoded. *

* When the content type header is previously set to multipart/form-data it will be used instead. * @param body the body * @return */ public Single> rxSendForm(io.vertx.rxjava.core.MultiMap body) { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { sendForm(body, fut); })); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body multimap encoded as form and the content type * set to multipart/form-data. You may use this method to send attributes and upload files. * @param body the body * @param handler */ public void sendMultipartForm(io.vertx.rxjava.ext.web.multipart.MultipartForm body, Handler>> handler) { delegate.sendMultipartForm(body.getDelegate(), new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body multimap encoded as form and the content type * set to multipart/form-data. You may use this method to send attributes and upload files. * @param body the body */ public void sendMultipartForm(io.vertx.rxjava.ext.web.multipart.MultipartForm body) { sendMultipartForm(body, ar -> { }); } /** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request body multimap encoded as form and the content type * set to multipart/form-data. You may use this method to send attributes and upload files. * @param body the body * @return */ public Single> rxSendMultipartForm(io.vertx.rxjava.ext.web.multipart.MultipartForm body) { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { sendMultipartForm(body, fut); })); } /** * Send a request, the handler will receive the response as an {@link io.vertx.rxjava.ext.web.client.HttpResponse}. * @param handler */ public void send(Handler>> handler) { delegate.send(new Handler>>() { public void handle(AsyncResult> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance((io.vertx.ext.web.client.HttpResponse)ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); } /** * Send a request, the handler will receive the response as an {@link io.vertx.rxjava.ext.web.client.HttpResponse}. */ public void send() { send(ar -> { }); } /** * Send a request, the handler will receive the response as an {@link io.vertx.rxjava.ext.web.client.HttpResponse}. * @return */ public Single> rxSend() { return Single.create(new SingleOnSubscribeAdapter<>(fut -> { send(fut); })); } /** * Configure the request to set a new HTTP header with multiple values. * @param name the header name * @param value the header value * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.web.client.HttpRequest putHeader(String name, java.lang.Iterable value) { delegate.putHeader(name, value); return this; } private io.vertx.rxjava.core.MultiMap cached_0; public static HttpRequest newInstance(io.vertx.ext.web.client.HttpRequest arg) { return arg != null ? new HttpRequest(arg) : null; } public static HttpRequest newInstance(io.vertx.ext.web.client.HttpRequest arg, TypeArg __typeArg_T) { return arg != null ? new HttpRequest(arg, __typeArg_T) : null; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy