2021-03-16 14:25:15 +01:00
|
|
|
package eu.openaire.urls_controller.util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2022-09-12 16:04:05 +02:00
|
|
|
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
|
2021-03-16 14:25:15 +01:00
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
2021-12-06 19:27:39 +01:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
2023-01-03 17:39:50 +01:00
|
|
|
import java.net.HttpURLConnection;
|
2021-03-16 14:25:15 +01:00
|
|
|
import java.net.InetAddress;
|
2021-12-06 19:27:39 +01:00
|
|
|
import java.net.URL;
|
2021-03-16 14:25:15 +01:00
|
|
|
|
2022-02-22 12:54:16 +01:00
|
|
|
|
2021-03-16 14:25:15 +01:00
|
|
|
public class UriBuilder {
|
|
|
|
|
2022-02-22 12:54:16 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(UriBuilder.class);
|
|
|
|
|
2022-09-12 16:04:05 +02:00
|
|
|
public static String ip = null;
|
2022-02-22 12:54:16 +01:00
|
|
|
public static String baseUrl = null;
|
|
|
|
|
2022-09-12 16:04:05 +02:00
|
|
|
public UriBuilder(Environment environment, ServletWebServerApplicationContext webServerAppCtxt) {
|
2022-02-22 12:54:16 +01:00
|
|
|
baseUrl = "http";
|
2022-09-12 16:04:05 +02:00
|
|
|
|
2022-02-22 12:54:16 +01:00
|
|
|
String sslEnabled = environment.getProperty("server.ssl.enabled");
|
|
|
|
if (sslEnabled == null) { // It's expected to not exist if there is no SSL-configuration.
|
2023-03-21 16:04:28 +01:00
|
|
|
logger.warn("No property \"server.ssl.enabled\" was found in \"application.yml\". Continuing with plain HTTP..");
|
2022-02-22 12:54:16 +01:00
|
|
|
sslEnabled = "false";
|
|
|
|
}
|
|
|
|
baseUrl += sslEnabled.equals("true") ? "s" : "";
|
|
|
|
baseUrl += "://";
|
|
|
|
|
2023-04-26 18:33:28 +02:00
|
|
|
if ( (ip = getPublicIP()) == null )
|
2022-09-12 16:04:05 +02:00
|
|
|
ip = InetAddress.getLoopbackAddress().getHostAddress(); // Non-null.
|
2022-02-22 12:54:16 +01:00
|
|
|
|
2022-09-12 16:04:05 +02:00
|
|
|
baseUrl += ip + ":" + webServerAppCtxt.getWebServer().getPort();
|
2022-02-22 12:54:16 +01:00
|
|
|
|
|
|
|
String baseInternalPath = environment.getProperty("server.servlet.context-path");
|
|
|
|
if ( baseInternalPath != null ) {
|
|
|
|
if ( !baseInternalPath.startsWith("/") )
|
|
|
|
baseUrl += "/";
|
|
|
|
baseUrl += baseInternalPath;
|
|
|
|
if ( !baseInternalPath.endsWith("/") )
|
|
|
|
baseUrl += "/";
|
|
|
|
} else {
|
2023-03-21 16:04:28 +01:00
|
|
|
logger.warn("No property \"server.servlet.context-path\" was found in \"application.yml\"!"); // Yes it's expected.
|
2022-02-22 12:54:16 +01:00
|
|
|
baseUrl += "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("ServerBaseURL: " + baseUrl);
|
|
|
|
}
|
|
|
|
|
2022-09-12 16:04:05 +02:00
|
|
|
private static String getPublicIP()
|
2022-02-22 12:54:16 +01:00
|
|
|
{
|
|
|
|
String publicIpAddress = "";
|
2023-01-03 17:39:50 +01:00
|
|
|
HttpURLConnection conn = null;
|
2023-05-23 13:57:15 +02:00
|
|
|
String urlString = "https://checkip.amazonaws.com/";
|
2022-02-22 12:54:16 +01:00
|
|
|
try {
|
2023-01-09 14:44:53 +01:00
|
|
|
conn = (HttpURLConnection) new URL(urlString).openConnection();
|
2023-05-22 20:33:02 +02:00
|
|
|
conn.setConnectTimeout(60_000); // 1 minute
|
|
|
|
conn.setReadTimeout(120_000); // 2 minutes
|
2023-01-03 17:39:50 +01:00
|
|
|
conn.setRequestMethod("GET");
|
|
|
|
conn.connect();
|
2023-01-09 14:44:53 +01:00
|
|
|
|
|
|
|
int responseCode = conn.getResponseCode();
|
|
|
|
if ( responseCode != 200 ) {
|
|
|
|
logger.warn("Cannot get the publicIP address for this machine, as \"" + urlString + "\" returned the HTTP-error-code: " + responseCode);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-01-03 17:39:50 +01:00
|
|
|
try ( BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
|
|
|
|
publicIpAddress = bf.readLine().trim();
|
|
|
|
}
|
2022-02-22 12:54:16 +01:00
|
|
|
} catch (Exception e) {
|
2023-01-09 14:44:53 +01:00
|
|
|
logger.warn("Cannot get the publicIP address for this machine, from \"" + urlString + "\"!", e);
|
2022-02-22 12:54:16 +01:00
|
|
|
return null;
|
2023-01-03 17:39:50 +01:00
|
|
|
} finally {
|
|
|
|
if ( conn != null )
|
|
|
|
conn.disconnect();
|
2022-02-22 12:54:16 +01:00
|
|
|
}
|
|
|
|
return publicIpAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getBaseUrl() {
|
|
|
|
return baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setBaseUrl(String baseUrl) {
|
|
|
|
UriBuilder.baseUrl = baseUrl;
|
|
|
|
}
|
2021-03-16 14:25:15 +01:00
|
|
|
|
2022-09-12 16:04:05 +02:00
|
|
|
}
|