-
Notifications
You must be signed in to change notification settings - Fork 215
/
GenericMqttPublish.java
614 lines (519 loc) · 20.8 KB
/
GenericMqttPublish.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.mqtt.hivemq.publish;
import java.nio.ByteBuffer;
import java.text.MessageFormat;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.base.model.common.CharsetDeterminer;
import org.eclipse.ditto.base.model.common.ConditionChecker;
import com.hivemq.client.mqtt.MqttVersion;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.datatypes.MqttTopic;
import com.hivemq.client.mqtt.datatypes.MqttUtf8String;
import com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish;
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperties;
import com.hivemq.client.mqtt.mqtt5.datatypes.Mqtt5UserProperty;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5PayloadFormatIndicator;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import akka.http.javadsl.model.ContentType;
import akka.http.javadsl.model.ContentTypes;
/**
* Unifies access to MQTT Publish messages of protocol versions 3 and 5.
*/
@SuppressWarnings("java:S1610")
@NotThreadSafe
/*
* Abstract class is appropriate in this case to completely hide existing
* implementations and avoid new implementations.
* This would neither be possible by sealing nor by converting to
* an interface.
*/
public abstract class GenericMqttPublish {
private GenericMqttPublish() {
super();
}
/**
* Returns a new instance of {@code GenericMqttPublish} that wraps the specified {@code Mqtt3Publish} argument.
*
* @param mqtt3Publish the {@code PUBLISH} message to be wrapped.
* @return the new instance.
* @throws NullPointerException if {@code mqtt3Publish} is {@code null}.
*/
public static GenericMqttPublish ofMqtt3Publish(final Mqtt3Publish mqtt3Publish) {
return new GenericMqtt3Publish(mqtt3Publish);
}
/**
* Returns a new instance of {@code GenericMqttPublish} that wraps the specified {@code Mqtt5Publish} argument.
*
* @param mqtt5Publish the {@code PUBLISH} message to be wrapped.
* @return the new instance.
* @throws NullPointerException if {@code mqtt5Publish} is {@code null}.
*/
public static GenericMqttPublish ofMqtt5Publish(final Mqtt5Publish mqtt5Publish) {
return new GenericMqtt5Publish(mqtt5Publish);
}
/**
* Returns a mutable builder with fluent API for building a {@code GenericMqttPublish} from scratch.
* <em>Note:</em> the built Publish cannot be acknowledged, i.e. calling the method throws an exception.
*
* @param mqttTopic the mandatory topic.
* @param mqttQos the mandatory QoS.
* @return the builder.
* @throws NullPointerException if any argument is {@code null}.
*/
public static Builder builder(final MqttTopic mqttTopic, final MqttQos mqttQos) {
return new Builder(mqttTopic, mqttQos);
}
/**
* Returns the topic of this Publish message.
*
* @return the topic.
*/
public abstract MqttTopic getTopic();
public abstract MqttQos getQos();
/**
* Indicates whether this Publish message is a retained message.
*
* @return {@code true} if this is a retained message, {@code false} else.
*/
public abstract boolean isRetain();
public abstract Optional<ByteBuffer> getCorrelationData();
public abstract Optional<MqttTopic> getResponseTopic();
public abstract Optional<String> getContentType();
public abstract Stream<UserProperty> userProperties();
public abstract Optional<ByteBuffer> getPayload();
public Optional<String> getPayloadAsHumanReadable() {
return getPayload()
.map(payloadByteBuffer -> {
final var charsetDeterminer = CharsetDeterminer.getInstance();
final var charset = charsetDeterminer.apply(getContentType().orElse(null));
final var charBuffer = charset.decode(payloadByteBuffer);
return charBuffer.toString();
});
}
public abstract void acknowledge() throws ManualAcknowledgementDisabledException,
MessageAlreadyAcknowledgedException, AcknowledgementUnsupportedException;
/**
* Indicates whether the version of this {@code GenericMqtt5Publish} is the same as the specified
* {@code MqttVersion} argument.
*
* @param mqttVersion the expected MQTT version.
* @return {@code true} if this {@code GenericMqtt5Publish} has version {@code mqttVersion}, {@code false} else.
*/
public abstract boolean isMqttVersion(MqttVersion mqttVersion);
/**
* Returns this {@code GenericMqttPublish} as {@code Mqtt3Publish}.
* <em>Note:</em> {@link Mqtt3Publish#acknowledge()} only works if this {@code GenericMqttPublish} was constructed
* from a {@code Mqtt3Publish}
*
* @return the {@code Mqtt3Publish} representation of this {@code GenericMqttPublish}.
*/
public abstract Mqtt3Publish getAsMqtt3Publish();
private static Mqtt3Publish getAsMqtt3Publish(final GenericMqttPublish genericMqttPublish) {
return Mqtt3Publish.builder()
.topic(genericMqttPublish.getTopic())
.qos(genericMqttPublish.getQos())
.retain(genericMqttPublish.isRetain())
.payload(genericMqttPublish.getPayload().orElse(null))
.build();
}
/**
* Returns this {@code GenericMqttPublish} as {@code Mqtt5Publish}.
* <em>Note:</em> {@link Mqtt5Publish#acknowledge()} only works if this {@code GenericMqttPublish} was constructed
* from a {@code Mqtt5Publish}
*
* @return the {@code Mqtt5Publish} representation of this {@code GenericMqttPublish}.
*/
public abstract Mqtt5Publish getAsMqtt5Publish();
private static Mqtt5Publish getAsMqtt5Publish(final GenericMqttPublish genericMqttPublish) {
return Mqtt5Publish.builder()
.topic(genericMqttPublish.getTopic())
.qos(genericMqttPublish.getQos())
.retain(genericMqttPublish.isRetain())
.payload(genericMqttPublish.getPayload().orElse(null))
.correlationData(genericMqttPublish.getCorrelationData().orElse(null))
.responseTopic(genericMqttPublish.getResponseTopic().orElse(null))
.contentType(genericMqttPublish.getContentType().orElse(null))
.userProperties(Mqtt5UserProperties.of(genericMqttPublish.userProperties()
.map(userProperty -> Mqtt5UserProperty.of(userProperty.name(), userProperty.value()))
.collect(Collectors.toList())))
.build();
}
@Override
public abstract String toString();
/**
* Mutable builder with a fluent API for constructing a {@code GenericMqttPublish} from scratch.
* <em>Note:</em> The built Publish cannot be acknowledged.
*/
@NotThreadSafe
public static final class Builder {
private final MqttTopic mqttTopic;
private final MqttQos mqttQos;
private boolean retain;
@Nullable private ByteBuffer payload;
@Nullable private ByteBuffer correlationData;
@Nullable private MqttTopic responseTopic;
@Nullable private String contentType;
private Set<UserProperty> userProperties;
private Builder(final MqttTopic mqttTopic, final MqttQos mqttQos) {
this.mqttTopic = ConditionChecker.checkNotNull(mqttTopic, "mqttTopic");
this.mqttQos = ConditionChecker.checkNotNull(mqttQos, "mqttQos");
retain = false;
payload = null;
correlationData = null;
responseTopic = null;
contentType = null;
userProperties = Set.of();
}
/**
* Sets whether the Publish should be retained.
*
* @param retain {@code true} if the Publish should be retained, {@code false} else.
* @return this Builder instance for method chaining.
*/
public Builder retain(final boolean retain) {
this.retain = retain;
return this;
}
/**
* Sets the optional payload.
*
* @param payload the payload of the Publish or {@code null} to remove any previously set payload.
* @return this Builder instance for method chaining.
*/
public Builder payload(@Nullable final ByteBuffer payload) {
if (null != payload) {
this.payload = payload.asReadOnlyBuffer();
} else {
this.payload = null;
}
return this;
}
public Builder correlationData(@Nullable final ByteBuffer correlationData) {
this.correlationData = correlationData;
return this;
}
public Builder responseTopic(@Nullable final MqttTopic responseTopic) {
this.responseTopic = responseTopic;
return this;
}
public Builder contentType(@Nullable final String contentType) {
if (null != contentType) {
ConditionChecker.checkArgument(contentType,
arg -> !arg.isBlank(),
() -> "The contentType must not be blank.");
}
this.contentType = contentType;
return this;
}
public Builder userProperties(final Set<UserProperty> userProperties) {
this.userProperties = ConditionChecker.checkNotNull(userProperties, "userProperties");
return this;
}
public GenericMqttPublish build() {
return new FromScratchGenericMqttPublish(this);
}
}
private static final class FromScratchGenericMqttPublish extends GenericMqttPublish {
private final MqttTopic mqttTopic;
private final MqttQos mqttQos;
private final boolean retain;
@Nullable private final ByteBuffer payload;
@Nullable private final ByteBuffer correlationData;
@Nullable private final MqttTopic responseTopic;
@Nullable private final String contentType;
private final Set<UserProperty> userProperties;
private FromScratchGenericMqttPublish(final Builder builder) {
mqttTopic = builder.mqttTopic;
mqttQos = builder.mqttQos;
retain = builder.retain;
payload = builder.payload;
correlationData = builder.correlationData;
responseTopic = builder.responseTopic;
contentType = builder.contentType;
userProperties = new LinkedHashSet<>(builder.userProperties);
}
@Override
public MqttTopic getTopic() {
return mqttTopic;
}
@Override
public MqttQos getQos() {
return mqttQos;
}
@Override
public boolean isRetain() {
return retain;
}
@Override
public Optional<ByteBuffer> getCorrelationData() {
return Optional.ofNullable(correlationData);
}
@Override
public Optional<MqttTopic> getResponseTopic() {
return Optional.ofNullable(responseTopic);
}
@Override
public Optional<String> getContentType() {
return Optional.ofNullable(contentType);
}
@Override
public Stream<UserProperty> userProperties() {
return userProperties.stream();
}
@Override
public Optional<ByteBuffer> getPayload() {
return Optional.ofNullable(payload);
}
@Override
public void acknowledge() throws AcknowledgementUnsupportedException {
throw new AcknowledgementUnsupportedException(
MessageFormat.format("A {0} that was built from scratch cannot be acknowledged.",
GenericMqttPublish.class.getSimpleName())
);
}
@Override
public boolean isMqttVersion(final MqttVersion mqttVersion) {
return false;
}
@Override
public Mqtt3Publish getAsMqtt3Publish() {
return GenericMqttPublish.getAsMqtt3Publish(this);
}
@Override
public Mqtt5Publish getAsMqtt5Publish() {
return GenericMqttPublish.getAsMqtt5Publish(this);
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (FromScratchGenericMqttPublish) o;
return Objects.equals(mqttTopic, that.mqttTopic) &&
mqttQos == that.mqttQos &&
retain == that.retain &&
Objects.equals(payload, that.payload) &&
Objects.equals(correlationData, that.correlationData) &&
Objects.equals(responseTopic, that.responseTopic) &&
Objects.equals(contentType, that.contentType) &&
Objects.equals(userProperties, that.userProperties);
}
@Override
public int hashCode() {
return Objects.hash(mqttTopic,
mqttQos,
retain,
payload,
correlationData,
responseTopic,
contentType,
userProperties);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"mqttTopic=" + mqttTopic +
", mqttQos=" + mqttQos +
", retain=" + retain +
", payload=" + getPayloadAsHumanReadable().orElse(null) +
", correlationData=" + correlationData +
", responseTopic=" + responseTopic +
", contentType=" + contentType +
", userProperties=" + userProperties +
"]";
}
}
private static final class GenericMqtt3Publish extends GenericMqttPublish {
private final Mqtt3Publish mqtt3Publish;
private GenericMqtt3Publish(final Mqtt3Publish mqtt3Publish) {
this.mqtt3Publish = ConditionChecker.checkNotNull(mqtt3Publish, "mqtt3Publish");
}
@Override
public MqttTopic getTopic() {
return mqtt3Publish.getTopic();
}
@Override
public MqttQos getQos() {
return mqtt3Publish.getQos();
}
@Override
public boolean isRetain() {
return mqtt3Publish.isRetain();
}
@Override
public Optional<ByteBuffer> getCorrelationData() {
return Optional.empty();
}
@Override
public Optional<MqttTopic> getResponseTopic() {
return Optional.empty();
}
@Override
public Optional<String> getContentType() {
return Optional.empty();
}
@Override
public Stream<UserProperty> userProperties() {
return Stream.empty();
}
@Override
public Optional<ByteBuffer> getPayload() {
return mqtt3Publish.getPayload();
}
@Override
public void acknowledge() throws ManualAcknowledgementDisabledException, MessageAlreadyAcknowledgedException {
try {
mqtt3Publish.acknowledge();
} catch (final UnsupportedOperationException e) {
throw new ManualAcknowledgementDisabledException(e);
} catch (final IllegalStateException e) {
throw new MessageAlreadyAcknowledgedException(e);
}
}
@Override
public boolean isMqttVersion(final MqttVersion mqttVersion) {
return MqttVersion.MQTT_3_1_1 == mqttVersion;
}
@Override
public Mqtt3Publish getAsMqtt3Publish() {
return mqtt3Publish;
}
@Override
public Mqtt5Publish getAsMqtt5Publish() {
return GenericMqttPublish.getAsMqtt5Publish(this);
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (GenericMqtt3Publish) o;
return Objects.equals(mqtt3Publish, that.mqtt3Publish);
}
@Override
public int hashCode() {
return Objects.hash(mqtt3Publish);
}
@Override
public String toString() {
return mqtt3Publish.toString();
}
}
private static final class GenericMqtt5Publish extends GenericMqttPublish {
private final Mqtt5Publish mqtt5Publish;
private GenericMqtt5Publish(final Mqtt5Publish mqtt5Publish) {
this.mqtt5Publish = ConditionChecker.checkNotNull(mqtt5Publish, "mqtt5Publish");
}
@Override
public MqttTopic getTopic() {
return mqtt5Publish.getTopic();
}
@Override
public MqttQos getQos() {
return mqtt5Publish.getQos();
}
@Override
public boolean isRetain() {
return mqtt5Publish.isRetain();
}
@Override
public Optional<ByteBuffer> getCorrelationData() {
return mqtt5Publish.getCorrelationData();
}
@Override
public Optional<MqttTopic> getResponseTopic() {
return mqtt5Publish.getResponseTopic();
}
@Override
public Optional<String> getContentType() {
return mqtt5Publish.getContentType()
.map(MqttUtf8String::toString)
.or(() -> mqtt5Publish.getPayloadFormatIndicator()
.map(payloadFormatIndicator -> {
final ContentType contentType;
if (Mqtt5PayloadFormatIndicator.UTF_8 == payloadFormatIndicator) {
contentType = ContentTypes.TEXT_PLAIN_UTF8;
} else {
contentType = ContentTypes.APPLICATION_OCTET_STREAM;
}
return contentType;
})
.map(ContentType::toString));
}
@Override
public Stream<UserProperty> userProperties() {
final var mqtt5PublishUserProperties = mqtt5Publish.getUserProperties();
final var mqtt5UserPropertiesAsList = mqtt5PublishUserProperties.asList();
return mqtt5UserPropertiesAsList.stream().map(UserProperty::of);
}
@Override
public Optional<ByteBuffer> getPayload() {
return mqtt5Publish.getPayload();
}
@Override
public void acknowledge() throws ManualAcknowledgementDisabledException, MessageAlreadyAcknowledgedException {
try {
mqtt5Publish.acknowledge();
} catch (final UnsupportedOperationException e) {
throw new ManualAcknowledgementDisabledException(e);
} catch (final IllegalStateException e) {
throw new MessageAlreadyAcknowledgedException(e);
}
}
@Override
public boolean isMqttVersion(final MqttVersion mqttVersion) {
return MqttVersion.MQTT_5_0 == mqttVersion;
}
@Override
public Mqtt3Publish getAsMqtt3Publish() {
return GenericMqttPublish.getAsMqtt3Publish(this);
}
@Override
public Mqtt5Publish getAsMqtt5Publish() {
return mqtt5Publish;
}
@Override
public boolean equals(@Nullable final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (GenericMqtt5Publish) o;
return Objects.equals(mqtt5Publish, that.mqtt5Publish);
}
@Override
public int hashCode() {
return Objects.hash(mqtt5Publish);
}
@Override
public String toString() {
return mqtt5Publish.toString();
}
}
}