Example usage for org.apache.http.conn.util PublicSuffixMatcher getDomainRoot

List of usage examples for org.apache.http.conn.util PublicSuffixMatcher getDomainRoot

Introduction

In this page you can find the example usage for org.apache.http.conn.util PublicSuffixMatcher getDomainRoot.

Prototype

public String getDomainRoot(final String domain) 

Source Link

Document

Returns registrable part of the domain for the given domain name of null if given domain represents a public suffix.

Usage

From source file:org.apache.http.conn.ssl.DefaultHostnameVerifier.java

private static boolean matchIdentity(final String host, final String identity,
        final PublicSuffixMatcher publicSuffixMatcher, final boolean strict) {
    if (publicSuffixMatcher != null && host.contains(".")) {
        if (!matchDomainRoot(host, publicSuffixMatcher.getDomainRoot(identity))) {
            return false;
        }//w w  w . j av  a 2s . c  o  m
    }

    // RFC 2818, 3.1. Server Identity
    // "...Names may contain the wildcard
    // character * which is considered to match any single domain name
    // component or component fragment..."
    // Based on this statement presuming only singular wildcard is legal
    final int asteriskIdx = identity.indexOf('*');
    if (asteriskIdx != -1) {
        final String prefix = identity.substring(0, asteriskIdx);
        final String suffix = identity.substring(asteriskIdx + 1);
        if (!prefix.isEmpty() && !host.startsWith(prefix)) {
            return false;
        }
        if (!suffix.isEmpty() && !host.endsWith(suffix)) {
            return false;
        }
        // Additional sanity checks on content selected by wildcard can be done here
        if (strict) {
            final String remainder = host.substring(prefix.length(), host.length() - suffix.length());
            if (remainder.contains(".")) {
                return false;
            }
        }
        return true;
    }
    return host.equalsIgnoreCase(identity);
}

From source file:org.apache.http.HC4.conn.ssl.DefaultHostnameVerifier.java

private static boolean matchIdentity(final String host, final String identity,
        final PublicSuffixMatcher publicSuffixMatcher, final boolean strict) {
    if (host == null) {
        return false;
    }/*from  w ww. j  a  va  2s.c  om*/

    if (publicSuffixMatcher != null) {
        String domainRoot = publicSuffixMatcher.getDomainRoot(identity);
        if (domainRoot == null) {
            // Public domain
            return false;
        }
        domainRoot = "." + domainRoot;
        if (!host.endsWith(domainRoot)) {
            // Domain root mismatch
            return false;
        }
        if (strict && countDots(identity) != countDots(domainRoot)) {
            return false;
        }
    }

    // RFC 2818, 3.1. Server Identity
    // "...Names may contain the wildcard
    // character * which is considered to match any single domain name
    // component or component fragment..."
    // Based on this statement presuming only singular wildcard is legal
    final int asteriskIdx = identity.indexOf('*');
    if (asteriskIdx != -1) {
        final String prefix = identity.substring(0, asteriskIdx);
        final String suffix = identity.substring(asteriskIdx + 1);
        if (!prefix.isEmpty() && !host.startsWith(prefix)) {
            return false;
        }
        if (!suffix.isEmpty() && !host.endsWith(suffix)) {
            return false;
        }
        // Additional sanity checks on content selected by wildcard can be done here
        if (strict) {
            final String remainder = host.substring(prefix.length(), host.length() - suffix.length());
            if (remainder.contains(".")) {
                return false;
            }
        }
        return true;
    }
    return host.equalsIgnoreCase(identity);
}