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