View Javadoc
1   /*
2    * Copyright 2020 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    *   https://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.example.stomp.websocket;
17  
18  import io.netty.util.AttributeKey;
19  import io.netty.util.internal.StringUtil;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  public enum StompVersion {
25  
26      STOMP_V11("1.1", "v11.stomp"),
27  
28      STOMP_V12("1.2", "v12.stomp");
29  
30      public static final AttributeKey<StompVersion> CHANNEL_ATTRIBUTE_KEY = AttributeKey.valueOf("stomp_version");
31      public static final String SUB_PROTOCOLS;
32  
33      static {
34          List<String> subProtocols = new ArrayList<String>(values().length);
35          for (StompVersion stompVersion : values()) {
36              subProtocols.add(stompVersion.subProtocol);
37          }
38  
39          SUB_PROTOCOLS = StringUtil.join(",", subProtocols).toString();
40      }
41  
42      private final String version;
43      private final String subProtocol;
44  
45      StompVersion(String version, String subProtocol) {
46          this.version = version;
47          this.subProtocol = subProtocol;
48      }
49  
50      public String version() {
51          return version;
52      }
53  
54      public String subProtocol() {
55          return subProtocol;
56      }
57  
58      public static StompVersion findBySubProtocol(String subProtocol) {
59          if (subProtocol != null) {
60              for (StompVersion stompVersion : values()) {
61                  if (stompVersion.subProtocol().equals(subProtocol)) {
62                      return stompVersion;
63                  }
64              }
65          }
66  
67          throw new IllegalArgumentException("Not found StompVersion for '" + subProtocol + "'");
68      }
69  }