Implemented a service, that fetches data from a remote-host
[demos/testing] / src / main / java / de / juplo / demo / RemoteContentService.java
1 package de.juplo.demo;
2
3 import org.springframework.web.reactive.function.client.WebClient;
4 import reactor.core.publisher.Mono;
5
6
7 /**
8  * Fetches data from remote-webserver.
9  * @author Kai Moritz
10  */
11 public class RemoteContentService
12 {
13   WebClient webClient;
14
15
16   /**
17    * The {@link WebClient}, that is used to fetch the data.
18    * <p>
19    * The <code>WebClient</code> has to be configured to us a given
20    * remote-server by default. This service will only add a path.
21    * @param webClient a <code>WebClient</code> instance with configured host
22    */
23   public RemoteContentService(WebClient webClient)
24   {
25     this.webClient = webClient;
26   }
27
28
29   /**
30    * Fetches the given path from the configured remote-server.
31    * @param path the path to fetch from the configured remote-server
32    * @return a {@link Mono}, that represents the fetched data
33    */
34   public Mono<String> getRemoteText(String path)
35   {
36     return
37         webClient
38             .get()
39             .uri(path)
40             .retrieve()
41             .bodyToMono(String.class);
42   }
43 }