Implemented a service, that fetches data from a remote-host
[demos/testing] / src / main / java / de / juplo / demo / RemoteContentService.java
diff --git a/src/main/java/de/juplo/demo/RemoteContentService.java b/src/main/java/de/juplo/demo/RemoteContentService.java
new file mode 100644 (file)
index 0000000..c62225e
--- /dev/null
@@ -0,0 +1,43 @@
+package de.juplo.demo;
+
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
+
+
+/**
+ * Fetches data from remote-webserver.
+ * @author Kai Moritz
+ */
+public class RemoteContentService
+{
+  WebClient webClient;
+
+
+  /**
+   * The {@link WebClient}, that is used to fetch the data.
+   * <p>
+   * The <code>WebClient</code> has to be configured to us a given
+   * remote-server by default. This service will only add a path.
+   * @param webClient a <code>WebClient</code> instance with configured host
+   */
+  public RemoteContentService(WebClient webClient)
+  {
+    this.webClient = webClient;
+  }
+
+
+  /**
+   * Fetches the given path from the configured remote-server.
+   * @param path the path to fetch from the configured remote-server
+   * @return a {@link Mono}, that represents the fetched data
+   */
+  public Mono<String> getRemoteText(String path)
+  {
+    return
+        webClient
+            .get()
+            .uri(path)
+            .retrieve()
+            .bodyToMono(String.class);
+  }
+}