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    *   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  
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      public MqttMessage(MqttFixedHeader mqttFixedHeader) {
33          this(mqttFixedHeader, null, null);
34      }
35  
36      public MqttMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader) {
37          this(mqttFixedHeader, variableHeader, null);
38      }
39  
40      public MqttMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader, Object payload) {
41          this(mqttFixedHeader, variableHeader, payload, DecoderResult.SUCCESS);
42      }
43  
44      public MqttMessage(
45              MqttFixedHeader mqttFixedHeader,
46              Object variableHeader,
47              Object payload,
48              DecoderResult decoderResult) {
49          this.mqttFixedHeader = mqttFixedHeader;
50          this.variableHeader = variableHeader;
51          this.payload = payload;
52          this.decoderResult = decoderResult;
53      }
54  
55      public MqttFixedHeader fixedHeader() {
56          return mqttFixedHeader;
57      }
58  
59      public Object variableHeader() {
60          return variableHeader;
61      }
62  
63      public Object payload() {
64          return payload;
65      }
66  
67      public DecoderResult decoderResult() {
68          return decoderResult;
69      }
70  
71      @Override
72      public String toString() {
73          return new StringBuilder(StringUtil.simpleClassName(this))
74              .append('[')
75              .append("fixedHeader=").append(fixedHeader() != null ? fixedHeader().toString() : "")
76              .append(", variableHeader=").append(variableHeader() != null ? variableHeader.toString() : "")
77              .append(", payload=").append(payload() != null ? payload.toString() : "")
78              .append(']')
79              .toString();
80      }
81  }