View Javadoc
1   /*
2    * Copyright 2014 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  
17  package io.netty.handler.codec.mqtt;
18  
19  /**
20   * Return Code of {@link MqttConnAckMessage}
21   */
22  public enum MqttConnectReturnCode {
23      CONNECTION_ACCEPTED((byte) 0x00),
24      //MQTT 3 codes
25      CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION((byte) 0X01),
26      CONNECTION_REFUSED_IDENTIFIER_REJECTED((byte) 0x02),
27      CONNECTION_REFUSED_SERVER_UNAVAILABLE((byte) 0x03),
28      CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD((byte) 0x04),
29      CONNECTION_REFUSED_NOT_AUTHORIZED((byte) 0x05),
30      //MQTT 5 codes
31      CONNECTION_REFUSED_UNSPECIFIED_ERROR((byte) 0x80),
32      CONNECTION_REFUSED_MALFORMED_PACKET((byte) 0x81),
33      CONNECTION_REFUSED_PROTOCOL_ERROR((byte) 0x82),
34      CONNECTION_REFUSED_IMPLEMENTATION_SPECIFIC((byte) 0x83),
35      CONNECTION_REFUSED_UNSUPPORTED_PROTOCOL_VERSION((byte) 0x84),
36      CONNECTION_REFUSED_CLIENT_IDENTIFIER_NOT_VALID((byte) 0x85),
37      CONNECTION_REFUSED_BAD_USERNAME_OR_PASSWORD((byte) 0x86),
38      CONNECTION_REFUSED_NOT_AUTHORIZED_5((byte) 0x87),
39      CONNECTION_REFUSED_SERVER_UNAVAILABLE_5((byte) 0x88),
40      CONNECTION_REFUSED_SERVER_BUSY((byte) 0x89),
41      CONNECTION_REFUSED_BANNED((byte) 0x8A),
42      CONNECTION_REFUSED_BAD_AUTHENTICATION_METHOD((byte) 0x8C),
43      CONNECTION_REFUSED_TOPIC_NAME_INVALID((byte) 0x90),
44      CONNECTION_REFUSED_PACKET_TOO_LARGE((byte) 0x95),
45      CONNECTION_REFUSED_QUOTA_EXCEEDED((byte) 0x97),
46      CONNECTION_REFUSED_PAYLOAD_FORMAT_INVALID((byte) 0x99),
47      CONNECTION_REFUSED_RETAIN_NOT_SUPPORTED((byte) 0x9A),
48      CONNECTION_REFUSED_QOS_NOT_SUPPORTED((byte) 0x9B),
49      CONNECTION_REFUSED_USE_ANOTHER_SERVER((byte) 0x9C),
50      CONNECTION_REFUSED_SERVER_MOVED((byte) 0x9D),
51      CONNECTION_REFUSED_CONNECTION_RATE_EXCEEDED((byte) 0x9F);
52  
53      private static final MqttConnectReturnCode[] VALUES;
54  
55      static {
56          MqttConnectReturnCode[] values = values();
57          VALUES = new MqttConnectReturnCode[160];
58          for (MqttConnectReturnCode code : values) {
59              final int unsignedByte = code.byteValue & 0xFF;
60              // Suppress a warning about out of bounds access since the enum contains only correct values
61              VALUES[unsignedByte] = code;    // lgtm [java/index-out-of-bounds]
62          }
63      }
64  
65      private final byte byteValue;
66  
67      MqttConnectReturnCode(byte byteValue) {
68          this.byteValue = byteValue;
69      }
70  
71      public byte byteValue() {
72          return byteValue;
73      }
74  
75      public static MqttConnectReturnCode valueOf(byte b) {
76          final int unsignedByte = b & 0xFF;
77          MqttConnectReturnCode mqttConnectReturnCode = null;
78          try {
79              mqttConnectReturnCode = VALUES[unsignedByte];
80          } catch (ArrayIndexOutOfBoundsException ignored) {
81              // no op
82          }
83          if (mqttConnectReturnCode == null) {
84              throw new IllegalArgumentException("unknown connect return code: " + unsignedByte);
85          }
86          return mqttConnectReturnCode;
87      }
88  }