Search in sources :

Example 6 with WebClientOptions

use of io.vertx.ext.web.client.WebClientOptions in project vertx-openshift-it by cescoffier.

the class GreetingServiceVerticle method start.

@Override
public void start() throws Exception {
    circuit = CircuitBreaker.create("circuit-breaker", vertx, new CircuitBreakerOptions().setFallbackOnFailure(true).setMaxFailures(3).setResetTimeout(5000).setNotificationAddress("circuit-breaker").setTimeout(1000));
    client = WebClient.create(vertx, new WebClientOptions().setDefaultHost("name-service").setDefaultPort(8080));
    Router router = Router.router(vertx);
    router.get("/health").handler(rc -> rc.response().end("OK"));
    router.get("/eventbus/*").handler(getSockJsHandler());
    // The address is the circuit breaker notification address configured above.
    router.get("/metrics").handler(HystrixMetricHandler.create(vertx, "circuit-breaker"));
    router.get("/api/greeting").handler(this::greeting);
    router.get("/api/cb-state").handler(rc -> rc.response().putHeader(CONTENT_TYPE.toString(), APPLICATION_JSON.toString()).end(new JsonObject().put("state", circuit.state()).encodePrettily()));
    router.get("/*").handler(StaticHandler.create());
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) Router(io.vertx.rxjava.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) CircuitBreakerOptions(io.vertx.circuitbreaker.CircuitBreakerOptions)

Example 7 with WebClientOptions

use of io.vertx.ext.web.client.WebClientOptions in project NetDiscovery by fengzhizi715.

the class VertxDownloader method download.

