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  /**
21   * The default {@link SpdySynStreamFrame} implementation.
22   */
23  public class DefaultSpdySynStreamFrame extends DefaultSpdyHeadersFrame
24          implements SpdySynStreamFrame {
25  
26      private int associatedStreamId;
27      private byte priority;
28      private boolean unidirectional;
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param streamId           the Stream-ID of this frame
34       * @param associatedStreamId the Associated-To-Stream-ID of this frame
35       * @param priority           the priority of the stream
36       */
37      public DefaultSpdySynStreamFrame(int streamId, int associatedStreamId, byte priority) {
38          this(streamId, associatedStreamId, priority, true);
39      }
40  
41      /**
42       * Creates a new instance.
43       *
44       * @param streamId           the Stream-ID of this frame
45       * @param associatedStreamId the Associated-To-Stream-ID of this frame
46       * @param priority           the priority of the stream
47       * @param validateHeaders    validate the header names and values when adding them to the {@link SpdyHeaders}
48       */
49      public DefaultSpdySynStreamFrame(int streamId, int associatedStreamId, byte priority, boolean validateHeaders) {
50          super(streamId, validateHeaders);
51          setAssociatedStreamId(associatedStreamId);
52          setPriority(priority);
53      }
54  
55      @Override
56      public SpdySynStreamFrame setStreamId(int streamId) {
57          super.setStreamId(streamId);
58          return this;
59      }
60  
61      @Override
62      public SpdySynStreamFrame setLast(boolean last) {
63          super.setLast(last);
64          return this;
65      }
66  
67      @Override
68      public SpdySynStreamFrame setInvalid() {
69          super.setInvalid();
70          return this;
71      }
72  
73      @Override
74      public int associatedStreamId() {
75          return associatedStreamId;
76      }
77  
78      @Override
79      public SpdySynStreamFrame setAssociatedStreamId(int associatedStreamId) {
80          if (associatedStreamId < 0) {
81              throw new IllegalArgumentException(
82                      "Associated-To-Stream-ID cannot be negative: " +
83                      associatedStreamId);
84          }
85          this.associatedStreamId = associatedStreamId;
86          return this;
87      }
88  
89      @Override
90      public byte priority() {
91          return priority;
92      }
93  
94      @Override
95      public SpdySynStreamFrame setPriority(byte priority) {
96          if (priority < 0 || priority > 7) {
97              throw new IllegalArgumentException(
98                      "Priority must be between 0 and 7 inclusive: " + priority);
99          }
100         this.priority = priority;
101         return this;
102     }
103 
104     @Override
105     public boolean isUnidirectional() {
106         return unidirectional;
107     }
108 
109     @Override
110     public SpdySynStreamFrame setUnidirectional(boolean unidirectional) {
111         this.unidirectional = unidirectional;
112         return this;
113     }
114 
115     @Override
116     public String toString() {
117         StringBuilder buf = new StringBuilder()
118             .append(StringUtil.simpleClassName(this))
119             .append("(last: ")
120             .append(isLast())
121             .append("; unidirectional: ")
122             .append(isUnidirectional())
123             .append(')')
124             .append(StringUtil.NEWLINE)
125             .append("--> Stream-ID = ")
126             .append(streamId())
127             .append(StringUtil.NEWLINE);
128         if (associatedStreamId != 0) {
129             buf.append("--> Associated-To-Stream-ID = ")
130                .append(associatedStreamId())
131                .append(StringUtil.NEWLINE);
132         }
133         buf.append("--> Priority = ")
134            .append(priority())
135            .append(StringUtil.NEWLINE)
136            .append("--> Headers:")
137            .append(StringUtil.NEWLINE);
138         appendHeaders(buf);
139 
140         // Remove the last newline.
141         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
142         return buf.toString();
143     }
144 }