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  import java.util.concurrent.locks.ReentrantLock;
39  
40  import org.apache.hc.core5.annotation.Contract;
41  import org.apache.hc.core5.annotation.ThreadingBehavior;
42  import org.apache.hc.core5.util.Args;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  /**
47   * {@link PublicSuffixMatcher} loader.
48   *
49   * @since 4.4
50   */
51  @Contract(threading = ThreadingBehavior.SAFE)
52  public final class PublicSuffixMatcherLoader {
53  
54      private static final String PUBLIC_SUFFIX_LIST = "/org/publicsuffix/list/effective_tld_names.dat";
55  
56      private static final Logger LOG = LoggerFactory.getLogger(PublicSuffixMatcherLoader.class);
57  
58      private static final ReentrantLock lock = new ReentrantLock();
59  
60      private static PublicSuffixMatcher load(final InputStream in) throws IOException {
61          final List<PublicSuffixList> lists = PublicSuffixListParser.INSTANCE.parseByType(
62                  new InputStreamReader(in, StandardCharsets.UTF_8));
63          return new PublicSuffixMatcher(lists);
64      }
65  
66      public static PublicSuffixMatcher load(final URL url) throws IOException {
67          Args.notNull(url, "URL");
68          try (InputStream in = url.openStream()) {
69              return load(in);
70          }
71      }
72  
73      public static PublicSuffixMatcher load(final File file) throws IOException {
74          Args.notNull(file, "File");
75          try (InputStream in = new FileInputStream(file)) {
76              return load(in);
77          }
78      }
79  
80      private static volatile PublicSuffixMatcher DEFAULT_INSTANCE;
81  
82      public static PublicSuffixMatcher getDefault() {
83          if (DEFAULT_INSTANCE == null) {
84              lock.lock();
85              try {
86                  if (DEFAULT_INSTANCE == null) {
87                      final URL url = PublicSuffixMatcherLoader.class.getResource(PUBLIC_SUFFIX_LIST);
88                      if (url != null) {
89                          try {
90                              DEFAULT_INSTANCE = load(url);
91                          } catch (final IOException ex) {
92                              // Should never happen
93                              LOG.warn("Failure loading public suffix list from default resource", ex);
94                          }
95                      } else {
96                          DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Collections.singletonList("com"), null);
97                      }
98                  }
99              } finally {
100                 lock.unlock();
101             }
102         }
103         return DEFAULT_INSTANCE;
104     }
105 
106 }