|
|
|
@ -5,7 +5,11 @@ import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
|
|
public class UriBuilder {
|
|
|
|
|
|
|
|
|
@ -24,7 +28,10 @@ public class UriBuilder {
|
|
|
|
|
baseUrl += sslEnabled.equals("true") ? "s" : "";
|
|
|
|
|
baseUrl += "://";
|
|
|
|
|
|
|
|
|
|
String hostName = InetAddress.getLoopbackAddress().getHostName(); // Non-null.
|
|
|
|
|
String hostName = getPublicIP();
|
|
|
|
|
if ( hostName == null )
|
|
|
|
|
hostName = InetAddress.getLoopbackAddress().getHostName(); // Non-null.
|
|
|
|
|
|
|
|
|
|
baseUrl += hostName;
|
|
|
|
|
String serverPort = environment.getProperty("server.port");
|
|
|
|
|
if (serverPort == null) { // This is unacceptable!
|
|
|
|
@ -34,11 +41,11 @@ public class UriBuilder {
|
|
|
|
|
baseUrl += ":" + serverPort;
|
|
|
|
|
|
|
|
|
|
String baseInternalPath = environment.getProperty("server.servlet.context-path");
|
|
|
|
|
if (baseInternalPath != null) {
|
|
|
|
|
if (!baseInternalPath.startsWith("/"))
|
|
|
|
|
if ( baseInternalPath != null ) {
|
|
|
|
|
if ( !baseInternalPath.startsWith("/") )
|
|
|
|
|
baseUrl += "/";
|
|
|
|
|
baseUrl += baseInternalPath;
|
|
|
|
|
if (!baseInternalPath.endsWith("/"))
|
|
|
|
|
if ( !baseInternalPath.endsWith("/") )
|
|
|
|
|
baseUrl += "/";
|
|
|
|
|
} else {
|
|
|
|
|
logger.warn("No property \"server.servlet.context-path\" was found in \"application.properties\"!"); // Yes it's expected.
|
|
|
|
@ -48,6 +55,25 @@ public class UriBuilder {
|
|
|
|
|
logger.debug("ServerBaseURL: " + baseUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getPublicIP()
|
|
|
|
|
{
|
|
|
|
|
String publicIpAddress = "";
|
|
|
|
|
URL url_name;
|
|
|
|
|
try {
|
|
|
|
|
url_name = new URL("https://api.ipify.org/");
|
|
|
|
|
} catch (MalformedURLException mue) {
|
|
|
|
|
logger.warn(mue.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
try (BufferedReader bf = new BufferedReader(new InputStreamReader(url_name.openStream()))) {
|
|
|
|
|
publicIpAddress = bf.readLine().trim();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
logger.warn("Cannot get the publicIP address for this machine!", e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return publicIpAddress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getBaseUrl() {
|
|
|
|
|
return baseUrl;
|
|
|
|
|
}
|
|
|
|
|