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    *   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  package io.netty.handler.codec.spdy;
17  
18  import io.netty.util.internal.StringUtil;
19  
20  import java.util.Map;
21  
22  /**
23   * The default {@link SpdyHeadersFrame} implementation.
24   */
25  public class DefaultSpdyHeadersFrame extends DefaultSpdyStreamFrame
26          implements SpdyHeadersFrame {
27  
28      private boolean invalid;
29      private boolean truncated;
30      private final SpdyHeaders headers;
31  
32      /**
33       * Creates a new instance.
34       *
35       * @param streamId the Stream-ID of this frame
36       */
37      public DefaultSpdyHeadersFrame(int streamId) {
38          this(streamId, true);
39      }
40  
41      /**
42       * Creates a new instance.
43       *
44       * @param streamId the Stream-ID of this frame
45       * @param validate validate the header names and values when adding them to the {@link SpdyHeaders}
46       */
47      public DefaultSpdyHeadersFrame(int streamId, boolean validate) {
48          super(streamId);
49          headers = new DefaultSpdyHeaders(validate);
50      }
51  
52      @Override
53      public SpdyHeadersFrame setStreamId(int streamId) {
54          super.setStreamId(streamId);
55          return this;
56      }
57  
58      @Override
59      public SpdyHeadersFrame setLast(boolean last) {
60          super.setLast(last);
61          return this;
62      }
63  
64      @Override
65      public boolean isInvalid() {
66          return invalid;
67      }
68  
69      @Override
70      public SpdyHeadersFrame setInvalid() {
71          invalid = true;
72          return this;
73      }
74  
75      @Override
76      public boolean isTruncated() {
77          return truncated;
78      }
79  
80      @Override
81      public SpdyHeadersFrame setTruncated() {
82          truncated = true;
83          return this;
84      }
85  
86      @Override
87      public SpdyHeaders headers() {
88          return headers;
89      }
90  
91      @Override
92      public String toString() {
93          StringBuilder buf = new StringBuilder()
94              .append(StringUtil.simpleClassName(this))
95              .append("(last: ")
96              .append(isLast())
97              .append(')')
98              .append(StringUtil.NEWLINE)
99              .append("--> Stream-ID = ")
100             .append(streamId())
101             .append(StringUtil.NEWLINE)
102             .append("--> Headers:")
103             .append(StringUtil.NEWLINE);
104         appendHeaders(buf);
105 
106         // Remove the last newline.
107         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
108         return buf.toString();
109     }
110 
111     protected void appendHeaders(StringBuilder buf) {
112         for (Map.Entry<CharSequence, CharSequence> e: headers()) {
113             buf.append("    ");
114             buf.append(e.getKey());
115             buf.append(": ");
116             buf.append(e.getValue());
117             buf.append(StringUtil.NEWLINE);
118         }
119     }
120 }