public Maybe<Response> download(Request request) {
    WebClientOptions options = initWebClientOptions(request);
    webClient = WebClient.create(vertx, options);
    HttpRequest<Buffer> httpRequest = null;
    if (request.getHttpMethod() == HttpMethod.GET) {
        if ("http".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.getAbs(request.getUrl());
        } else if ("https".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.get(443, request.getUrlParser().getHost(), Preconditions.isNotBlank(request.getUrlParser().getQuery()) ? request.getUrlParser().getPath() + "?" + request.getUrlParser().getQuery() : request.getUrlParser().getPath()).ssl(true);
        }
    } else if (request.getHttpMethod() == HttpMethod.POST) {
        if ("http".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.postAbs(request.getUrl());
        } else if ("https".equals(request.getUrlParser().getProtocol())) {
            httpRequest = webClient.post(443, request.getUrlParser().getHost(), Preconditions.isNotBlank(request.getUrlParser().getQuery()) ? request.getUrlParser().getPath() + "?" + request.getUrlParser().getQuery() : request.getUrlParser().getPath()).ssl(true);
        }
    }
    // 设置请求头header
    if (Preconditions.isNotBlank(header)) {
        for (Map.Entry<String, String> entry : header.entrySet()) {
            httpRequest.putHeader(entry.getKey(), entry.getValue());
        }
    }
    // 针对post请求,需要对header添加一些信息
    if (request.getHttpMethod() == HttpMethod.POST) {
        if (Preconditions.isNotBlank(request.getHttpRequestBody()) && Preconditions.isNotBlank(request.getHttpRequestBody().getContentType())) {
            httpRequest.putHeader("Content-type", request.getHttpRequestBody().getContentType());
        }
    }
    String charset = null;
    if (Preconditions.isNotBlank(request.getCharset())) {
        charset = request.getCharset();
    } else {
        charset = Constant.UTF_8;
    }
    HttpRequest<String> stringHttpRequest = httpRequest.as(BodyCodec.string(charset));
    Single<HttpResponse<String>> httpResponseSingle = null;
    if (request.getHttpMethod() == HttpMethod.GET) {
        httpResponseSingle = stringHttpRequest.rxSend();
    } else if (request.getHttpMethod() == HttpMethod.POST) {
        if (Preconditions.isNotBlank(request.getHttpRequestBody())) {
            Buffer buffer = Buffer.buffer();
            buffer.getDelegate().appendBytes(request.getHttpRequestBody().getBody());
            httpResponseSingle = stringHttpRequest.rxSendBuffer(buffer);
        } else {
            httpResponseSingle = stringHttpRequest.rxSend();
        }
    }
    return httpResponseSingle.toMaybe().map(new Function<HttpResponse<String>, Response>() {

        @Override
        public Response apply(HttpResponse<String> stringHttpResponse) throws Exception {
            String html = stringHttpResponse.body();
            Response response = new Response();
            response.setContent(html.getBytes());
            response.setStatusCode(stringHttpResponse.statusCode());
            response.setContentType(stringHttpResponse.getHeader("Content-Type"));
            if (request.isSaveCookie()) {
                // save cookies
                if (Preconditions.isNotBlank(stringHttpResponse.cookies())) {
                    CookieGroup cookieGroup = CookieManager.getInsatance().getCookieGroup(request.getUrlParser().getHost());
                    if (cookieGroup == null) {
                        cookieGroup = new CookieGroup(request.getUrlParser().getHost());
                        for (String cookieStr : stringHttpResponse.cookies()) {
                            String[] segs = cookieStr.split(";");
                            if (Preconditions.isNotBlank(segs)) {
                                for (String seg : segs) {
                                    String[] pairs = seg.trim().split("\\=");
                                    if (pairs.length == 2) {
                                        cookieSet.add(new Cookie(pairs[0], pairs[1]));
                                    }
                                }
                            }
                        }
                        cookieGroup.putAllCookies(cookieSet);
                        CookieManager.getInsatance().addCookieGroup(cookieGroup);
                    } else {
                        for (String cookieStr : stringHttpResponse.cookies()) {
                            String[] segs = cookieStr.split(";");
                            if (Preconditions.isNotBlank(segs)) {
                                for (String seg : segs) {
                                    String[] pairs = seg.trim().split("\\=");
                                    if (pairs.length == 2) {
                                        cookieSet.add(new Cookie(pairs[0], pairs[1]));
                                    }
                                }
                            }
                        }
                        cookieGroup.putAllCookies(cookieSet);
                    }
                }
            }
            return response;
        }
    });
}
Also used : Buffer(io.vertx.reactivex.core.buffer.Buffer) Cookie(com.cv4j.netdiscovery.core.cookies.Cookie) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) HttpResponse(io.vertx.reactivex.ext.web.client.HttpResponse) Response(com.cv4j.netdiscovery.core.domain.Response) CookieGroup(com.cv4j.netdiscovery.core.cookies.CookieGroup) Map(java.util.Map)

Aggregations

WebClientOptions (io.vertx.ext.web.client.WebClientOptions)7 JsonObject (io.vertx.core.json.JsonObject)2 WebClient (io.vertx.ext.web.client.WebClient)2 Router (io.vertx.rxjava.ext.web.Router)2 Cookie (com.cv4j.netdiscovery.core.cookies.Cookie)1 CookieGroup (com.cv4j.netdiscovery.core.cookies.CookieGroup)1 Response (com.cv4j.netdiscovery.core.domain.Response)1 CircuitBreakerOptions (io.vertx.circuitbreaker.CircuitBreakerOptions)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpServer (io.vertx.core.http.HttpServer)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 JksOptions (io.vertx.core.net.JksOptions)1 ProxyOptions (io.vertx.core.net.ProxyOptions)1 Router (io.vertx.ext.web.Router)1 Buffer (io.vertx.reactivex.core.buffer.Buffer)1 HttpResponse (io.vertx.reactivex.ext.web.client.HttpResponse)1 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)1 HttpClient (io.vertx.rxjava.core.http.HttpClient)1 JDBCClient (io.vertx.rxjava.ext.jdbc.JDBCClient)1