View Javadoc
1   /*
2    * Copyright 2013 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.spdy;
17  
18  import io.netty.handler.codec.CharSequenceValueConverter;
19  import io.netty.handler.codec.DefaultHeaders;
20  import io.netty.handler.codec.HeadersUtils;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map.Entry;
25  
26  import static io.netty.util.AsciiString.CASE_INSENSITIVE_HASHER;
27  import static io.netty.util.AsciiString.CASE_SENSITIVE_HASHER;
28  
29  public class DefaultSpdyHeaders extends DefaultHeaders<CharSequence, CharSequence, SpdyHeaders> implements SpdyHeaders {
30      private static final NameValidator<CharSequence> SpdyNameValidator = new NameValidator<CharSequence>() {
31          @Override
32          public void validateName(CharSequence name) {
33              SpdyCodecUtil.validateHeaderName(name);
34          }
35      };
36  
37      public DefaultSpdyHeaders() {
38          this(true);
39      }
40  
41      @SuppressWarnings("unchecked")
42      public DefaultSpdyHeaders(boolean validate) {
43          super(CASE_INSENSITIVE_HASHER,
44                  validate ? HeaderValueConverterAndValidator.INSTANCE : CharSequenceValueConverter.INSTANCE,
45                  validate ? SpdyNameValidator : NameValidator.NOT_NULL);
46      }
47  
48      @Override
49      public String getAsString(CharSequence name) {
50          return HeadersUtils.getAsString(this, name);
51      }
52  
53      @Override
54      public List<String> getAllAsString(CharSequence name) {
55          return HeadersUtils.getAllAsString(this, name);
56      }
57  
58      @Override
59      public Iterator<Entry<String, String>> iteratorAsString() {
60          return HeadersUtils.iteratorAsString(this);
61      }
62  
63      @Override
64      public boolean contains(CharSequence name, CharSequence value) {
65          return contains(name, value, false);
66      }
67  
68      @Override
69      public boolean contains(CharSequence name, CharSequence value, boolean ignoreCase) {
70          return contains(name, value,
71                  ignoreCase ? CASE_INSENSITIVE_HASHER : CASE_SENSITIVE_HASHER);
72      }
73  
74      private static final class HeaderValueConverterAndValidator extends CharSequenceValueConverter {
75          public static final HeaderValueConverterAndValidator INSTANCE = new HeaderValueConverterAndValidator();
76  
77          @Override
78          public CharSequence convertObject(Object value) {
79              final CharSequence seq = super.convertObject(value);
80              SpdyCodecUtil.validateHeaderValue(seq);
81              return seq;
82          }
83      }
84  }