Cleanup of codec-mqtt
Motivation: codec-mqtt had some typos and was not restrict enough in terms of making things final and private constructors. Modifications: - Fix typos - Make most pojos final - Remove redundant else blocks. Result: Cleaner and more restrict code.
This commit is contained in:
parent
2c390ae66b
commit
269677820d
@ -42,7 +42,8 @@ final class MqttCodecUtil {
|
|||||||
if (mqttVersion == MqttVersion.MQTT_3_1) {
|
if (mqttVersion == MqttVersion.MQTT_3_1) {
|
||||||
return clientId != null && clientId.length() >= MIN_CLIENT_ID_LENGTH &&
|
return clientId != null && clientId.length() >= MIN_CLIENT_ID_LENGTH &&
|
||||||
clientId.length() <= MAX_CLIENT_ID_LENGTH;
|
clientId.length() <= MAX_CLIENT_ID_LENGTH;
|
||||||
} else if (mqttVersion == MqttVersion.MQTT_3_1_1) {
|
}
|
||||||
|
if (mqttVersion == MqttVersion.MQTT_3_1_1) {
|
||||||
// In 3.1.3.1 Client Identifier of MQTT 3.1.1 specification, The Server MAY allow ClientId’s
|
// In 3.1.3.1 Client Identifier of MQTT 3.1.1 specification, The Server MAY allow ClientId’s
|
||||||
// that contain more than 23 encoded bytes. And, The Server MAY allow zero-length ClientId.
|
// that contain more than 23 encoded bytes. And, The Server MAY allow zero-length ClientId.
|
||||||
return clientId != null;
|
return clientId != null;
|
||||||
|
@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
|
|||||||
/**
|
/**
|
||||||
* Variable header of {@link MqttConnectMessage}
|
* Variable header of {@link MqttConnectMessage}
|
||||||
*/
|
*/
|
||||||
public class MqttConnAckVariableHeader {
|
public final class MqttConnAckVariableHeader {
|
||||||
|
|
||||||
private final MqttConnectReturnCode connectReturnCode;
|
private final MqttConnectReturnCode connectReturnCode;
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
|
|||||||
/**
|
/**
|
||||||
* Payload of {@link MqttConnectMessage}
|
* Payload of {@link MqttConnectMessage}
|
||||||
*/
|
*/
|
||||||
public class MqttConnectPayload {
|
public final class MqttConnectPayload {
|
||||||
|
|
||||||
private final String clientIdentifier;
|
private final String clientIdentifier;
|
||||||
private final String willTopic;
|
private final String willTopic;
|
||||||
|
@ -21,7 +21,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return Code of {@link io.netty.handler.codec.mqtt.MqttConnAckMessage}
|
* Return Code of {@link MqttConnAckMessage}
|
||||||
*/
|
*/
|
||||||
public enum MqttConnectReturnCode {
|
public enum MqttConnectReturnCode {
|
||||||
CONNECTION_ACCEPTED((byte) 0x00),
|
CONNECTION_ACCEPTED((byte) 0x00),
|
||||||
|
@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
|
|||||||
/**
|
/**
|
||||||
* Variable Header for the {@link MqttConnectMessage}
|
* Variable Header for the {@link MqttConnectMessage}
|
||||||
*/
|
*/
|
||||||
public class MqttConnectVariableHeader {
|
public final class MqttConnectVariableHeader {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int version;
|
private final int version;
|
||||||
|
@ -33,7 +33,7 @@ import static io.netty.handler.codec.mqtt.MqttCodecUtil.*;
|
|||||||
* <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html">
|
* <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html">
|
||||||
* the MQTT protocl specification v3.1</a>
|
* the MQTT protocl specification v3.1</a>
|
||||||
*/
|
*/
|
||||||
public class MqttDecoder extends ReplayingDecoder<DecoderState> {
|
public final class MqttDecoder extends ReplayingDecoder<DecoderState> {
|
||||||
|
|
||||||
private static final int DEFAULT_MAX_BYTES_IN_MESSAGE = 8092;
|
private static final int DEFAULT_MAX_BYTES_IN_MESSAGE = 8092;
|
||||||
|
|
||||||
|
@ -21,20 +21,21 @@ import io.netty.buffer.ByteBufAllocator;
|
|||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.handler.codec.MessageToMessageEncoder;
|
import io.netty.handler.codec.MessageToMessageEncoder;
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
|
import io.netty.util.internal.EmptyArrays;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static io.netty.handler.codec.mqtt.MqttCodecUtil.*;
|
import static io.netty.handler.codec.mqtt.MqttCodecUtil.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes Mqtt messages into bytes following the protocl specification v3.1
|
* Encodes Mqtt messages into bytes following the protocol specification v3.1
|
||||||
* as described here <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html">MQTTV3.1</a>
|
* as described here <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html">MQTTV3.1</a>
|
||||||
*/
|
*/
|
||||||
public class MqttEncoder extends MessageToMessageEncoder<MqttMessage> {
|
public final class MqttEncoder extends MessageToMessageEncoder<MqttMessage> {
|
||||||
|
|
||||||
public static final MqttEncoder DEFAUL_ENCODER = new MqttEncoder();
|
public static final MqttEncoder INSTANCE = new MqttEncoder();
|
||||||
|
|
||||||
private static final byte[] EMPTY = new byte[0];
|
private MqttEncoder() { }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void encode(ChannelHandlerContext ctx, MqttMessage msg, List<Object> out) throws Exception {
|
protected void encode(ChannelHandlerContext ctx, MqttMessage msg, List<Object> out) throws Exception {
|
||||||
@ -109,22 +110,22 @@ public class MqttEncoder extends MessageToMessageEncoder<MqttMessage> {
|
|||||||
|
|
||||||
// Will topic and message
|
// Will topic and message
|
||||||
String willTopic = payload.willTopic();
|
String willTopic = payload.willTopic();
|
||||||
byte[] willTopicBytes = willTopic != null ? encodeStringUtf8(willTopic) : EMPTY;
|
byte[] willTopicBytes = willTopic != null ? encodeStringUtf8(willTopic) : EmptyArrays.EMPTY_BYTES;
|
||||||
String willMessage = payload.willMessage();
|
String willMessage = payload.willMessage();
|
||||||
byte[] willMessageBytes = willMessage != null ? encodeStringUtf8(willMessage) : EMPTY;
|
byte[] willMessageBytes = willMessage != null ? encodeStringUtf8(willMessage) : EmptyArrays.EMPTY_BYTES;
|
||||||
if (variableHeader.isWillFlag()) {
|
if (variableHeader.isWillFlag()) {
|
||||||
payloadBufferSize += 2 + willTopicBytes.length;
|
payloadBufferSize += 2 + willTopicBytes.length;
|
||||||
payloadBufferSize += 2 + willMessageBytes.length;
|
payloadBufferSize += 2 + willMessageBytes.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
String userName = payload.userName();
|
String userName = payload.userName();
|
||||||
byte[] userNameBytes = userName != null ? encodeStringUtf8(userName) : EMPTY;
|
byte[] userNameBytes = userName != null ? encodeStringUtf8(userName) : EmptyArrays.EMPTY_BYTES;
|
||||||
if (variableHeader.hasUserName()) {
|
if (variableHeader.hasUserName()) {
|
||||||
payloadBufferSize += 2 + userNameBytes.length;
|
payloadBufferSize += 2 + userNameBytes.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
String password = payload.password();
|
String password = payload.password();
|
||||||
byte[] passwordBytes = password != null ? encodeStringUtf8(password) : EMPTY;
|
byte[] passwordBytes = password != null ? encodeStringUtf8(password) : EmptyArrays.EMPTY_BYTES;
|
||||||
if (variableHeader.hasPassword()) {
|
if (variableHeader.hasPassword()) {
|
||||||
payloadBufferSize += 2 + passwordBytes.length;
|
payloadBufferSize += 2 + passwordBytes.length;
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,14 @@
|
|||||||
|
|
||||||
package io.netty.handler.codec.mqtt;
|
package io.netty.handler.codec.mqtt;
|
||||||
|
|
||||||
|
import io.netty.util.internal.ObjectUtil;
|
||||||
import io.netty.util.internal.StringUtil;
|
import io.netty.util.internal.StringUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#fixed-header">
|
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#fixed-header">
|
||||||
* MQTTV3.1/fixed-header</a>
|
* MQTTV3.1/fixed-header</a>
|
||||||
*/
|
*/
|
||||||
public class MqttFixedHeader {
|
public final class MqttFixedHeader {
|
||||||
|
|
||||||
private final MqttMessageType messageType;
|
private final MqttMessageType messageType;
|
||||||
private final boolean isDup;
|
private final boolean isDup;
|
||||||
@ -36,9 +37,9 @@ public class MqttFixedHeader {
|
|||||||
MqttQoS qosLevel,
|
MqttQoS qosLevel,
|
||||||
boolean isRetain,
|
boolean isRetain,
|
||||||
int remainingLength) {
|
int remainingLength) {
|
||||||
this.messageType = messageType;
|
this.messageType = ObjectUtil.checkNotNull(messageType, "messageType");
|
||||||
this.isDup = isDup;
|
this.isDup = isDup;
|
||||||
this.qosLevel = qosLevel;
|
this.qosLevel = ObjectUtil.checkNotNull(qosLevel, "qosLevel");
|
||||||
this.isRetain = isRetain;
|
this.isRetain = isRetain;
|
||||||
this.remainingLength = remainingLength;
|
this.remainingLength = remainingLength;
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ import io.netty.handler.codec.DecoderException;
|
|||||||
/**
|
/**
|
||||||
* A {@link MqttIdentifierRejectedException} which is thrown when a CONNECT request contains invalid client identifier.
|
* A {@link MqttIdentifierRejectedException} which is thrown when a CONNECT request contains invalid client identifier.
|
||||||
*/
|
*/
|
||||||
public class MqttIdentifierRejectedException extends DecoderException {
|
public final class MqttIdentifierRejectedException extends DecoderException {
|
||||||
|
|
||||||
private static final long serialVersionUID = -1323503322689614981L;
|
private static final long serialVersionUID = -1323503322689614981L;
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ package io.netty.handler.codec.mqtt;
|
|||||||
/**
|
/**
|
||||||
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#puback">MQTTV3.1/puback</a>
|
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#puback">MQTTV3.1/puback</a>
|
||||||
*/
|
*/
|
||||||
public class MqttPubAckMessage extends MqttMessage {
|
public final class MqttPubAckMessage extends MqttMessage {
|
||||||
|
|
||||||
public MqttPubAckMessage(MqttFixedHeader mqttFixedHeader, MqttMessageIdVariableHeader variableHeader) {
|
public MqttPubAckMessage(MqttFixedHeader mqttFixedHeader, MqttMessageIdVariableHeader variableHeader) {
|
||||||
super(mqttFixedHeader, variableHeader);
|
super(mqttFixedHeader, variableHeader);
|
||||||
|
@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
|
|||||||
/**
|
/**
|
||||||
* Variable Header of the {@link MqttPublishMessage}
|
* Variable Header of the {@link MqttPublishMessage}
|
||||||
*/
|
*/
|
||||||
public class MqttPublishVariableHeader {
|
public final class MqttPublishVariableHeader {
|
||||||
|
|
||||||
private final String topicName;
|
private final String topicName;
|
||||||
private final int messageId;
|
private final int messageId;
|
||||||
|
@ -19,7 +19,7 @@ package io.netty.handler.codec.mqtt;
|
|||||||
/**
|
/**
|
||||||
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#suback">MQTTV3.1/suback</a>
|
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#suback">MQTTV3.1/suback</a>
|
||||||
*/
|
*/
|
||||||
public class MqttSubAckMessage extends MqttMessage {
|
public final class MqttSubAckMessage extends MqttMessage {
|
||||||
|
|
||||||
public MqttSubAckMessage(
|
public MqttSubAckMessage(
|
||||||
MqttFixedHeader mqttFixedHeader,
|
MqttFixedHeader mqttFixedHeader,
|
||||||
|
@ -20,7 +20,7 @@ package io.netty.handler.codec.mqtt;
|
|||||||
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#subscribe">
|
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#subscribe">
|
||||||
* MQTTV3.1/subscribe</a>
|
* MQTTV3.1/subscribe</a>
|
||||||
*/
|
*/
|
||||||
public class MqttSubscribeMessage extends MqttMessage {
|
public final class MqttSubscribeMessage extends MqttMessage {
|
||||||
|
|
||||||
public MqttSubscribeMessage(
|
public MqttSubscribeMessage(
|
||||||
MqttFixedHeader mqttFixedHeader,
|
MqttFixedHeader mqttFixedHeader,
|
||||||
|
@ -24,7 +24,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Payload of the {@link MqttSubscribeMessage}
|
* Payload of the {@link MqttSubscribeMessage}
|
||||||
*/
|
*/
|
||||||
public class MqttSubscribePayload {
|
public final class MqttSubscribePayload {
|
||||||
|
|
||||||
private final List<MqttTopicSubscription> topicSubscriptions;
|
private final List<MqttTopicSubscription> topicSubscriptions;
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ import io.netty.util.internal.StringUtil;
|
|||||||
* Contains a topic name and Qos Level.
|
* Contains a topic name and Qos Level.
|
||||||
* This is part of the {@link MqttSubscribePayload}
|
* This is part of the {@link MqttSubscribePayload}
|
||||||
*/
|
*/
|
||||||
public class MqttTopicSubscription {
|
public final class MqttTopicSubscription {
|
||||||
|
|
||||||
private final String topicFilter;
|
private final String topicFilter;
|
||||||
private final MqttQoS qualityOfService;
|
private final MqttQoS qualityOfService;
|
||||||
|
@ -21,7 +21,7 @@ import io.netty.handler.codec.DecoderException;
|
|||||||
* A {@link MqttUnacceptableProtocolVersionException} which is thrown when
|
* A {@link MqttUnacceptableProtocolVersionException} which is thrown when
|
||||||
* a CONNECT request contains unacceptable protocol version.
|
* a CONNECT request contains unacceptable protocol version.
|
||||||
*/
|
*/
|
||||||
public class MqttUnacceptableProtocolVersionException extends DecoderException {
|
public final class MqttUnacceptableProtocolVersionException extends DecoderException {
|
||||||
|
|
||||||
private static final long serialVersionUID = 4914652213232455749L;
|
private static final long serialVersionUID = 4914652213232455749L;
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ package io.netty.handler.codec.mqtt;
|
|||||||
/**
|
/**
|
||||||
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#unsuback">MQTTV3.1/unsuback</a>
|
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#unsuback">MQTTV3.1/unsuback</a>
|
||||||
*/
|
*/
|
||||||
public class MqttUnsubAckMessage extends MqttMessage {
|
public final class MqttUnsubAckMessage extends MqttMessage {
|
||||||
|
|
||||||
public MqttUnsubAckMessage(MqttFixedHeader mqttFixedHeader, MqttMessageIdVariableHeader variableHeader) {
|
public MqttUnsubAckMessage(MqttFixedHeader mqttFixedHeader, MqttMessageIdVariableHeader variableHeader) {
|
||||||
super(mqttFixedHeader, variableHeader, null);
|
super(mqttFixedHeader, variableHeader, null);
|
||||||
|
@ -20,7 +20,7 @@ package io.netty.handler.codec.mqtt;
|
|||||||
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#unsubscribe">
|
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#unsubscribe">
|
||||||
* MQTTV3.1/unsubscribe</a>
|
* MQTTV3.1/unsubscribe</a>
|
||||||
*/
|
*/
|
||||||
public class MqttUnsubscribeMessage extends MqttMessage {
|
public final class MqttUnsubscribeMessage extends MqttMessage {
|
||||||
|
|
||||||
public MqttUnsubscribeMessage(
|
public MqttUnsubscribeMessage(
|
||||||
MqttFixedHeader mqttFixedHeader,
|
MqttFixedHeader mqttFixedHeader,
|
||||||
|
@ -24,7 +24,7 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Pyaload of the {@link MqttUnsubscribeMessage}
|
* Pyaload of the {@link MqttUnsubscribeMessage}
|
||||||
*/
|
*/
|
||||||
public class MqttUnsubscribePayload {
|
public final class MqttUnsubscribePayload {
|
||||||
|
|
||||||
private final List<String> topics;
|
private final List<String> topics;
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package io.netty.handler.codec.mqtt;
|
package io.netty.handler.codec.mqtt;
|
||||||
|
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
|
import io.netty.util.internal.ObjectUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mqtt version specific constant values used by multiple classes in mqtt-codec.
|
* Mqtt version specific constant values used by multiple classes in mqtt-codec.
|
||||||
@ -25,12 +26,12 @@ public enum MqttVersion {
|
|||||||
MQTT_3_1("MQIsdp", (byte) 3),
|
MQTT_3_1("MQIsdp", (byte) 3),
|
||||||
MQTT_3_1_1("MQTT", (byte) 4);
|
MQTT_3_1_1("MQTT", (byte) 4);
|
||||||
|
|
||||||
private String name;
|
private final String name;
|
||||||
private byte level;
|
private final byte level;
|
||||||
|
|
||||||
private MqttVersion(String protocolName, byte protocolLevel) {
|
MqttVersion(String protocolName, byte protocolLevel) {
|
||||||
this.name = protocolName;
|
name = ObjectUtil.checkNotNull(protocolName, "protocolName");
|
||||||
this.level = protocolLevel;
|
level = protocolLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String protocolName() {
|
public String protocolName() {
|
||||||
@ -58,5 +59,4 @@ public enum MqttVersion {
|
|||||||
}
|
}
|
||||||
throw new MqttUnacceptableProtocolVersionException(protocolName + "is unknown protocol name");
|
throw new MqttUnacceptableProtocolVersionException(protocolName + "is unknown protocol name");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user