1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.psl;
28
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.net.URL;
35 import java.nio.charset.StandardCharsets;
36 import java.util.Arrays;
37 import java.util.List;
38
39 import org.apache.hc.core5.annotation.Contract;
40 import org.apache.hc.core5.annotation.ThreadingBehavior;
41 import org.apache.hc.core5.util.Args;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47
48
49
50 @Contract(threading = ThreadingBehavior.SAFE)
51 public final class PublicSuffixMatcherLoader {
52
53 private static final Logger LOG = LoggerFactory.getLogger(PublicSuffixMatcherLoader.class);
54
55 private static PublicSuffixMatcher load(final InputStream in) throws IOException {
56 final List<PublicSuffixList> lists = new PublicSuffixListParser().parseByType(
57 new InputStreamReader(in, StandardCharsets.UTF_8));
58 return new PublicSuffixMatcher(lists);
59 }
60
61 public static PublicSuffixMatcher load(final URL url) throws IOException {
62 Args.notNull(url, "URL");
63 try (InputStream in = url.openStream()) {
64 return load(in);
65 }
66 }
67
68 public static PublicSuffixMatcher load(final File file) throws IOException {
69 Args.notNull(file, "File");
70 try (InputStream in = new FileInputStream(file)) {
71 return load(in);
72 }
73 }
74
75 private static volatile PublicSuffixMatcher DEFAULT_INSTANCE;
76
77 public static PublicSuffixMatcher getDefault() {
78 if (DEFAULT_INSTANCE == null) {
79 synchronized (PublicSuffixMatcherLoader.class) {
80 if (DEFAULT_INSTANCE == null){
81 final URL url = PublicSuffixMatcherLoader.class.getResource(
82 "/mozilla/public-suffix-list.txt");
83 if (url != null) {
84 try {
85 DEFAULT_INSTANCE = load(url);
86 } catch (final IOException ex) {
87
88 LOG.warn("Failure loading public suffix list from default resource", ex);
89 }
90 } else {
91 DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Arrays.asList("com"), null);
92 }
93 }
94 }
95 }
96 return DEFAULT_INSTANCE;
97 }
98
99 }