Bugfix, no response sent to Dmaap for PUT
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / AsyncRestClient.java
index 3fd16d0..12c0745 100644 (file)
@@ -17,6 +17,7 @@
  * limitations under the License.
  * ========================LICENSE_END===================================
  */
+
 package org.oransc.policyagent.clients;
 
 import java.lang.invoke.MethodHandles;
@@ -31,8 +32,9 @@ import reactor.core.publisher.Mono;
 public class AsyncRestClient {
     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
     private final WebClient client;
+    private final String baseUrl;
 
-    private static class AsyncRestClientException extends Exception {
+    public class AsyncRestClientException extends Exception {
 
         private static final long serialVersionUID = 1L;
 
@@ -43,21 +45,38 @@ public class AsyncRestClient {
 
     public AsyncRestClient(String baseUrl) {
         this.client = WebClient.create(baseUrl);
+        this.baseUrl = baseUrl;
     }
 
     public Mono<String> post(String uri, String body) {
+        logger.debug("POST uri = '{}{}''", baseUrl, uri);
+        return 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) //
+            .defaultIfEmpty("");
+    }
+
+    public Mono<String> postWithAuthHeader(String uri, String body, String username, String password) {
+        logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri);
         return 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);
+            .bodyToMono(String.class) //
+            .defaultIfEmpty("");
     }
 
     public Mono<String> put(String uri, String body) {
-        logger.debug("PUT uri = '{}''", uri);
+        logger.debug("PUT uri = '{}{}''", baseUrl, uri);
         return client.put() //
             .uri(uri) //
             .contentType(MediaType.APPLICATION_JSON) //
@@ -65,26 +84,29 @@ public class AsyncRestClient {
             .retrieve() //
             .onStatus(HttpStatus::isError,
                 response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
-            .bodyToMono(String.class);
+            .bodyToMono(String.class) //
+            .defaultIfEmpty("");
     }
 
     public Mono<String> get(String uri) {
-        logger.debug("GET uri = '{}''", 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);
+            .bodyToMono(String.class) //
+            .defaultIfEmpty("");
     }
 
     public Mono<String> delete(String uri) {
-        logger.debug("DELETE uri = '{}''", 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);
+            .bodyToMono(String.class) //
+            .defaultIfEmpty("");
     }
 }