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 Logger LOG = LoggerFactory.getLogger(PublicSuffixMatcherLoader.class);
55  
56      private static final ReentrantLock lock = new ReentrantLock();
57  
58      private static PublicSuffixMatcher load(final InputStream in) throws IOException {
59          final List<PublicSuffixList> lists = PublicSuffixListParser.INSTANCE.parseByType(
60                  new InputStreamReader(in, StandardCharsets.UTF_8));
61          return new PublicSuffixMatcher(lists);
62      }
63  
64      public static PublicSuffixMatcher load(final URL url) throws IOException {
65          Args.notNull(url, "URL");
66          try (InputStream in = url.openStream()) {
67              return load(in);
68          }
69      }
70  
71      public static PublicSuffixMatcher load(final File file) throws IOException {
72          Args.notNull(file, "File");
73          try (InputStream in = new FileInputStream(file)) {
74              return load(in);
75          }
76      }
77  
78      private static volatile PublicSuffixMatcher DEFAULT_INSTANCE;
79  
80      public static PublicSuffixMatcher getDefault() {
81          if (DEFAULT_INSTANCE == null) {
82              lock.lock();
83              try {
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                              // Should never happen
92                              LOG.warn("Failure loading public suffix list from default resource", ex);
93                          }
94                      } else {
95                          DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Collections.singletonList("com"), null);
96                      }
97                  }
98              } finally {
99                  lock.unlock();
100             }
101         }
102         return DEFAULT_INSTANCE;
103     }
104 
105 }