private HttpClient(HttpClient.Builder builder) { HttpClientOptions.Builder clientOptionsBuilder = HttpClientOptions.builder(); if (Objects.nonNull(builder.options)) { builder.options.accept(clientOptionsBuilder); } if (!clientOptionsBuilder.isLoopAvailable()) { clientOptionsBuilder.loopResources(HttpResources.get()); } if (!clientOptionsBuilder.isPoolAvailable() && !clientOptionsBuilder.isPoolDisabled()) { clientOptionsBuilder.poolResources(HttpResources.get()); } this.options = clientOptionsBuilder.build(); this.client = new TcpBridgeClient(options); }
/** * The HTTP proxy configuration * * @param proxyOptions the HTTP proxy configuration * @return {@code this} */ public final Builder httpProxy(Function<ClientProxyOptions.AddressSpec, ClientProxyOptions.Builder> proxyOptions) { super.proxy(t -> proxyOptions.apply(t.type(Proxy.HTTP))); return get(); }
@Test public void hoverflyProxy() { Mono<HttpClientResponse> remote = HttpClient.create(o -> o.httpProxy(ops -> ops .address(new InetSocketAddress("localhost", hoverflyRule.getProxyPort())) )) .get("http://www.my-test.com/api/bookings/1", HttpClientRequest::sendHeaders); Mono<String> page = remote .flatMapMany(r -> r.receive() .retain() .asString() .limitRate(1)) .reduce(String::concat); StepVerifier.create(page) .expectNextMatches(s -> s.contains("bookingId")) .expectComplete() .verify(Duration.ofSeconds(30)); } }
/** * Sets request options using Feign {@link Request.Options} * * @param options Feign {@code Request.Options} object * @return this builder */ public Builder<T> options(final ReactiveOptions options) { if (!options.isEmpty()) { ReactorClientHttpConnector connector = new ReactorClientHttpConnector( opts -> { if (options.getConnectTimeoutMillis() != null) { opts.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeoutMillis()); } if (options.getReadTimeoutMillis() != null) { opts.afterNettyContextInit(ctx -> { ctx.addHandlerLast(new ReadTimeoutHandler( options.getReadTimeoutMillis(), TimeUnit.MILLISECONDS)); }); } if (options.isTryUseCompression() != null) { opts.compression(options.isTryUseCompression()); } }); this.webClient = webClient.mutate().clientConnector(connector).build(); } return this; }
@Override public Builder<T> options(ReactiveOptions options) { if (!options.isEmpty()) { ReactorClientHttpConnector connector = new ReactorClientHttpConnector( opts -> { if (options.getConnectTimeoutMillis() != null) { opts.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, options.getConnectTimeoutMillis().intValue()); } if (options.getReadTimeoutMillis() != null) { opts.afterNettyContextInit(ctx -> { ctx.addHandlerLast(new ReadTimeoutHandler( options.getReadTimeoutMillis(), TimeUnit.MILLISECONDS)); }); } if (options.isTryUseCompression() != null) { opts.compression(options.isTryUseCompression()); } }); setWebClient(webClient.mutate().clientConnector(connector).build()); } return this; }
/** * Fill the builder with attribute values from the provided options. * * @param options The instance from which to copy values * @return {@code this} */ public final Builder from(HttpClientOptions options) { super.from(options); this.acceptGzip = options.acceptGzip; return get(); }
/** * Enable GZip accept-encoding header and support for compressed response * * @param enabled true whether gzip support is enabled * @return {@code this} */ public final Builder compression(boolean enabled) { this.acceptGzip = enabled; return get(); }
/** * Create a new HttpClientOptions.Builder * * @return a new HttpClientOptions.Builder */ @SuppressWarnings("unchecked") public static HttpClientOptions.Builder builder() { return new HttpClientOptions.Builder(); }
/** * Creates a simple HTTP client bound on the provided address and port * * @param address the host to which this client should connect. * @param port the port to which this client should connect. * @return a simple HTTP client bound on the provided address and port */ public static HttpClient create(String address, int port) { return create(opts -> opts.host(address).port(port)); }