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.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   * {@link org.apache.http.conn.util.PublicSuffixMatcher} loader.
47   *
48   * @since 4.4
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                              // Should never happen
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 }