fix for more domains

This commit is contained in:
Greg DiCristofaro 2020-12-01 15:44:11 -05:00
parent 4a5e224228
commit cb1736f333

View File

@ -23,6 +23,7 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.logging.Level; import java.util.logging.Level;
import org.apache.commons.lang.StringUtils;
public class NetworkUtils { public class NetworkUtils {
@ -104,19 +105,21 @@ public class NetworkUtils {
if (urlString == null) { if (urlString == null) {
return ""; return "";
} }
String result = ""; String urlHost = null;
try { try {
URL url = new URL(urlString); URL url = new URL(urlString);
result = url.getHost(); urlHost = url.getHost();
} catch (MalformedURLException ex) { } catch (MalformedURLException ex) {
//do not log if not a valid URL - we will try to extract it ourselves //do not log if not a valid URL - we will try to extract it ourselves
} }
//was not a valid URL, try a less picky method // if there is a valid url host, get base domain from that host
if (result == null || result.trim().isEmpty()) { // otherwise use urlString and parse the domain
return getBaseDomain(urlString); String result = (StringUtils.isNotBlank(urlHost))
} ? getBaseDomain(urlHost)
: getBaseDomain(urlString);
return result; return result;
} }