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:
Norman Maurer 2016-03-22 14:29:34 +01:00
parent 2c390ae66b
commit 269677820d
20 changed files with 37 additions and 34 deletions

View File

@ -42,7 +42,8 @@ final class MqttCodecUtil {
if (mqttVersion == MqttVersion.MQTT_3_1) {
return clientId != null && clientId.length() >= MIN_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 ClientIds
// that contain more than 23 encoded bytes. And, The Server MAY allow zero-length ClientId.
return clientId != null;

View File

@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
/**
* Variable header of {@link MqttConnectMessage}
*/
public class MqttConnAckVariableHeader {
public final class MqttConnAckVariableHeader {
private final MqttConnectReturnCode connectReturnCode;

View File

@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
/**
* Payload of {@link MqttConnectMessage}
*/
public class MqttConnectPayload {
public final class MqttConnectPayload {
private final String clientIdentifier;
private final String willTopic;

View File

@ -21,7 +21,7 @@ import java.util.HashMap;
import java.util.Map;
/**
* Return Code of {@link io.netty.handler.codec.mqtt.MqttConnAckMessage}
* Return Code of {@link MqttConnAckMessage}
*/
public enum MqttConnectReturnCode {
CONNECTION_ACCEPTED((byte) 0x00),

View File

@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
/**
* Variable Header for the {@link MqttConnectMessage}
*/
public class MqttConnectVariableHeader {
public final class MqttConnectVariableHeader {
private final String name;
private final int version;

View File

@ -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">
* 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;

View File

@ -21,20 +21,21 @@ import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.EmptyArrays;
import java.util.List;
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>
*/
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
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
String willTopic = payload.willTopic();
byte[] willTopicBytes = willTopic != null ? encodeStringUtf8(willTopic) : EMPTY;
byte[] willTopicBytes = willTopic != null ? encodeStringUtf8(willTopic) : EmptyArrays.EMPTY_BYTES;
String willMessage = payload.willMessage();
byte[] willMessageBytes = willMessage != null ? encodeStringUtf8(willMessage) : EMPTY;
byte[] willMessageBytes = willMessage != null ? encodeStringUtf8(willMessage) : EmptyArrays.EMPTY_BYTES;
if (variableHeader.isWillFlag()) {
payloadBufferSize += 2 + willTopicBytes.length;
payloadBufferSize += 2 + willMessageBytes.length;
}
String userName = payload.userName();
byte[] userNameBytes = userName != null ? encodeStringUtf8(userName) : EMPTY;
byte[] userNameBytes = userName != null ? encodeStringUtf8(userName) : EmptyArrays.EMPTY_BYTES;
if (variableHeader.hasUserName()) {
payloadBufferSize += 2 + userNameBytes.length;
}
String password = payload.password();
byte[] passwordBytes = password != null ? encodeStringUtf8(password) : EMPTY;
byte[] passwordBytes = password != null ? encodeStringUtf8(password) : EmptyArrays.EMPTY_BYTES;
if (variableHeader.hasPassword()) {
payloadBufferSize += 2 + passwordBytes.length;
}

View File

@ -16,13 +16,14 @@
package io.netty.handler.codec.mqtt;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil;
/**
* See <a href="http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#fixed-header">
* MQTTV3.1/fixed-header</a>
*/
public class MqttFixedHeader {
public final class MqttFixedHeader {
private final MqttMessageType messageType;
private final boolean isDup;
@ -36,9 +37,9 @@ public class MqttFixedHeader {
MqttQoS qosLevel,
boolean isRetain,
int remainingLength) {
this.messageType = messageType;
this.messageType = ObjectUtil.checkNotNull(messageType, "messageType");
this.isDup = isDup;
this.qosLevel = qosLevel;
this.qosLevel = ObjectUtil.checkNotNull(qosLevel, "qosLevel");
this.isRetain = isRetain;
this.remainingLength = remainingLength;
}

View File

@ -20,7 +20,7 @@ import io.netty.handler.codec.DecoderException;
/**
* 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;

View File

@ -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>
*/
public class MqttPubAckMessage extends MqttMessage {
public final class MqttPubAckMessage extends MqttMessage {
public MqttPubAckMessage(MqttFixedHeader mqttFixedHeader, MqttMessageIdVariableHeader variableHeader) {
super(mqttFixedHeader, variableHeader);

View File

@ -21,7 +21,7 @@ import io.netty.util.internal.StringUtil;
/**
* Variable Header of the {@link MqttPublishMessage}
*/
public class MqttPublishVariableHeader {
public final class MqttPublishVariableHeader {
private final String topicName;
private final int messageId;

View File

@ -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>
*/
public class MqttSubAckMessage extends MqttMessage {
public final class MqttSubAckMessage extends MqttMessage {
public MqttSubAckMessage(
MqttFixedHeader mqttFixedHeader,

View File

@ -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">
* MQTTV3.1/subscribe</a>
*/
public class MqttSubscribeMessage extends MqttMessage {
public final class MqttSubscribeMessage extends MqttMessage {
public MqttSubscribeMessage(
MqttFixedHeader mqttFixedHeader,

View File

@ -24,7 +24,7 @@ import java.util.List;
/**
* Payload of the {@link MqttSubscribeMessage}
*/
public class MqttSubscribePayload {
public final class MqttSubscribePayload {
private final List<MqttTopicSubscription> topicSubscriptions;

View File

@ -22,7 +22,7 @@ import io.netty.util.internal.StringUtil;
* Contains a topic name and Qos Level.
* This is part of the {@link MqttSubscribePayload}
*/
public class MqttTopicSubscription {
public final class MqttTopicSubscription {
private final String topicFilter;
private final MqttQoS qualityOfService;

View File

@ -21,7 +21,7 @@ import io.netty.handler.codec.DecoderException;
* A {@link MqttUnacceptableProtocolVersionException} which is thrown when
* a CONNECT request contains unacceptable protocol version.
*/
public class MqttUnacceptableProtocolVersionException extends DecoderException {
public final class MqttUnacceptableProtocolVersionException extends DecoderException {
private static final long serialVersionUID = 4914652213232455749L;

View File

@ -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>
*/
public class MqttUnsubAckMessage extends MqttMessage {
public final class MqttUnsubAckMessage extends MqttMessage {
public MqttUnsubAckMessage(MqttFixedHeader mqttFixedHeader, MqttMessageIdVariableHeader variableHeader) {
super(mqttFixedHeader, variableHeader, null);

View File

@ -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">
* MQTTV3.1/unsubscribe</a>
*/
public class MqttUnsubscribeMessage extends MqttMessage {
public final class MqttUnsubscribeMessage extends MqttMessage {
public MqttUnsubscribeMessage(
MqttFixedHeader mqttFixedHeader,

View File

@ -24,7 +24,7 @@ import java.util.List;
/**
* Pyaload of the {@link MqttUnsubscribeMessage}
*/
public class MqttUnsubscribePayload {
public final class MqttUnsubscribePayload {
private final List<String> topics;

View File

@ -17,6 +17,7 @@
package io.netty.handler.codec.mqtt;
import io.netty.util.CharsetUtil;
import io.netty.util.internal.ObjectUtil;
/**
* 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_1("MQTT", (byte) 4);
private String name;
private byte level;
private final String name;
private final byte level;
private MqttVersion(String protocolName, byte protocolLevel) {
this.name = protocolName;
this.level = protocolLevel;
MqttVersion(String protocolName, byte protocolLevel) {
name = ObjectUtil.checkNotNull(protocolName, "protocolName");
level = protocolLevel;
}
public String protocolName() {
@ -58,5 +59,4 @@ public enum MqttVersion {
}
throw new MqttUnacceptableProtocolVersionException(protocolName + "is unknown protocol name");
}
}