X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FAsyncRestClient.java;h=f0b2ce339ff44daa588d7faa0f7d8bdb324bde24;hb=d375c04a9c1917a9bf98285fb1f30062cff7a6d6;hp=0d27bd6ccf4d75528bdbcde46a964122e99a2914;hpb=7c297ddb425a52dae965adc6a83629a14421ea05;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java index 0d27bd6c..f0b2ce33 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java @@ -20,88 +20,144 @@ package org.oransc.policyagent.clients; +import io.netty.channel.ChannelOption; +import io.netty.handler.timeout.ReadTimeoutHandler; +import io.netty.handler.timeout.WriteTimeoutHandler; + import java.lang.invoke.MethodHandles; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.lang.Nullable; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec; +import org.springframework.web.reactive.function.client.WebClientResponseException; + import reactor.core.publisher.Mono; +import reactor.netty.http.client.HttpClient; +import reactor.netty.tcp.TcpClient; +/** + * Generic reactive REST client. + */ public class AsyncRestClient { private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final WebClient client; private final String baseUrl; - public class AsyncRestClientException extends Exception { + public AsyncRestClient(String baseUrl) { - private static final long serialVersionUID = 1L; + TcpClient tcpClient = TcpClient.create() // + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000) // + .doOnConnected(connection -> { + connection.addHandler(new ReadTimeoutHandler(10)); + connection.addHandler(new WriteTimeoutHandler(30)); + }); + HttpClient httpClient = HttpClient.from(tcpClient); + ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient); - public AsyncRestClientException(String message) { - super(message); - } - } + this.client = WebClient.builder() // + .clientConnector(connector) // + .baseUrl(baseUrl) // + .build(); - public AsyncRestClient(String baseUrl) { - this.client = WebClient.create(baseUrl); this.baseUrl = baseUrl; } - public Mono post(String uri, String body) { + public Mono> postForEntity(String uri, @Nullable String body) { logger.debug("POST uri = '{}{}''", baseUrl, uri); - return client.post() // + Mono bodyProducer = body != null ? Mono.just(body) : Mono.empty(); + RequestHeadersSpec request = client.post() // .uri(uri) // .contentType(MediaType.APPLICATION_JSON) // - .bodyValue(body) // - .retrieve() // - .onStatus(HttpStatus::isError, - response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // - .bodyToMono(String.class); + .body(bodyProducer, String.class); + return retrieve(request); + } + + public Mono post(String uri, @Nullable String body) { + return postForEntity(uri, body) // + .flatMap(this::toBody); } public Mono postWithAuthHeader(String uri, String body, String username, String password) { logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri); - return client.post() // + RequestHeadersSpec request = client.post() // .uri(uri) // .headers(headers -> headers.setBasicAuth(username, password)) // .contentType(MediaType.APPLICATION_JSON) // - .bodyValue(body) // - .retrieve() // - .onStatus(HttpStatus::isError, - response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // - .bodyToMono(String.class); + .bodyValue(body); + return retrieve(request) // + .flatMap(this::toBody); } - public Mono put(String uri, String body) { + public Mono> putForEntity(String uri, String body) { logger.debug("PUT uri = '{}{}''", baseUrl, uri); - return client.put() // + RequestHeadersSpec request = client.put() // .uri(uri) // .contentType(MediaType.APPLICATION_JSON) // - .bodyValue(body) // - .retrieve() // - .onStatus(HttpStatus::isError, - response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // - .bodyToMono(String.class); + .bodyValue(body); + return retrieve(request); } - public Mono get(String uri) { + public Mono> putForEntity(String uri) { + logger.debug("PUT uri = '{}{}''", baseUrl, uri); + RequestHeadersSpec request = client.put() // + .uri(uri); + return retrieve(request); + } + + public Mono put(String uri, String body) { + return putForEntity(uri, body) // + .flatMap(this::toBody); + } + + public Mono> getForEntity(String uri) { logger.debug("GET uri = '{}{}''", baseUrl, uri); - return client.get() // - .uri(uri) // - .retrieve() // - .onStatus(HttpStatus::isError, - response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // - .bodyToMono(String.class); + RequestHeadersSpec request = client.get().uri(uri); + return retrieve(request); } - public Mono delete(String uri) { + public Mono get(String uri) { + return getForEntity(uri) // + .flatMap(this::toBody); + } + + public Mono> deleteForEntity(String uri) { logger.debug("DELETE uri = '{}{}''", baseUrl, uri); - return client.delete() // - .uri(uri) // - .retrieve() // - .onStatus(HttpStatus::isError, - response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // - .bodyToMono(String.class); + RequestHeadersSpec request = client.delete().uri(uri); + return retrieve(request); } + + public Mono delete(String uri) { + return deleteForEntity(uri) // + .flatMap(this::toBody); + } + + private Mono> 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 toBody(ResponseEntity entity) { + if (entity.getBody() == null) { + return Mono.just(""); + } else { + return Mono.just(entity.getBody()); + } + } + }