View Javadoc
1   /*
2    * Copyright 2015 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    *   https://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  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   * Provides utility methods related to {@link Headers}.
29   */
30  public final class HeadersUtils {
31  
32      private HeadersUtils() {
33      }
34  
35      /**
36       * {@link Headers#get(Object)} and convert each element of {@link List} to a {@link String}.
37       * @param name the name of the header to retrieve
38       * @return a {@link List} of header values or an empty {@link List} if no values are found.
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       * {@link Headers#get(Object)} and convert the result to a {@link String}.
58       * @param headers the headers to get the {@code name} from
59       * @param name the name of the header to retrieve
60       * @return the first header value if the header is found. {@code null} if there's no such entry.
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       * {@link Headers#iterator()} which converts each {@link Entry}'s key and value to a {@link String}.
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       * Helper for implementing toString for {@link DefaultHeaders} and wrappers such as DefaultHttpHeaders.
77       * @param headersClass the class of headers
78       * @param headersIt the iterator on the actual headers
79       * @param size the size of the iterator
80       * @return a String representation of the headers
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              // original capacity assumes 20 chars per headers
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      * {@link Headers#names()} and convert each element of {@link Set} to a {@link String}.
102      * @param headers the headers to get the names from
103      * @return a {@link Set} of header values or an empty {@link Set} if no values are found.
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 }