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