- Improve the stability of "UriBuilder.getPublicIP()", by using a "HttpURLConnection" to increase the connection and read timeouts and avoid timeout-exceptions.

- Update Spring.
This commit is contained in:
Lampros Smyrnaios 2023-01-03 18:39:50 +02:00
parent 4528d1f9be
commit 9904ea5743
2 changed files with 14 additions and 10 deletions

View File

@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.7.6'
id 'org.springframework.boot' version '2.7.7'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}

View File

@ -8,8 +8,8 @@ import org.springframework.core.env.Environment;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
@ -55,18 +55,22 @@ public class UriBuilder {
private static String getPublicIP()
{
String publicIpAddress = "";
URL url_name;
HttpURLConnection conn = null;
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();
conn = (HttpURLConnection) new URL("https://api.ipify.org/").openConnection();
conn.setConnectTimeout(60_000);
conn.setReadTimeout(60_000);
conn.setRequestMethod("GET");
conn.connect();
try ( BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
publicIpAddress = bf.readLine().trim();
}
} catch (Exception e) {
logger.warn("Cannot get the publicIP address for this machine!", e);
return null;
} finally {
if ( conn != null )
conn.disconnect();
}
return publicIpAddress;
}