- logger.debug("DELETE uri = '{}''", uri);
- return client.delete() //
- .uri(uri) //
- .retrieve() //
- .onStatus(HttpStatus::isError,
- response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
- .bodyToMono(String.class);
+ return deleteForEntity(uri) //
+ .flatMap(this::toBody);
+ }
+
+ private Mono<ResponseEntity<String>> retrieve(RequestHeadersSpec<?> request) {
+ return request.retrieve() //
+ .toEntity(String.class) //
+ .doOnError(this::onHttpError);
+ }
+
+ private void onHttpError(Throwable t) {
+ if (t instanceof WebClientResponseException) {
+ WebClientResponseException exception = (WebClientResponseException) t;
+ logger.debug("HTTP error status = '{}', body '{}'", exception.getStatusCode(),
+ exception.getResponseBodyAsString());
+ } else {
+ logger.debug("HTTP error: {}", t.getMessage());
+ }
+ }
+
+ private Mono<String> toBody(ResponseEntity<String> entity) {
+ if (entity.getBody() == null) {
+ return Mono.just("");
+ } else {
+ return Mono.just(entity.getBody());
+ }
+ }
+
+ private static SslContext createSslContext() throws SSLException {
+ return SslContextBuilder.forClient() //
+ .trustManager(InsecureTrustManagerFactory.INSTANCE) //
+ .build();
+ }
+
+ private static WebClient createWebClient(String baseUrl, SslContext sslContext) {
+ TcpClient tcpClient = TcpClient.create() //
+ .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000) //
+ .secure(c -> c.sslContext(sslContext)) //
+ .doOnConnected(connection -> {
+ connection.addHandler(new ReadTimeoutHandler(10));
+ connection.addHandler(new WriteTimeoutHandler(30));
+ });
+ HttpClient httpClient = HttpClient.from(tcpClient);
+ ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
+
+ return WebClient.builder() //
+ .clientConnector(connector) //
+ .baseUrl(baseUrl) //
+ .build();