WIP:service
authorKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 08:52:21 +0000 (09:52 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 11 Jan 2020 08:54:53 +0000 (09:54 +0100)
src/main/java/de/juplo/demo/DemoApplication.java
src/main/java/de/juplo/demo/RemoteContentController.java
src/main/java/de/juplo/demo/RemoteContentService.java [new file with mode: 0644]
src/main/resources/templates/layout.html [deleted file]

index 53303c5..88b890c 100644 (file)
@@ -4,15 +4,15 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.annotation.Bean;
+import org.springframework.web.reactive.function.client.WebClient;
 
 @SpringBootApplication
 public class DemoApplication {
 
   @Bean
-  public RemoteContentController remoteContentController(
-      @Value("${remote.host}")String remoteHost)
+  public RemoteContentService service(@Value("${remote.host}")String remoteHost)
   {
-    return new RemoteContentController(remoteHost);
+    return new RemoteContentService(WebClient.create(remoteHost));
   }
 
 
index fa0e49a..00b0b96 100644 (file)
@@ -1,41 +1,26 @@
 package de.juplo.demo;
 
-
-<<<<<<< HEAD:src/main/java/de/juplo/demo/RemoteContentController.java
-=======
->>>>>>> f9152f7... WIP: Implemented...:src/main/java/de/juplo/integrationtest/RemoteContentController.java
-import org.springframework.ui.Model;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Mono;
 
 
 /**
  * Fetches data from remote-webserver and renders them as HTML.
  * @author Kai Moritz
  */
+@RestController
 public class RemoteContentController
 {
-  WebClient webClient;
-
-
-  public RemoteContentController(WebClient webClient)
-  {
-    this.webClient = webClient;
-  }
+  @Autowired
+  RemoteContentService service;
 
 
   @GetMapping("/")
-  public String renderRemoteText(Model model, @RequestParam String path)
+  public Mono<String> renderRemoteText(@RequestParam String path)
   {
-    model.addAttribute(
-        "text",
-        webClient
-            .get()
-            .uri(path)
-            .retrieve()
-            .bodyToMono(String.class).block());
-
-    return "layout";
+    return service.getRemoteText(path);
   }
 }
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);
+  }
+}
diff --git a/src/main/resources/templates/layout.html b/src/main/resources/templates/layout.html
deleted file mode 100644 (file)
index 1087568..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<!DOCTYPE HTML>
-<html xmlns:th="http://www.thymeleaf.org">
-<head>
-  <title>Shows Remote-Content</title>
-  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-</head>
-<body>
-  <h1>Remote-Content:</h1>
-  <p th:text="${text}">TEXT</p>
-</body>
-</html>