From ec2552b256472beac7fd7b89836b0f9950559d3a Mon Sep 17 00:00:00 2001 From: Jesus Date: Tue, 15 Nov 2022 22:52:58 +0100 Subject: [PATCH] Now we can get data from user and server --- .../com/jesuspinar/webserver/Manager.java | 47 +++++++++++++++++-- .../resources/templates/{ => files}/time.html | 2 +- .../resources/templates/files/whoami.html | 23 +++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) rename src/main/resources/templates/{ => files}/time.html (89%) create mode 100644 src/main/resources/templates/files/whoami.html diff --git a/src/main/java/com/jesuspinar/webserver/Manager.java b/src/main/java/com/jesuspinar/webserver/Manager.java index 7a3335c..b1ffff3 100644 --- a/src/main/java/com/jesuspinar/webserver/Manager.java +++ b/src/main/java/com/jesuspinar/webserver/Manager.java @@ -3,23 +3,60 @@ package com.jesuspinar.webserver; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.ui.Model; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; +import java.net.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @Controller public class Manager { @GetMapping({"/", "", "/index"}) - public String welcome(Model model){ + public String welcome(Model model) { loadTheme(model); model.addAttribute("mesage", "Hello"); return "index"; } - @GetMapping("/hour") - public String hour(Model model){ + @GetMapping("/time") + public String hour(Model model) { loadTheme(model); - return "time"; + return "files/time"; + } + + @GetMapping("/whoami") + public String test(Model model) throws MalformedURLException { + loadTheme(model); + + InetAddress inetAddress = null; + String hostnameCanonical = null; + String hostname = null; + byte[] address = null; + + String currentURL = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString(); + URL requestURL = new URL(currentURL); + + try { + inetAddress = InetAddress.getByName(InetAddress.getLocalHost().getHostName()); + hostnameCanonical = inetAddress.getCanonicalHostName(); + address = inetAddress.getAddress(); + hostname = inetAddress.getHostName(); + } catch (UnknownHostException e) { + model.addAttribute("error", "UnknownHostException"); + } + //Data from the user + model.addAttribute("inetAddress", inetAddress); + model.addAttribute("hostname", hostname); + model.addAttribute("hostnameCanonical", hostnameCanonical); + model.addAttribute("address", address); + //Data from the server + model.addAttribute("url", currentURL); + model.addAttribute("protocol", requestURL.getProtocol()); + //ToDo: split host info in a class + model.addAttribute("host", requestURL.getHost()); + model.addAttribute("port", requestURL.getPort()); + + return "files/whoami"; } private void loadTheme(Model model) { @@ -28,7 +65,7 @@ public class Manager { DateTimeFormatter hour = DateTimeFormatter.ofPattern("HH"); int h = Integer.parseInt(hour.format(LocalDateTime.now())); String bg = "light"; - if (h >= 20 || h <= 7){ + if (h >= 20 || h <= 7) { bg = "dark"; } model.addAttribute("background", bg); diff --git a/src/main/resources/templates/time.html b/src/main/resources/templates/files/time.html similarity index 89% rename from src/main/resources/templates/time.html rename to src/main/resources/templates/files/time.html index 66b807f..31fe9d7 100644 --- a/src/main/resources/templates/time.html +++ b/src/main/resources/templates/files/time.html @@ -10,6 +10,6 @@

The actual date is :

-

+

\ No newline at end of file diff --git a/src/main/resources/templates/files/whoami.html b/src/main/resources/templates/files/whoami.html new file mode 100644 index 0000000..a2937bc --- /dev/null +++ b/src/main/resources/templates/files/whoami.html @@ -0,0 +1,23 @@ + + + + + + + + + + +

User data

+

Internet Address:

+

Hostname:

+

Canonical Hostname:

+

Address in bytes:

+ +

Server data

+

URL:

+

Protocol:

+

Host:

+

Port:

+ + \ No newline at end of file