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 java.util.Arrays;
20  
21  import io.netty.util.CharsetUtil;
22  import io.netty.util.internal.StringUtil;
23  
24  /**
25   * Payload of {@link MqttConnectMessage}
26   */
27  public final class MqttConnectPayload {
28  
29      private final String clientIdentifier;
30      private final MqttProperties willProperties;
31      private final String willTopic;
32      private final byte[] willMessage;
33      private final String userName;
34      private final byte[] password;
35  
36      /**
37       * @deprecated use {@link MqttConnectPayload#MqttConnectPayload(String,
38       * MqttProperties, String, byte[], String, byte[])} instead
39       */
40      @Deprecated
41      public MqttConnectPayload(
42              String clientIdentifier,
43              String willTopic,
44              String willMessage,
45              String userName,
46              String password) {
47          this(
48            clientIdentifier,
49            MqttProperties.NO_PROPERTIES,
50            willTopic,
51            willMessage.getBytes(CharsetUtil.UTF_8),
52            userName,
53            password.getBytes(CharsetUtil.UTF_8));
54      }
55  
56      public MqttConnectPayload(
57              String clientIdentifier,
58              String willTopic,
59              byte[] willMessage,
60              String userName,
61              byte[] password) {
62          this(clientIdentifier,
63                  MqttProperties.NO_PROPERTIES,
64                  willTopic,
65                  willMessage,
66                  userName,
67                  password);
68      }
69  
70      public MqttConnectPayload(
71              String clientIdentifier,
72              MqttProperties willProperties,
73              String willTopic,
74              byte[] willMessage,
75              String userName,
76              byte[] password) {
77          this.clientIdentifier = clientIdentifier;
78          this.willProperties = MqttProperties.withEmptyDefaults(willProperties);
79          this.willTopic = willTopic;
80          this.willMessage = willMessage;
81          this.userName = userName;
82          this.password = password;
83      }
84  
85      public String clientIdentifier() {
86          return clientIdentifier;
87      }
88  
89      public MqttProperties willProperties() {
90          return willProperties;
91      }
92  
93      public String willTopic() {
94          return willTopic;
95      }
96  
97      /**
98       * @deprecated use {@link MqttConnectPayload#willMessageInBytes()} instead
99       */
100     @Deprecated
101     public String willMessage() {
102         return willMessage == null ? null : new String(willMessage, CharsetUtil.UTF_8);
103     }
104 
105     public byte[] willMessageInBytes() {
106         return willMessage;
107     }
108 
109     public String userName() {
110         return userName;
111     }
112 
113     /**
114      * @deprecated use {@link MqttConnectPayload#passwordInBytes()} instead
115      */
116     @Deprecated
117     public String password() {
118         return password == null ? null : new String(password, CharsetUtil.UTF_8);
119     }
120 
121     public byte[] passwordInBytes() {
122         return password;
123     }
124 
125     @Override
126     public String toString() {
127         return new StringBuilder(StringUtil.simpleClassName(this))
128             .append('[')
129             .append("clientIdentifier=").append(clientIdentifier)
130             .append(", willTopic=").append(willTopic)
131             .append(", willMessage=").append(Arrays.toString(willMessage))
132             .append(", userName=").append(userName)
133             .append(", password=").append(Arrays.toString(password))
134             .append(']')
135             .toString();
136     }
137 }