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  /**
19   * The SPDY stream status code and its description.
20   */
21  public class SpdyStreamStatus implements Comparable<SpdyStreamStatus> {
22  
23      /**
24       * 1 Protocol Error
25       */
26      public static final SpdyStreamStatus PROTOCOL_ERROR =
27          new SpdyStreamStatus(1, "PROTOCOL_ERROR");
28  
29      /**
30       * 2 Invalid Stream
31       */
32      public static final SpdyStreamStatus INVALID_STREAM =
33          new SpdyStreamStatus(2, "INVALID_STREAM");
34  
35      /**
36       * 3 Refused Stream
37       */
38      public static final SpdyStreamStatus REFUSED_STREAM =
39          new SpdyStreamStatus(3, "REFUSED_STREAM");
40  
41      /**
42       * 4 Unsupported Version
43       */
44      public static final SpdyStreamStatus UNSUPPORTED_VERSION =
45          new SpdyStreamStatus(4, "UNSUPPORTED_VERSION");
46  
47      /**
48       * 5 Cancel
49       */
50      public static final SpdyStreamStatus CANCEL =
51          new SpdyStreamStatus(5, "CANCEL");
52  
53      /**
54       * 6 Internal Error
55       */
56      public static final SpdyStreamStatus INTERNAL_ERROR =
57          new SpdyStreamStatus(6, "INTERNAL_ERROR");
58  
59      /**
60       * 7 Flow Control Error
61       */
62      public static final SpdyStreamStatus FLOW_CONTROL_ERROR =
63          new SpdyStreamStatus(7, "FLOW_CONTROL_ERROR");
64  
65      /**
66       * 8 Stream In Use
67       */
68      public static final SpdyStreamStatus STREAM_IN_USE =
69          new SpdyStreamStatus(8, "STREAM_IN_USE");
70  
71      /**
72       * 9 Stream Already Closed
73       */
74      public static final SpdyStreamStatus STREAM_ALREADY_CLOSED =
75          new SpdyStreamStatus(9, "STREAM_ALREADY_CLOSED");
76  
77      /**
78       * 10 Invalid Credentials
79       */
80      public static final SpdyStreamStatus INVALID_CREDENTIALS =
81          new SpdyStreamStatus(10, "INVALID_CREDENTIALS");
82  
83      /**
84       * 11 Frame Too Large
85       */
86      public static final SpdyStreamStatus FRAME_TOO_LARGE =
87          new SpdyStreamStatus(11, "FRAME_TOO_LARGE");
88  
89      /**
90       * Returns the {@link SpdyStreamStatus} represented by the specified code.
91       * If the specified code is a defined SPDY status code, a cached instance
92       * will be returned.  Otherwise, a new instance will be returned.
93       */
94      public static SpdyStreamStatus valueOf(int code) {
95          if (code == 0) {
96              throw new IllegalArgumentException(
97                      "0 is not a valid status code for a RST_STREAM");
98          }
99  
100         switch (code) {
101         case 1:
102             return PROTOCOL_ERROR;
103         case 2:
104             return INVALID_STREAM;
105         case 3:
106             return REFUSED_STREAM;
107         case 4:
108             return UNSUPPORTED_VERSION;
109         case 5:
110             return CANCEL;
111         case 6:
112             return INTERNAL_ERROR;
113         case 7:
114             return FLOW_CONTROL_ERROR;
115         case 8:
116             return STREAM_IN_USE;
117         case 9:
118             return STREAM_ALREADY_CLOSED;
119         case 10:
120             return INVALID_CREDENTIALS;
121         case 11:
122             return FRAME_TOO_LARGE;
123         }
124 
125         return new SpdyStreamStatus(code, "UNKNOWN (" + code + ')');
126     }
127 
128     private final int code;
129 
130     private final String statusPhrase;
131 
132     /**
133      * Creates a new instance with the specified {@code code} and its
134      * {@code statusPhrase}.
135      */
136     public SpdyStreamStatus(int code, String statusPhrase) {
137         if (code == 0) {
138             throw new IllegalArgumentException(
139                     "0 is not a valid status code for a RST_STREAM");
140         }
141 
142         if (statusPhrase == null) {
143             throw new NullPointerException("statusPhrase");
144         }
145 
146         this.code = code;
147         this.statusPhrase = statusPhrase;
148     }
149 
150     /**
151      * Returns the code of this status.
152      */
153     public int code() {
154         return code;
155     }
156 
157     /**
158      * Returns the status phrase of this status.
159      */
160     public String statusPhrase() {
161         return statusPhrase;
162     }
163 
164     @Override
165     public int hashCode() {
166         return code();
167     }
168 
169     @Override
170     public boolean equals(Object o) {
171         if (!(o instanceof SpdyStreamStatus)) {
172             return false;
173         }
174 
175         return code() == ((SpdyStreamStatus) o).code();
176     }
177 
178     @Override
179     public String toString() {
180         return statusPhrase();
181     }
182 
183     @Override
184     public int compareTo(SpdyStreamStatus o) {
185         return code() - o.code();
186     }
187 }