@@ -513,14 +513,14 @@ public void send(MqttWireMessage message, MqttToken token) throws MqttException
513513 if (message .isMessageIdRequired () && (message .getMessageId () == 0 )) {
514514 if (message instanceof MqttPublish && (((MqttPublish ) message ).getMessage ().getQos () != 0 )) {
515515 message .setMessageId (getNextMessageId ());
516- if (this .mqttSession .getTopicAliasMaximum () > 0 ) {
516+ if (this .mqttSession .getOutgoingTopicAliasMaximum () > 0 ) {
517517 String topic = ((MqttPublish ) message ).getTopicName ();
518518 if (outgoingTopicAliases .containsKey (topic )) {
519519 // Existing Topic Alias, Assign it and remove the topic string
520520 ((MqttPublish ) message ).getProperties ().setTopicAlias (outgoingTopicAliases .get (topic ));
521521 ((MqttPublish ) message ).setTopicName (null );
522522 } else {
523- if (outgoingTopicAliasCount <= this .mqttSession .getTopicAliasMaximum ()) {
523+ if (outgoingTopicAliasCount <= this .mqttSession .getOutgoingTopicAliasMaximum ()) {
524524 // Create a new Topic Alias and increment the counter
525525 ((MqttPublish ) message ).getProperties ().setTopicAlias (outgoingTopicAliasCount );
526526 outgoingTopicAliases .put (((MqttPublish ) message ).getTopicName (), outgoingTopicAliasCount );
@@ -1117,41 +1117,44 @@ protected void notifyReceivedMsg(MqttWireMessage message) throws MqttException {
11171117 if (!quiescing ) {
11181118 if (message instanceof MqttPublish ) {
11191119 MqttPublish send = (MqttPublish ) message ;
1120- // If using Topic Aliases, restore the Topic Name or create the new Alias
1121- if (send .getTopicName () != null && send .getProperties ().getTopicAlias () != 0 ) {
1122- // We've been sent an new topic alias
1123-
1124- // Do we have space for this alias? / Are aliases enabled?
1125- if (incomingTopicAliases .size () < clientComms .getConOptions ().getTopicAliasMaximum ()
1126- && clientComms .getConOptions ().getTopicAliasMaximum () > 0 ) {
1120+
1121+ // Do we have an incoming topic Alias?
1122+ if (send .getProperties ().getTopicAlias () != null && send .getProperties ().getTopicAlias () != 0 ) {
1123+ int incomingTopicAlias = send .getProperties ().getTopicAlias ();
1124+
1125+ // Are incoming Topic Aliases enabled / is it a valid Alias?
1126+ if (incomingTopicAlias > this .mqttSession .getIncomingTopicAliasMax () || incomingTopicAlias == 0 ) {
1127+ // @TRACE 653=Invalid Topic Alias: topicAliasMax={0}, publishTopicAlias={1}
1128+ log .severe (CLASS_NAME , methodName , "653" ,
1129+ new Object [] { Integer .valueOf (this .mqttSession .getIncomingTopicAliasMax ()),
1130+ Integer .valueOf (incomingTopicAlias ) });
1131+ if (callback != null ) {
1132+ callback .mqttErrorOccured (new MqttException (MqttException .REASON_CODE_INVALID_TOPIC_ALAS ));
1133+ }
1134+ throw new MqttException (MqttClientException .REASON_CODE_INVALID_TOPIC_ALAS );
1135+
1136+ }
1137+
1138+ // Is this alias being sent with a topic string?
1139+ if (send .getTopicName () != null ) {
11271140 // @TRACE 652=Setting Incoming New Topic Alias alias={0}, topicName={1}
11281141 log .fine (CLASS_NAME , methodName , "652" ,
11291142 new Object [] { Integer .valueOf (send .getProperties ().getTopicAlias ()), send .getTopicName () });
11301143 incomingTopicAliases .put (send .getProperties ().getTopicAlias (), send .getTopicName ());
11311144 } else {
1132- // @TRACE 653=Invalid Topic Alias: topicAliasMax={0}, publishTopicAlias={1}
1133- log .severe (CLASS_NAME , methodName , "653" ,
1134- new Object [] { Integer .valueOf (clientComms .getConOptions ().getTopicAliasMaximum ()),
1135- Integer .valueOf (send .getProperties ().getTopicAlias ()) });
1136- throw new MqttException (MqttClientException .REASON_CODE_INVALID_TOPIC_ALAS );
1137- }
1138- } else if (send .getTopicName () == null && send .getProperties ().getTopicAlias () != 0 ) {
1139- // We've been sent an existing topic alias
1140- if (incomingTopicAliases .get (send .getProperties ().getTopicAlias ()) != null ) {
1141- send .setTopicName (incomingTopicAliases .get (send .getProperties ().getTopicAlias ()));
1142- }
1143- } else if ((send .getTopicName () == null || send .getTopicName ().length () == 0 )
1144- && (send .getProperties ().getTopicAlias () == 0
1145- || send .getProperties ().getTopicAlias () > clientComms .getConOptions ().getTopicAliasMaximum ())) {
1146- // No Topic String provided, topic alias is invalid
1147- // @TRACE 653=Invalid Topic Alias: topicAliasMax={0}, publishTopicAlias={1}
1148- log .fine (CLASS_NAME , methodName , "653" ,
1149- new Object [] { Integer .valueOf (clientComms .getConOptions ().getTopicAliasMaximum ()),
1150- Integer .valueOf (send .getProperties ().getTopicAlias ()) });
1151- if (callback != null ) {
1152- callback .mqttErrorOccured (new MqttException (MqttException .REASON_CODE_INVALID_TOPIC_ALAS ));
1145+ // No Topic String, so must be in incomingTopicAliases.
1146+ if (incomingTopicAliases .contains (incomingTopicAlias )) {
1147+ send .setTopicName (incomingTopicAliases .get (incomingTopicAlias ));
1148+ } else {
1149+ // @TRACE 654=Unknown Topic Alias: Incoming Alias={1}
1150+ log .severe (CLASS_NAME , methodName , "654" ,
1151+ new Object [] { Integer .valueOf (send .getProperties ().getTopicAlias ()) });
1152+ throw new MqttException (MqttClientException .REASON_CODE_UNKNOWN_TOPIC_ALIAS );
1153+ }
11531154 }
11541155 }
1156+
1157+
11551158 switch (send .getMessage ().getQos ()) {
11561159 case 0 :
11571160 case 1 :
0 commit comments