Example usage for com.google.common.net InternetDomainName hasPublicSuffix

List of usage examples for com.google.common.net InternetDomainName hasPublicSuffix

Introduction

In this page you can find the example usage for com.google.common.net InternetDomainName hasPublicSuffix.

Prototype

public boolean hasPublicSuffix() 

Source Link

Document

Indicates whether this domain name ends in a #isPublicSuffix() public suffix , including if it is a public suffix itself.

Usage

From source file:org.apache.metron.common.dsl.functions.NetworkFunctions.java

/**
 * Extract the TLD.  If the domain is a normal domain, then we can handle the TLD via the InternetDomainName object.
 * If it is not, then we default to returning the last segment after the final '.'
 * @param idn/*w  w  w  .  j  a  v a  2  s .  com*/
 * @param dn
 * @return The TLD of the domain
 */
private static String extractTld(InternetDomainName idn, String dn) {

    if (idn != null && idn.hasPublicSuffix()) {
        return idn.publicSuffix().toString();
    } else if (dn != null) {
        StringBuffer tld = new StringBuffer("");
        for (int idx = dn.length() - 1; idx >= 0; idx--) {
            char c = dn.charAt(idx);
            if (c == '.') {
                break;
            } else {
                tld.append(dn.charAt(idx));
            }
        }
        return tld.reverse().toString();
    } else {
        return null;
    }
}

From source file:org.sindice.core.analytics.commons.util.URIUtil.java

/**
 * Return the second-level domain name. Returns null if the domain is not valid.
 * This method normalises domain names by removing the leading www sub-domain,
 * if present.//from www  .  j  a  v a2 s.  co  m
 * @param domain
 * @return
 */
public static String getSndDomain(String domain) {
    if (domain == null) {
        return null;
    }
    // Remove www subdomain if it exists
    if (domain.startsWith("www.")) {
        domain = domain.substring(4);
    }
    if (InternetDomainName.isValid(domain)) { // the domain is valid according to the RFC3490
        final InternetDomainName idn = InternetDomainName.from(domain);
        if (idn.hasPublicSuffix()) { // the domain has a public suffix
            if (idn.isUnderPublicSuffix()) {
                return idn.topPrivateDomain().name();
            } else if (idn.hasParent()) {
                final List<String> parts = idn.parts();
                return parts.get(parts.size() - 2).concat(".").concat(parts.get(parts.size() - 1));
            }
        }
    }
    return null;
}

From source file:org.apache.metron.stellar.dsl.functions.NetworkFunctions.java

/**
 * Extract the TLD.  If the domain is a normal domain, then we can handle the TLD via the InternetDomainName object.
 * If it is not, then we default to returning the last segment after the final '.'
 * @param idn/*ww w  . j a v a2s.c o m*/
 * @param dn
 * @return The TLD of the domain
 */
private static String extractTld(InternetDomainName idn, String dn) {

    if (idn != null && idn.hasPublicSuffix()) {
        String ret = idn.publicSuffix().toString();
        if (ret.startsWith("InternetDomainName")) {
            return Joiner.on(".").join(idn.publicSuffix().parts());
        } else {
            return ret;
        }
    } else if (dn != null) {
        StringBuffer tld = new StringBuffer("");
        for (int idx = dn.length() - 1; idx >= 0; idx--) {
            char c = dn.charAt(idx);
            if (c == '.') {
                break;
            } else {
                tld.append(dn.charAt(idx));
            }
        }
        return tld.reverse().toString();
    } else {
        return null;
    }
}

From source file:org.mayocat.multitenancy.DefaultTenantResolver.java

private String extractSlugFromHost(String host) {
    String rootDomain;/*ww  w . ja  v  a 2 s  .  co m*/
    String siteName = siteSettings.getWebDomainName().or(siteSettings.getDomainName());
    if (Strings.emptyToNull(siteName) == null) {
        InternetDomainName domainName = InternetDomainName.from(host);
        if (domainName.hasPublicSuffix()) {
            // Domain is under a valid TLD, extract the TLD + first child
            rootDomain = domainName.topPrivateDomain().name();
        } else if (host.indexOf(".") > 0 && host.indexOf(".") < host.length()) {
            // Otherwise, best guess : strip everything before the first dot.
            rootDomain = host.substring(host.indexOf(".") + 1);
        } else {
            rootDomain = host;
        }
    } else {
        rootDomain = StringUtils.substringBefore(siteSettings.getDomainName(), ":");
    }
    if (host.indexOf("." + rootDomain) > 0) {
        return host.substring(0, host.indexOf("." + rootDomain));
    } else {
        return host;
    }
}

