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

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

Introduction

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

Prototype

public PublicSuffixMatcher(final Collection<String> rules, final Collection<String> exceptions) 

Source Link

Usage

From source file:net.i2p.util.I2PSSLSocketFactory.java

/**
 *  From Apache PublicSuffixMatcherLoader.getDefault()
 *
 *  https://publicsuffix.org/list/effective_tld_names.dat
 *  What does this get us?/*ww w.j  ava2  s  .  co  m*/
 *  Deciding whether to issue or accept an SSL wildcard certificate for *.public.suffix.
 *
 *  @return null on failure
 *  @since 0.9.20
 */
private static PublicSuffixMatcher getDefaultMatcher(I2PAppContext ctx) {
    synchronized (I2PSSLSocketFactory.class) {
        if (!_matcherLoaded) {
            String geoDir = ctx.getProperty(PROP_GEOIP_DIR, GEOIP_DIR_DEFAULT);
            File geoFile = new File(geoDir);
            if (!geoFile.isAbsolute())
                geoFile = new File(ctx.getBaseDir(), geoDir);
            geoFile = new File(geoFile, PUBLIC_SUFFIX_LIST);
            Log log = ctx.logManager().getLog(I2PSSLSocketFactory.class);
            if (geoFile.exists()) {
                try {
                    // we can't use PublicSuffixMatcherLoader.load() here because we
                    // want to add some of our own and a PublicSuffixMatcher's
                    // underlying PublicSuffixList is immutable and inaccessible
                    long begin = System.currentTimeMillis();
                    InputStream in = null;
                    PublicSuffixList list = new PublicSuffixList(Arrays.asList(ADDITIONAL_TLDS),
                            Collections.<String>emptyList());
                    try {
                        in = new FileInputStream(geoFile);
                        PublicSuffixList list2 = new PublicSuffixListParser()
                                .parse(new InputStreamReader(in, "UTF-8"));
                        list = merge(list, list2);
                    } finally {
                        try {
                            if (in != null)
                                in.close();
                        } catch (IOException ioe) {
                        }
                    }
                    DEFAULT_MATCHER = new PublicSuffixMatcher(list.getRules(), list.getExceptions());
                    if (log.shouldWarn())
                        log.warn("Loaded " + geoFile + " in " + (System.currentTimeMillis() - begin)
                                + " ms and created list with " + list.getRules().size() + " entries and "
                                + list.getExceptions().size() + " exceptions");
                } catch (IOException ex) {
                    log.error("Failure loading public suffix list from " + geoFile, ex);
                    // DEFAULT_MATCHER remains null
                }
            } else {
                List<String> list = new ArrayList<String>(320);
                addCountries(ctx, list);
                list.addAll(Arrays.asList(DEFAULT_TLDS));
                list.addAll(Arrays.asList(ADDITIONAL_TLDS));
                DEFAULT_MATCHER = new PublicSuffixMatcher(list, null);
                if (log.shouldWarn())
                    log.warn("No public suffix list found at " + geoFile + " - created default with "
                            + list.size() + " entries");
            }
        }
        _matcherLoaded = true;
    }
    return DEFAULT_MATCHER;
}

From source file:org.apache.http.conn.util.PublicSuffixMatcherLoader.java

private static PublicSuffixMatcher load(final InputStream in) throws IOException {
    final PublicSuffixList list = new PublicSuffixListParser().parse(new InputStreamReader(in, Consts.UTF_8));
    return new PublicSuffixMatcher(list.getRules(), list.getExceptions());
}

From source file:org.apache.http.conn.util.PublicSuffixMatcherLoader.java

public static PublicSuffixMatcher getDefault() {
    if (DEFAULT_INSTANCE == null) {
        synchronized (PublicSuffixMatcherLoader.class) {
            if (DEFAULT_INSTANCE == null) {
                final URL url = PublicSuffixMatcherLoader.class.getResource("/mozilla/public-suffix-list.txt");
                if (url != null) {
                    try {
                        DEFAULT_INSTANCE = load(url);
                    } catch (IOException ex) {
                        // Should never happen
                        final Log log = LogFactory.getLog(PublicSuffixMatcherLoader.class);
                        if (log.isWarnEnabled()) {
                            log.warn("Failure loading public suffix list from default resource", ex);
                        }//w w w  .ja  v a  2s  .  c  o m
                    }
                } else {
                    DEFAULT_INSTANCE = new PublicSuffixMatcher(Arrays.asList("com"), null);
                }
            }
        }
    }
    return DEFAULT_INSTANCE;
}