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  import io.netty.handler.codec.DecoderResult;
20  import io.netty.util.internal.StringUtil;
21  
22  /**
23   * Base class for all MQTT message types.
24   */
25  public class MqttMessage {
26  
27      private final MqttFixedHeader mqttFixedHeader;
28      private final Object variableHeader;
29      private final Object payload;
30      private final DecoderResult decoderResult;
31  
32      // Constants for fixed-header only message types with all flags set to 0 (see
33      // https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.2_-)
34      public static final MqttMessage PINGREQ = new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGREQ, false,
35              MqttQoS.AT_MOST_ONCE, false, 0));
36  
37      public static final MqttMessage PINGRESP = new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGRESP, false,
38              MqttQoS.AT_MOST_ONCE, false, 0));
39  
40      public static final MqttMessage DISCONNECT = new MqttMessage(new MqttFixedHeader(MqttMessageType.DISCONNECT, false,
41              MqttQoS.AT_MOST_ONCE, false, 0));
42  
43      public MqttMessage(MqttFixedHeader mqttFixedHeader) {
44          this(mqttFixedHeader, null, null);
45      }
46  
47      public MqttMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader) {
48          this(mqttFixedHeader, variableHeader, null);
49      }
50  
51      public MqttMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader, Object payload) {
52          this(mqttFixedHeader, variableHeader, payload, DecoderResult.SUCCESS);
53      }
54  
55      public MqttMessage(
56              MqttFixedHeader mqttFixedHeader,
57              Object variableHeader,
58              Object payload,
59              DecoderResult decoderResult) {
60          this.mqttFixedHeader = mqttFixedHeader;
61          this.variableHeader = variableHeader;
62          this.payload = payload;
63          this.decoderResult = decoderResult;
64      }
65  
66      public MqttFixedHeader fixedHeader() {
67          return mqttFixedHeader;
68      }
69  
70      public Object variableHeader() {
71          return variableHeader;
72      }
73  
74      public Object payload() {
75          return payload;
76      }
77  
78      public DecoderResult decoderResult() {
79          return decoderResult;
80      }
81  
82      @Override
83      public String toString() {
84          return new StringBuilder(StringUtil.simpleClassName(this))
85              .append('[')
86              .append("fixedHeader=").append(fixedHeader() != null ? fixedHeader().toString() : "")
87              .append(", variableHeader=").append(variableHeader() != null ? variableHeader.toString() : "")
88              .append(", payload=").append(payload() != null ? payload.toString() : "")
89              .append(']')
90              .toString();
91      }
92  }