Added a rest-controller, that serves the fetched data as text/plain
[demos/testing] / src / main / java / de / juplo / demo / HtmlController.java
1 package de.juplo.demo;
2
3
4 import org.springframework.http.MediaType;
5 import org.springframework.stereotype.Controller;
6 import org.springframework.ui.Model;
7 import org.springframework.web.bind.annotation.GetMapping;
8 import org.springframework.web.bind.annotation.RequestParam;
9 import reactor.core.publisher.Mono;
10
11
12 /**
13  * Fetches data from a remote-webserver and renders it as HTML.
14  * @author Kai Moritz
15  */
16 @Controller
17 public class HtmlController
18 {
19   RemoteContentService service;
20
21
22   public HtmlController(RemoteContentService service)
23   {
24     this.service = service;
25   }
26
27
28   @GetMapping(path = { "", "/" }, produces = MediaType.TEXT_HTML_VALUE)
29   public String fetch(Model model, @RequestParam(required = false) String path)
30   {
31     model.addAttribute("path", path);
32     model.addAttribute(
33         "text",
34         path == null
35             ? ""
36             : service
37                   .getRemoteText(path)
38                   .onErrorResume(t -> Mono.just(t.getMessage())));
39     return "home";
40   }
41 }