2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent.clients;
23 import io.netty.channel.ChannelOption;
24 import io.netty.handler.timeout.ReadTimeoutHandler;
25 import io.netty.handler.timeout.WriteTimeoutHandler;
27 import java.lang.invoke.MethodHandles;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.http.MediaType;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
34 import org.springframework.lang.Nullable;
35 import org.springframework.web.reactive.function.client.WebClient;
36 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
38 import reactor.core.publisher.Mono;
39 import reactor.netty.http.client.HttpClient;
40 import reactor.netty.tcp.TcpClient;
43 * Generic reactive REST client.
45 public class AsyncRestClient {
46 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47 private final WebClient client;
48 private final String baseUrl;
50 public AsyncRestClient(String baseUrl) {
52 TcpClient tcpClient = TcpClient.create() //
53 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000) //
54 .doOnConnected(connection -> {
55 connection.addHandler(new ReadTimeoutHandler(10));
56 connection.addHandler(new WriteTimeoutHandler(30));
58 HttpClient httpClient = HttpClient.from(tcpClient);
59 ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
61 this.client = WebClient.builder() //
62 .clientConnector(connector) //
66 this.baseUrl = baseUrl;
69 public Mono<ResponseEntity<String>> postForEntity(String uri, @Nullable String body) {
70 logger.debug("POST uri = '{}{}''", baseUrl, uri);
71 Mono<String> bodyProducer = body != null ? Mono.just(body) : Mono.empty();
72 RequestHeadersSpec<?> request = client.post() //
74 .contentType(MediaType.APPLICATION_JSON) //
75 .body(bodyProducer, String.class);
76 return retrieve(request);
79 public Mono<String> post(String uri, @Nullable String body) {
80 return postForEntity(uri, body) //
81 .flatMap(this::toBody);
84 public Mono<String> postWithAuthHeader(String uri, String body, String username, String password) {
85 logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri);
86 RequestHeadersSpec<?> request = client.post() //
88 .headers(headers -> headers.setBasicAuth(username, password)) //
89 .contentType(MediaType.APPLICATION_JSON) //
91 return retrieve(request) //
92 .flatMap(this::toBody);
95 public Mono<ResponseEntity<String>> putForEntity(String uri, String body) {
96 logger.debug("PUT uri = '{}{}''", baseUrl, uri);
97 RequestHeadersSpec<?> request = client.put() //
99 .contentType(MediaType.APPLICATION_JSON) //
101 return retrieve(request);
104 public Mono<ResponseEntity<String>> putForEntity(String uri) {
105 logger.debug("PUT uri = '{}{}''", baseUrl, uri);
106 RequestHeadersSpec<?> request = client.put() //
108 return retrieve(request);
111 public Mono<String> put(String uri, String body) {
112 return putForEntity(uri, body) //
113 .flatMap(this::toBody);
116 public Mono<ResponseEntity<String>> getForEntity(String uri) {
117 logger.debug("GET uri = '{}{}''", baseUrl, uri);
118 RequestHeadersSpec<?> request = client.get().uri(uri);
119 return retrieve(request);
122 public Mono<String> get(String uri) {
123 return getForEntity(uri) //
124 .flatMap(this::toBody);
127 public Mono<ResponseEntity<String>> deleteForEntity(String uri) {
128 logger.debug("DELETE uri = '{}{}''", baseUrl, uri);
129 RequestHeadersSpec<?> request = client.delete().uri(uri);
130 return retrieve(request);
133 public Mono<String> delete(String uri) {
134 return deleteForEntity(uri) //
135 .flatMap(this::toBody);
138 private Mono<ResponseEntity<String>> retrieve(RequestHeadersSpec<?> request) {
139 return request.retrieve() //
140 .toEntity(String.class);
143 Mono<String> toBody(ResponseEntity<String> entity) {
144 if (entity.getBody() == null) {
145 return Mono.just("");
147 return Mono.just(entity.getBody());