View Javadoc
1   /*
2    * Copyright 2017 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  package io.netty.handler.codec.mqtt;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.util.CharsetUtil;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  public final class MqttMessageBuilders {
26  
27      public static final class PublishBuilder {
28          private String topic;
29          private boolean retained;
30          private MqttQoS qos;
31          private ByteBuf payload;
32          private int messageId;
33  
34          PublishBuilder() {
35          }
36  
37          public PublishBuilder topicName(String topic) {
38              this.topic = topic;
39              return this;
40          }
41  
42          public PublishBuilder retained(boolean retained) {
43              this.retained = retained;
44              return this;
45          }
46  
47          public PublishBuilder qos(MqttQoS qos) {
48              this.qos = qos;
49              return this;
50          }
51  
52          public PublishBuilder payload(ByteBuf payload) {
53              this.payload = payload;
54              return this;
55          }
56  
57          public PublishBuilder messageId(int messageId) {
58              this.messageId = messageId;
59              return this;
60          }
61  
62          public MqttPublishMessage build() {
63              MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, qos, retained, 0);
64              MqttPublishVariableHeader mqttVariableHeader = new MqttPublishVariableHeader(topic, messageId);
65              return new MqttPublishMessage(mqttFixedHeader, mqttVariableHeader, Unpooled.buffer().writeBytes(payload));
66          }
67      }
68  
69      public static final class ConnectBuilder {
70  
71          private MqttVersion version = MqttVersion.MQTT_3_1_1;
72          private String clientId;
73          private boolean cleanSession;
74          private boolean hasUser;
75          private boolean hasPassword;
76          private int keepAliveSecs;
77          private boolean willFlag;
78          private boolean willRetain;
79          private MqttQoS willQos = MqttQoS.AT_MOST_ONCE;
80          private String willTopic;
81          private byte[] willMessage;
82          private String username;
83          private byte[] password;
84  
85          ConnectBuilder() {
86          }
87  
88          public ConnectBuilder protocolVersion(MqttVersion version) {
89              this.version = version;
90              return this;
91          }
92  
93          public ConnectBuilder clientId(String clientId) {
94              this.clientId = clientId;
95              return this;
96          }
97  
98          public ConnectBuilder cleanSession(boolean cleanSession) {
99              this.cleanSession = cleanSession;
100             return this;
101         }
102 
103         public ConnectBuilder keepAlive(int keepAliveSecs) {
104             this.keepAliveSecs = keepAliveSecs;
105             return this;
106         }
107 
108         public ConnectBuilder willFlag(boolean willFlag) {
109             this.willFlag = willFlag;
110             return this;
111         }
112 
113         public ConnectBuilder willQoS(MqttQoS willQos) {
114             this.willQos = willQos;
115             return this;
116         }
117 
118         public ConnectBuilder willTopic(String willTopic) {
119             this.willTopic = willTopic;
120             return this;
121         }
122 
123         /**
124          * @deprecated use {@link ConnectBuilder#willMessage(byte[])} instead
125          */
126         @Deprecated
127         public ConnectBuilder willMessage(String willMessage) {
128             willMessage(willMessage == null ? null : willMessage.getBytes(CharsetUtil.UTF_8));
129             return this;
130         }
131 
132         public ConnectBuilder willMessage(byte[] willMessage) {
133             this.willMessage = willMessage;
134             return this;
135         }
136 
137         public ConnectBuilder willRetain(boolean willRetain) {
138             this.willRetain = willRetain;
139             return this;
140         }
141 
142         public ConnectBuilder hasUser(boolean value) {
143             this.hasUser = value;
144             return this;
145         }
146 
147         public ConnectBuilder hasPassword(boolean value) {
148             this.hasPassword = value;
149             return this;
150         }
151 
152         public ConnectBuilder username(String username) {
153             this.hasUser = username != null;
154             this.username = username;
155             return this;
156         }
157 
158         /**
159          * @deprecated use {@link ConnectBuilder#password(byte[])} instead
160          */
161         @Deprecated
162         public ConnectBuilder password(String password) {
163             password(password == null ? null : password.getBytes(CharsetUtil.UTF_8));
164             return this;
165         }
166 
167         public ConnectBuilder password(byte[] password) {
168             this.hasPassword = password != null;
169             this.password = password;
170             return this;
171         }
172 
173         public MqttConnectMessage build() {
174             MqttFixedHeader mqttFixedHeader =
175                     new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
176             MqttConnectVariableHeader mqttConnectVariableHeader =
177                     new MqttConnectVariableHeader(
178                             version.protocolName(),
179                             version.protocolLevel(),
180                             hasUser,
181                             hasPassword,
182                             willRetain,
183                             willQos.value(),
184                             willFlag,
185                             cleanSession,
186                             keepAliveSecs);
187             MqttConnectPayload mqttConnectPayload =
188                     new MqttConnectPayload(clientId, willTopic, willMessage, username, password);
189             return new MqttConnectMessage(mqttFixedHeader, mqttConnectVariableHeader, mqttConnectPayload);
190         }
191     }
192 
193     public static final class SubscribeBuilder {
194 
195         private List<MqttTopicSubscription> subscriptions;
196         private int messageId;
197 
198         SubscribeBuilder() {
199         }
200 
201         public SubscribeBuilder addSubscription(MqttQoS qos, String topic) {
202             if (subscriptions == null) {
203                 subscriptions = new ArrayList<MqttTopicSubscription>(5);
204             }
205             subscriptions.add(new MqttTopicSubscription(topic, qos));
206             return this;
207         }
208 
209         public SubscribeBuilder messageId(int messageId) {
210             this.messageId = messageId;
211             return this;
212         }
213 
214         public MqttSubscribeMessage build() {
215             MqttFixedHeader mqttFixedHeader =
216                     new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, 0);
217             MqttMessageIdVariableHeader mqttVariableHeader = MqttMessageIdVariableHeader.from(messageId);
218             MqttSubscribePayload mqttSubscribePayload = new MqttSubscribePayload(subscriptions);
219             return new MqttSubscribeMessage(mqttFixedHeader, mqttVariableHeader, mqttSubscribePayload);
220         }
221     }
222 
223     public static final class UnsubscribeBuilder {
224 
225         private List<String> topicFilters;
226         private int messageId;
227 
228         UnsubscribeBuilder() {
229         }
230 
231         public UnsubscribeBuilder addTopicFilter(String topic) {
232             if (topicFilters == null) {
233                 topicFilters = new ArrayList<String>(5);
234             }
235             topicFilters.add(topic);
236             return this;
237         }
238 
239         public UnsubscribeBuilder messageId(int messageId) {
240             this.messageId = messageId;
241             return this;
242         }
243 
244         public MqttUnsubscribeMessage build() {
245             MqttFixedHeader mqttFixedHeader =
246                     new MqttFixedHeader(MqttMessageType.UNSUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, 0);
247             MqttMessageIdVariableHeader mqttVariableHeader = MqttMessageIdVariableHeader.from(messageId);
248             MqttUnsubscribePayload mqttSubscribePayload = new MqttUnsubscribePayload(topicFilters);
249             return new MqttUnsubscribeMessage(mqttFixedHeader, mqttVariableHeader, mqttSubscribePayload);
250         }
251     }
252 
253     public static final class ConnAckBuilder {
254 
255         private MqttConnectReturnCode returnCode;
256         private boolean sessionPresent;
257 
258         ConnAckBuilder() {
259         }
260 
261         public ConnAckBuilder returnCode(MqttConnectReturnCode returnCode) {
262             this.returnCode = returnCode;
263             return this;
264         }
265 
266         public ConnAckBuilder sessionPresent(boolean sessionPresent) {
267             this.sessionPresent = sessionPresent;
268             return this;
269         }
270 
271         public MqttConnAckMessage build() {
272             MqttFixedHeader mqttFixedHeader =
273                     new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
274             MqttConnAckVariableHeader mqttConnAckVariableHeader =
275                     new MqttConnAckVariableHeader(returnCode, sessionPresent);
276             return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
277         }
278     }
279 
280     public static ConnectBuilder connect() {
281         return new ConnectBuilder();
282     }
283 
284     public static ConnAckBuilder connAck() {
285         return new ConnAckBuilder();
286     }
287 
288     public static PublishBuilder publish() {
289         return new PublishBuilder();
290     }
291 
292     public static SubscribeBuilder subscribe() {
293         return new SubscribeBuilder();
294     }
295 
296     public static UnsubscribeBuilder unsubscribe() {
297         return new UnsubscribeBuilder();
298     }
299 
300     private MqttMessageBuilders() {
301     }
302 }