Update the "UriBuilder.java" to be able to acquire the running port of the server, in case the port-number was initially set to "random" (0). Also make sure we get the "localHostAddress" and not the "localHostName", in case the public IP is not retrievable.

This commit is contained in:
Lampros Smyrnaios 2022-09-12 17:04:05 +03:00
parent a2cd02115f
commit 3e8f9c6074
2 changed files with 14 additions and 15 deletions

View File

@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
@ -64,8 +65,9 @@ public class Application {
@Bean @Bean
public CommandLineRunner setServerBaseUrl(Environment environment) { public CommandLineRunner setServerBaseUrl(Environment environment, ServletWebServerApplicationContext webServerAppCtxt)
return args -> new UriBuilder(environment); {
return args -> new UriBuilder(environment, webServerAppCtxt);
} }
} }

View File

@ -3,6 +3,7 @@ package eu.openaire.urls_controller.util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -16,10 +17,12 @@ public class UriBuilder {
private static final Logger logger = LoggerFactory.getLogger(UriBuilder.class); private static final Logger logger = LoggerFactory.getLogger(UriBuilder.class);
public static String ip = null;
public static String baseUrl = null; public static String baseUrl = null;
public UriBuilder(Environment environment) { public UriBuilder(Environment environment, ServletWebServerApplicationContext webServerAppCtxt) {
baseUrl = "http"; baseUrl = "http";
String sslEnabled = environment.getProperty("server.ssl.enabled"); String sslEnabled = environment.getProperty("server.ssl.enabled");
if (sslEnabled == null) { // It's expected to not exist if there is no SSL-configuration. 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.."); 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 += sslEnabled.equals("true") ? "s" : "";
baseUrl += "://"; baseUrl += "://";
String hostName = getPublicIP(); ip = getPublicIP();
if ( hostName == null ) if ( ip == null )
hostName = InetAddress.getLoopbackAddress().getHostName(); // Non-null. ip = InetAddress.getLoopbackAddress().getHostAddress(); // Non-null.
baseUrl += hostName; baseUrl += ip + ":" + webServerAppCtxt.getWebServer().getPort();
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;
String baseInternalPath = environment.getProperty("server.servlet.context-path"); String baseInternalPath = environment.getProperty("server.servlet.context-path");
if ( baseInternalPath != null ) { if ( baseInternalPath != null ) {
@ -55,7 +52,7 @@ public class UriBuilder {
logger.debug("ServerBaseURL: " + baseUrl); logger.debug("ServerBaseURL: " + baseUrl);
} }
private String getPublicIP() private static String getPublicIP()
{ {
String publicIpAddress = ""; String publicIpAddress = "";
URL url_name; URL url_name;