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.http.conn.util;
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.util.Arrays;
36 import java.util.List;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.http.Consts;
41 import org.apache.http.annotation.Contract;
42 import org.apache.http.annotation.ThreadingBehavior;
43 import org.apache.http.util.Args;
44
45
46
47
48
49
50 @Contract(threading = ThreadingBehavior.SAFE)
51 public final class PublicSuffixMatcherLoader {
52
53 private static PublicSuffixMatcher load(final InputStream in) throws IOException {
54 final List<PublicSuffixList> lists = new PublicSuffixListParser().parseByType(
55 new InputStreamReader(in, Consts.UTF_8));
56 return new PublicSuffixMatcher(lists);
57 }
58
59 public static PublicSuffixMatcher load(final URL url) throws IOException {
60 Args.notNull(url, "URL");
61 final InputStream in = url.openStream();
62 try {
63 return load(in);
64 } finally {
65 in.close();
66 }
67 }
68
69 public static PublicSuffixMatcher load(final File file) throws IOException {
70 Args.notNull(file, "File");
71 final InputStream in = new FileInputStream(file);
72 try {
73 return load(in);
74 } finally {
75 in.close();
76 }
77 }
78
79 private static volatile PublicSuffixMatcher DEFAULT_INSTANCE;
80
81 public static PublicSuffixMatcher getDefault() {
82 if (DEFAULT_INSTANCE == null) {
83 synchronized (PublicSuffixMatcherLoader.class) {
84 if (DEFAULT_INSTANCE == null){
85 final URL url = PublicSuffixMatcherLoader.class.getResource(
86 "/mozilla/public-suffix-list.txt");
87 if (url != null) {
88 try {
89 DEFAULT_INSTANCE = load(url);
90 } catch (final IOException ex) {
91
92 final Log log = LogFactory.getLog(PublicSuffixMatcherLoader.class);
93 if (log.isWarnEnabled()) {
94 log.warn("Failure loading public suffix list from default resource", ex);
95 }
96 }
97 } else {
98 DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Arrays.asList("com"), null);
99 }
100 }
101 }
102 }
103 return DEFAULT_INSTANCE;
104 }
105
106 }