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.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  
55              case UNSUBSCRIBE:
56                  return new MqttUnsubscribeMessage(
57                          mqttFixedHeader,
58                          (MqttMessageIdVariableHeader) variableHeader,
59                          (MqttUnsubscribePayload) payload);
60  
61              case PUBLISH:
62                  return new MqttPublishMessage(
63                          mqttFixedHeader,
64                          (MqttPublishVariableHeader) variableHeader,
65                          (ByteBuf) payload);
66  
67              case PUBACK:
68                  return new MqttPubAckMessage(mqttFixedHeader, (MqttMessageIdVariableHeader) variableHeader);
69              case PUBREC:
70              case PUBREL:
71              case PUBCOMP:
72                  return new MqttMessage(mqttFixedHeader, variableHeader);
73  
74              case PINGREQ:
75              case PINGRESP:
76              case DISCONNECT:
77                  return new MqttMessage(mqttFixedHeader);
78  
79              default:
80                  throw new IllegalArgumentException("unknown message type: " + mqttFixedHeader.messageType());
81          }
82      }
83  
84      public static MqttMessage newInvalidMessage(Throwable cause) {
85          return new MqttMessage(null, null, null, DecoderResult.failure(cause));
86      }
87  
88      private MqttMessageFactory() { }
89  }