View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.Collections;
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   * {@link PublicSuffixMatcher} loader.
47   *
48   * @since 4.4
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 = PublicSuffixListParser.INSTANCE.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                              // Should never happen
88                              LOG.warn("Failure loading public suffix list from default resource", ex);
89                          }
90                      } else {
91                          DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Collections.singletonList("com"), null);
92                      }
93                  }
94              }
95          }
96          return DEFAULT_INSTANCE;
97      }
98  
99  }