forked from lsmyrnaios/UrlsController
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:
parent
a2cd02115f
commit
3e8f9c6074
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue