diff --git a/src/main/java/eu/openaire/urls_controller/Application.java b/src/main/java/eu/openaire/urls_controller/Application.java index 2fdf215..a122eb6 100644 --- a/src/main/java/eu/openaire/urls_controller/Application.java +++ b/src/main/java/eu/openaire/urls_controller/Application.java @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import org.springframework.scheduling.annotation.EnableScheduling; @@ -64,8 +65,9 @@ public class Application { @Bean - public CommandLineRunner setServerBaseUrl(Environment environment) { - return args -> new UriBuilder(environment); + public CommandLineRunner setServerBaseUrl(Environment environment, ServletWebServerApplicationContext webServerAppCtxt) + { + return args -> new UriBuilder(environment, webServerAppCtxt); } } \ No newline at end of file diff --git a/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java b/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java index 329dc11..7d2e557 100644 --- a/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java +++ b/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java @@ -3,6 +3,7 @@ package eu.openaire.urls_controller.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; import org.springframework.core.env.Environment; import java.io.BufferedReader; @@ -16,10 +17,12 @@ public class UriBuilder { private static final Logger logger = LoggerFactory.getLogger(UriBuilder.class); + public static String ip = null; public static String baseUrl = null; - public UriBuilder(Environment environment) { + public UriBuilder(Environment environment, ServletWebServerApplicationContext webServerAppCtxt) { baseUrl = "http"; + String sslEnabled = environment.getProperty("server.ssl.enabled"); if (sslEnabled == null) { // It's expected to not exist if there is no SSL-configuration. logger.warn("No property \"server.ssl.enabled\" was found in \"application.properties\". Continuing with plain HTTP.."); @@ -28,17 +31,11 @@ public class UriBuilder { baseUrl += sslEnabled.equals("true") ? "s" : ""; baseUrl += "://"; - String hostName = getPublicIP(); - if ( hostName == null ) - hostName = InetAddress.getLoopbackAddress().getHostName(); // Non-null. + ip = getPublicIP(); + if ( ip == null ) + ip = InetAddress.getLoopbackAddress().getHostAddress(); // Non-null. - baseUrl += hostName; - String serverPort = environment.getProperty("server.port"); - if (serverPort == null) { // This is unacceptable! - logger.error("No property \"server.port\" was found in \"application.properties\"!"); - System.exit(-1); // Well, I guess the Spring Boot would not start in this case anyway. - } - baseUrl += ":" + serverPort; + baseUrl += ip + ":" + webServerAppCtxt.getWebServer().getPort(); String baseInternalPath = environment.getProperty("server.servlet.context-path"); if ( baseInternalPath != null ) { @@ -55,7 +52,7 @@ public class UriBuilder { logger.debug("ServerBaseURL: " + baseUrl); } - private String getPublicIP() + private static String getPublicIP() { String publicIpAddress = ""; URL url_name; @@ -82,4 +79,4 @@ public class UriBuilder { UriBuilder.baseUrl = baseUrl; } -} \ No newline at end of file +}