View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.util;
18  
19  import io.netty.util.internal.StringUtil;
20  
21  import java.net.IDN;
22  import java.util.LinkedHashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  import java.util.regex.Pattern;
26  
27  /**
28   * Maps a domain name to its associated value object.
29   * <p>
30   * DNS wildcard is supported as hostname, so you can use {@code *.netty.io} to match both {@code netty.io}
31   * and {@code downloads.netty.io}.
32   * </p>
33   */
34  public class DomainNameMapping<V> implements Mapping<String, V> {
35  
36      private static final Pattern DNS_WILDCARD_PATTERN = Pattern.compile("^\\*\\..*");
37  
38      private final Map<String, V> map;
39  
40      private final V defaultValue;
41  
42      /**
43       * Creates a default, order-sensitive mapping. If your hostnames are in conflict, the mapping
44       * will choose the one you add first.
45       *
46       * @param defaultValue the default value for {@link #map(String)} to return when nothing matches the input
47       */
48      public DomainNameMapping(V defaultValue) {
49          this(4, defaultValue);
50      }
51  
52      /**
53       * Creates a default, order-sensitive mapping. If your hostnames are in conflict, the mapping
54       * will choose the one you add first.
55       *
56       * @param initialCapacity initial capacity for the internal map
57       * @param defaultValue the default value for {@link #map(String)} to return when nothing matches the input
58       */
59      public DomainNameMapping(int initialCapacity, V defaultValue) {
60          if (defaultValue == null) {
61              throw new NullPointerException("defaultValue");
62          }
63          map = new LinkedHashMap<String, V>(initialCapacity);
64          this.defaultValue = defaultValue;
65      }
66  
67      /**
68       * Adds a mapping that maps the specified (optionally wildcard) host name to the specified output value.
69       * <p>
70       * <a href="http://en.wikipedia.org/wiki/Wildcard_DNS_record">DNS wildcard</a> is supported as hostname.
71       * For example, you can use {@code *.netty.io} to match {@code netty.io} and {@code downloads.netty.io}.
72       * </p>
73       *
74       * @param hostname the host name (optionally wildcard)
75       * @param output the output value that will be returned by {@link #map(String)} when the specified host name
76       *               matches the specified input host name
77       */
78      public DomainNameMapping<V> add(String hostname, V output) {
79          if (hostname == null) {
80              throw new NullPointerException("input");
81          }
82  
83          if (output == null) {
84              throw new NullPointerException("output");
85          }
86  
87          map.put(normalizeHostname(hostname), output);
88          return this;
89      }
90  
91      /**
92       * Simple function to match <a href="http://en.wikipedia.org/wiki/Wildcard_DNS_record">DNS wildcard</a>.
93       */
94      private static boolean matches(String hostNameTemplate, String hostName) {
95          // note that inputs are converted and lowercased already
96          if (DNS_WILDCARD_PATTERN.matcher(hostNameTemplate).matches()) {
97              return hostNameTemplate.substring(2).equals(hostName) ||
98                      hostName.endsWith(hostNameTemplate.substring(1));
99          } else {
100             return hostNameTemplate.equals(hostName);
101         }
102     }
103 
104     /**
105      * IDNA ASCII conversion and case normalization
106      */
107     private static String normalizeHostname(String hostname) {
108         if (needsNormalization(hostname)) {
109             hostname = IDN.toASCII(hostname, IDN.ALLOW_UNASSIGNED);
110         }
111         return hostname.toLowerCase(Locale.US);
112     }
113 
114     private static boolean needsNormalization(String hostname) {
115         final int length = hostname.length();
116         for (int i = 0; i < length; i ++) {
117             int c = hostname.charAt(i);
118             if (c > 0x7F) {
119                 return true;
120             }
121         }
122         return false;
123     }
124 
125     @Override
126     public V map(String input) {
127         if (input != null) {
128             input = normalizeHostname(input);
129 
130             for (Map.Entry<String, V> entry : map.entrySet()) {
131                 if (matches(entry.getKey(), input)) {
132                     return entry.getValue();
133                 }
134             }
135         }
136 
137         return defaultValue;
138     }
139 
140     @Override
141     public String toString() {
142         return StringUtil.simpleClassName(this) + "(default: " + defaultValue + ", map: " + map + ')';
143     }
144 }