From source file:com.addthis.hydra.data.filter.bundle.BundleFilterURL.java

@Override
public boolean filter(Bundle bundle) {
    String pv = ValueUtil.asNativeString(field.getValue(bundle));
    if (!asFile) {
        if (pv == null || pv.length() < 7) {
            return invalidExit;
        }//from www  .ja  v  a  2 s  . co m
        String lpv = pv.trim().toLowerCase();
        if (!(lpv.startsWith("http"))) {
            if (fixProto) {
                if (clean && lpv.indexOf("%2f") >= 0) {
                    pv = LessBytes.urldecode(pv);
                }
                pv = "http://".concat(pv);
            } else {
                return invalidExit;
            }
        }
        if (clean && (lpv.startsWith("http%") || lpv.startsWith("https%"))) {
            pv = LessBytes.urldecode(pv);
        }
    }
    // up to two 'decoding' passes on the url to try and find a valid one
    for (int i = 0; i < 2; i++) {
        if (pv == null) {
            return invalidExit;
        }
        try {
            URL urec = asFile ? new URL("file://".concat(pv)) : new URL(pv);
            String urlhost = urec.getHost();
            String returnhost = null;
            if (resolveIP) {
                synchronized (iphost) {
                    returnhost = iphost.get(urlhost).toLowerCase();
                    if (returnhost == null) {
                        returnhost = resolveDottedIP(urlhost);
                        iphost.put(urlhost, returnhost);
                        if (iphost.size() > maxhostcache) {
                            iphost.removeEldest();
                        }
                    }
                }
            } else {
                returnhost = urlhost.toLowerCase();
            }
            // store cleaned up (url decoded) version back to packet
            if (clean) {
                if (urec != null && urec.getPath().isEmpty()) {
                    // if the path element is null, append the slash
                    pv = pv.concat("/");
                }
                field.setValue(bundle, ValueFactory.create(pv));
            }
            if (setHost != null) {
                if (toBaseDomain) {
                    returnhost = NetUtil.getBaseDomain(returnhost);
                } else if (toTopPrivateDomain) {
                    if (returnhost != null && InternetDomainName.isValid(returnhost)) {
                        InternetDomainName domain = InternetDomainName.from(returnhost);
                        if (domain.hasPublicSuffix() && domain.isUnderPublicSuffix()) {
                            InternetDomainName topPrivateDomain = domain.topPrivateDomain();
                            returnhost = topPrivateDomain.toString();
                        }
                    }
                }
                setHost.setValue(bundle, ValueFactory.create(returnhost));
            }
            if (setPath != null) {
                setPath.setValue(bundle, ValueFactory.create(urec.getPath()));
            }
            if (setParams != null) {
                setParams.setValue(bundle, ValueFactory.create(urec.getQuery()));
            }
            if (setAnchor != null) {
                setAnchor.setValue(bundle, ValueFactory.create(urec.getRef()));
            }
            if (setHostNormal != null) {
                Matcher m = hostNormalPattern.matcher(returnhost);
                if (m.find()) {
                    returnhost = m.group(1);
                }
                setHostNormal.setValue(bundle, ValueFactory.create(returnhost));
            }
            if (setTopPrivateDomain != null) {
                String topDomain = returnhost;
                if (InternetDomainName.isValid(returnhost)) {
                    InternetDomainName domainName = InternetDomainName.from(returnhost);
                    if (domainName.isTopPrivateDomain() || domainName.isUnderPublicSuffix()) {
                        topDomain = DOT_JOINER.join(domainName.topPrivateDomain().parts());
                    }
                }
                setTopPrivateDomain.setValue(bundle, ValueFactory.create(topDomain));
            }
        } catch (MalformedURLException e) {
            if (pv.indexOf("%3") > 0 && pv.indexOf("%2") > 0) {
                pv = LessBytes.urldecode(pv);
            } else {
                if (debugMalformed) {
                    System.err.println("malformed(" + i + ") " + pv);
                }
                return invalidExit;
            }
        }
    }
    return true;
}