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 = new DefaultSpdyHeaders();
31  
32      /**
33       * Creates a new instance.
34       *
35       * @param streamId the Stream-ID of this frame
36       */
37      public DefaultSpdyHeadersFrame(int streamId) {
38          super(streamId);
39      }
40  
41      @Override
42      public SpdyHeadersFrame setStreamId(int streamId) {
43          super.setStreamId(streamId);
44          return this;
45      }
46  
47      @Override
48      public SpdyHeadersFrame setLast(boolean last) {
49          super.setLast(last);
50          return this;
51      }
52  
53      @Override
54      public boolean isInvalid() {
55          return invalid;
56      }
57  
58      @Override
59      public SpdyHeadersFrame setInvalid() {
60          invalid = true;
61          return this;
62      }
63  
64      @Override
65      public boolean isTruncated() {
66          return truncated;
67      }
68  
69      @Override
70      public SpdyHeadersFrame setTruncated() {
71          truncated = true;
72          return this;
73      }
74  
75      @Override
76      public SpdyHeaders headers() {
77          return headers;
78      }
79  
80      @Override
81      public String toString() {
82          StringBuilder buf = new StringBuilder()
83              .append(StringUtil.simpleClassName(this))
84              .append("(last: ")
85              .append(isLast())
86              .append(')')
87              .append(StringUtil.NEWLINE)
88              .append("--> Stream-ID = ")
89              .append(streamId())
90              .append(StringUtil.NEWLINE)
91              .append("--> Headers:")
92              .append(StringUtil.NEWLINE);
93          appendHeaders(buf);
94  
95          // Remove the last newline.
96          buf.setLength(buf.length() - StringUtil.NEWLINE.length());
97          return buf.toString();
98      }
99  
100     protected void appendHeaders(StringBuilder buf) {
101         for (Map.Entry<String, String> e: headers()) {
102             buf.append("    ");
103             buf.append(e.getKey());
104             buf.append(": ");
105             buf.append(e.getValue());
106             buf.append(StringUtil.NEWLINE);
107         }
108     }
109 }