View Javadoc

1   /*
2    * Copyright (c) 2012 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.eclipse.jetty.spdy.api;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  /**
23   * <p>An enumeration of stream statuses.</p>
24   */
25  public enum StreamStatus
26  {
27      /**
28       * <p>The stream status indicating a protocol error</p>
29       */
30      PROTOCOL_ERROR(1, 1),
31      /**
32       * <p>The stream status indicating that the stream is not valid</p>
33       */
34      INVALID_STREAM(2, 2),
35      /**
36       * <p>The stream status indicating that the stream has been refused</p>
37       */
38      REFUSED_STREAM(3, 3),
39      /**
40       * <p>The stream status indicating that the implementation does not support the SPDY version of the stream</p>
41       */
42      UNSUPPORTED_VERSION(4, 4),
43      /**
44       * <p>The stream status indicating that the stream is no longer needed</p>
45       */
46      CANCEL_STREAM(5, 5),
47      /**
48       * <p>The stream status indicating an implementation error</p>
49       */
50      INTERNAL_ERROR(6, 6),
51      /**
52       * <p>The stream status indicating a flow control error</p>
53       */
54      FLOW_CONTROL_ERROR(7, 7),
55      /**
56       * <p>The stream status indicating a stream opened more than once</p>
57       */
58      STREAM_IN_USE(-1, 8),
59      /**
60       * <p>The stream status indicating data on a stream already closed</p>
61       */
62      STREAM_ALREADY_CLOSED(-1, 9),
63      /**
64       * <p>The stream status indicating credentials not valid</p>
65       */
66      INVALID_CREDENTIALS(-1, 10),
67      /**
68       * <p>The stream status indicating that the implementation could not support a frame too large</p>
69       */
70      FRAME_TOO_LARGE(-1, 11);
71  
72      /**
73       * @param version the SPDY protocol version
74       * @param code the stream status code
75       * @return a {@link StreamStatus} from the given version and code,
76       * or null if no such status exists
77       */
78      public static StreamStatus from(short version, int code)
79      {
80          switch (version)
81          {
82              case SPDY.V2:
83                  return Codes.v2Codes.get(code);
84              case SPDY.V3:
85                  return Codes.v3Codes.get(code);
86              default:
87                  throw new IllegalStateException();
88          }
89      }
90  
91      private final int v2Code;
92      private final int v3Code;
93  
94      private StreamStatus(int v2Code, int v3Code)
95      {
96          this.v2Code = v2Code;
97          if (v2Code >= 0)
98              Codes.v2Codes.put(v2Code, this);
99          this.v3Code = v3Code;
100         if (v3Code >= 0)
101             Codes.v3Codes.put(v3Code, this);
102     }
103 
104     /**
105      * @param version the SPDY protocol version
106      * @return the stream status code
107      */
108     public int getCode(short version)
109     {
110         switch (version)
111         {
112             case SPDY.V2:
113                 return v2Code;
114             case SPDY.V3:
115                 return v3Code;
116             default:
117                 throw new IllegalStateException();
118         }
119     }
120 
121     private static class Codes
122     {
123         private static final Map<Integer, StreamStatus> v2Codes = new HashMap<>();
124         private static final Map<Integer, StreamStatus> v3Codes = new HashMap<>();
125     }
126 }