1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.Collections;
23 import java.util.LinkedHashMap;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import static io.netty.util.internal.ObjectUtil.checkNotNull;
28 import static io.netty.util.internal.StringUtil.commonSuffixOfLength;
29
30
31
32
33
34
35
36
37
38 @Deprecated
39 public class DomainNameMapping<V> implements Mapping<String, V> {
40
41 final V defaultValue;
42 private final Map<String, V> map;
43 private final Map<String, V> unmodifiableMap;
44
45
46
47
48
49
50
51
52 @Deprecated
53 public DomainNameMapping(V defaultValue) {
54 this(4, defaultValue);
55 }
56
57
58
59
60
61
62
63
64
65 @Deprecated
66 public DomainNameMapping(int initialCapacity, V defaultValue) {
67 this(new LinkedHashMap<String, V>(initialCapacity), defaultValue);
68 }
69
70 DomainNameMapping(Map<String, V> map, V defaultValue) {
71 this.defaultValue = checkNotNull(defaultValue, "defaultValue");
72 this.map = map;
73 unmodifiableMap = map != null ? Collections.unmodifiableMap(map)
74 : null;
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89 @Deprecated
90 public DomainNameMapping<V> add(String hostname, V output) {
91 map.put(normalizeHostname(checkNotNull(hostname, "hostname")), checkNotNull(output, "output"));
92 return this;
93 }
94
95
96
97
98 static boolean matches(String template, String hostName) {
99 if (template.startsWith("*.")) {
100 return template.regionMatches(2, hostName, 0, hostName.length())
101 || commonSuffixOfLength(hostName, template, template.length() - 1);
102 }
103 return template.equals(hostName);
104 }
105
106
107
108
109 static String normalizeHostname(String hostname) {
110 if (needsNormalization(hostname)) {
111 hostname = IDN.toASCII(hostname, IDN.ALLOW_UNASSIGNED);
112 }
113 return hostname.toLowerCase(Locale.US);
114 }
115
116 private static boolean needsNormalization(String hostname) {
117 final int length = hostname.length();
118 for (int i = 0; i < length; i++) {
119 int c = hostname.charAt(i);
120 if (c > 0x7F) {
121 return true;
122 }
123 }
124 return false;
125 }
126
127 @Override
128 public V map(String hostname) {
129 if (hostname != null) {
130 hostname = normalizeHostname(hostname);
131
132 for (Map.Entry<String, V> entry : map.entrySet()) {
133 if (matches(entry.getKey(), hostname)) {
134 return entry.getValue();
135 }
136 }
137 }
138 return defaultValue;
139 }
140
141
142
143
144 public Map<String, V> asMap() {
145 return unmodifiableMap;
146 }
147
148 @Override
149 public String toString() {
150 return StringUtil.simpleClassName(this) + "(default: " + defaultValue + ", map: " + map + ')';
151 }
152 }