class UrlHelper {

    public static String getDomain(String url, boolean includeProtocol, boolean includeSubdomain) {
        String result = "";
        try {
            URL urlObj = new URL(url);
            String host = urlObj.getHost();
            if (!host.isEmpty()) {
                if (includeProtocol) {
                    result = urlObj.getProtocol() + "://";
                }
                result += urlObj.getHost();

                if (!includeSubdomain) {
                    String suffix = "";
                    for (String domainSuffix : DOMAIN_SUFFIXES) {
                        if (result.endsWith(domainSuffix)) {
                            suffix = domainSuffix;
                            break;
                        }
                    }
                    result = result.substring(0, result.length() - suffix.length());
                    String[] parts = result.split("\\.");
                    result = parts[parts.length - 1] + suffix;
                }

                LOGGER.trace("root url for {} -> {}", url, result);
            } else {
                LOGGER.trace("no domain specified {}", url);
            }
        } catch (MalformedURLException e) {
            LOGGER.trace("could not determine domain for {}", url);
        }
        return result;
    }

    private  UrlHelper();

}