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.buffer.ByteBuf;
20  import io.netty.handler.codec.DecoderResult;
21  
22  /**
23   * Utility class with factory methods to create different types of MQTT messages.
24   */
25  public final class MqttMessageFactory {
26  
27      public static MqttMessage newMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader, Object payload) {
28          switch (mqttFixedHeader.messageType()) {
29              case CONNECT :
30                  return new MqttConnectMessage(
31                          mqttFixedHeader,
32                          (MqttConnectVariableHeader) variableHeader,
33                          (MqttConnectPayload) payload);
34  
35              case CONNACK:
36                  return new MqttConnAckMessage(mqttFixedHeader, (MqttConnAckVariableHeader) variableHeader);
37  
38              case SUBSCRIBE:
39                  return new MqttSubscribeMessage(
40                          mqttFixedHeader,
41                          (MqttMessageIdVariableHeader) variableHeader,
42                          (MqttSubscribePayload) payload);
43  
44              case SUBACK:
45                  return new MqttSubAckMessage(
46                          mqttFixedHeader,
47                          (MqttMessageIdVariableHeader) variableHeader,
48                          (MqttSubAckPayload) payload);
49  
50              case UNSUBACK:
51                  return new MqttUnsubAckMessage(
52                          mqttFixedHeader,
53                          (MqttMessageIdVariableHeader) variableHeader,
54                          (MqttUnsubAckPayload) payload);
55  
56              case UNSUBSCRIBE:
57                  return new MqttUnsubscribeMessage(
58                          mqttFixedHeader,
59                          (MqttMessageIdVariableHeader) variableHeader,
60                          (MqttUnsubscribePayload) payload);
61  
62              case PUBLISH:
63                  return new MqttPublishMessage(
64                          mqttFixedHeader,
65                          (MqttPublishVariableHeader) variableHeader,
66                          (ByteBuf) payload);
67  
68              case PUBACK:
69                  //Having MqttPubReplyMessageVariableHeader or MqttMessageIdVariableHeader
70                  return new MqttPubAckMessage(mqttFixedHeader, (MqttMessageIdVariableHeader) variableHeader);
71              case PUBREC:
72              case PUBREL:
73              case PUBCOMP:
74                  //Having MqttPubReplyMessageVariableHeader or MqttMessageIdVariableHeader
75                  return new MqttMessage(mqttFixedHeader, variableHeader);
76  
77              case PINGREQ:
78              case PINGRESP:
79                  return new MqttMessage(mqttFixedHeader);
80  
81              case DISCONNECT:
82              case AUTH:
83                  //Having MqttReasonCodeAndPropertiesVariableHeader
84                  return new MqttMessage(mqttFixedHeader,
85                          variableHeader);
86  
87              default:
88                  throw new IllegalArgumentException("unknown message type: " + mqttFixedHeader.messageType());
89          }
90      }
91  
92      public static MqttMessage newInvalidMessage(Throwable cause) {
93          return new MqttMessage(null, null, null, DecoderResult.failure(cause));
94      }
95  
96      public static MqttMessage newInvalidMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader,
97                                                  Throwable cause) {
98          return new MqttMessage(mqttFixedHeader, variableHeader, null, DecoderResult.failure(cause));
99      }
100 
101     private MqttMessageFactory() { }
102 }