1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec;
17
18 import java.util.AbstractCollection;
19 import java.util.AbstractList;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map.Entry;
23 import java.util.Set;
24
25 import static io.netty.util.internal.ObjectUtil.checkNotNull;
26
27
28
29
30 public final class HeadersUtils {
31
32 private HeadersUtils() {
33 }
34
35
36
37
38
39
40 public static <K, V> List<String> getAllAsString(Headers<K, V, ?> headers, K name) {
41 final List<V> allNames = headers.getAll(name);
42 return new AbstractList<String>() {
43 @Override
44 public String get(int index) {
45 V value = allNames.get(index);
46 return value != null ? value.toString() : null;
47 }
48
49 @Override
50 public int size() {
51 return allNames.size();
52 }
53 };
54 }
55
56
57
58
59
60
61
62 public static <K, V> String getAsString(Headers<K, V, ?> headers, K name) {
63 V orig = headers.get(name);
64 return orig != null ? orig.toString() : null;
65 }
66
67
68
69
70 public static Iterator<Entry<String, String>> iteratorAsString(
71 Iterable<Entry<CharSequence, CharSequence>> headers) {
72 return new StringEntryIterator(headers.iterator());
73 }
74
75
76
77
78
79
80
81
82 public static <K, V> String toString(Class<?> headersClass, Iterator<Entry<K, V>> headersIt, int size) {
83 String simpleName = headersClass.getSimpleName();
84 if (size == 0) {
85 return simpleName + "[]";
86 } else {
87
88 StringBuilder sb = new StringBuilder(simpleName.length() + 2 + size * 20)
89 .append(simpleName)
90 .append('[');
91 while (headersIt.hasNext()) {
92 Entry<?, ?> header = headersIt.next();
93 sb.append(header.getKey()).append(": ").append(header.getValue()).append(", ");
94 }
95 sb.setLength(sb.length() - 2);
96 return sb.append(']').toString();
97 }
98 }
99
100
101
102
103
104
105 public static Set<String> namesAsString(Headers<CharSequence, CharSequence, ?> headers) {
106 return new DelegatingNameSet(headers);
107 }
108
109 private static final class StringEntryIterator implements Iterator<Entry<String, String>> {
110 private final Iterator<Entry<CharSequence, CharSequence>> iter;
111
112 StringEntryIterator(Iterator<Entry<CharSequence, CharSequence>> iter) {
113 this.iter = iter;
114 }
115
116 @Override
117 public boolean hasNext() {
118 return iter.hasNext();
119 }
120
121 @Override
122 public Entry<String, String> next() {
123 return new StringEntry(iter.next());
124 }
125
126 @Override
127 public void remove() {
128 iter.remove();
129 }
130 }
131
132 private static final class StringEntry implements Entry<String, String> {
133 private final Entry<CharSequence, CharSequence> entry;
134 private String name;
135 private String value;
136
137 StringEntry(Entry<CharSequence, CharSequence> entry) {
138 this.entry = entry;
139 }
140
141 @Override
142 public String getKey() {
143 if (name == null) {
144 name = entry.getKey().toString();
145 }
146 return name;
147 }
148
149 @Override
150 public String getValue() {
151 if (value == null && entry.getValue() != null) {
152 value = entry.getValue().toString();
153 }
154 return value;
155 }
156
157 @Override
158 public String setValue(String value) {
159 String old = getValue();
160 entry.setValue(value);
161 return old;
162 }
163
164 @Override
165 public String toString() {
166 return entry.toString();
167 }
168 }
169
170 private static final class StringIterator<T> implements Iterator<String> {
171 private final Iterator<T> iter;
172
173 StringIterator(Iterator<T> iter) {
174 this.iter = iter;
175 }
176
177 @Override
178 public boolean hasNext() {
179 return iter.hasNext();
180 }
181
182 @Override
183 public String next() {
184 T next = iter.next();
185 return next != null ? next.toString() : null;
186 }
187
188 @Override
189 public void remove() {
190 iter.remove();
191 }
192 }
193
194 private static final class DelegatingNameSet extends AbstractCollection<String> implements Set<String> {
195 private final Headers<CharSequence, CharSequence, ?> headers;
196
197 DelegatingNameSet(Headers<CharSequence, CharSequence, ?> headers) {
198 this.headers = checkNotNull(headers, "headers");
199 }
200
201 @Override
202 public int size() {
203 return headers.names().size();
204 }
205
206 @Override
207 public boolean isEmpty() {
208 return headers.isEmpty();
209 }
210
211 @Override
212 public boolean contains(Object o) {
213 return headers.contains(o.toString());
214 }
215
216 @Override
217 public Iterator<String> iterator() {
218 return new StringIterator<CharSequence>(headers.names().iterator());
219 }
220 }
221 }