diff --git a/build.gradle b/build.gradle index 7309bca..65186d9 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java b/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java index 7d2e557..b4980c6 100644 --- a/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java +++ b/src/main/java/eu/openaire/urls_controller/util/UriBuilder.java @@ -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; }