Skip to content

Commit 01506fa

Browse files
committedFeb 4, 2022
Fix NPE concerning connection events of unauthenticated devices.
Signed-off-by: Carsten Lohmann <carsten.lohmann@bosch.io>
1 parent 36c04a3 commit 01506fa

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed
 

‎adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterBase.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2021 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2016, 2022 Contributors to the Eclipse Foundation
33
*
44
* See the NOTICE file(s) distributed with this work for additional
55
* information regarding copyright ownership.
@@ -936,8 +936,10 @@ public void registerLivenessChecks(final HealthCheckHandler handler) {
936936
*/
937937
protected Future<Void> sendConnectedEvent(final String remoteId, final Device authenticatedDevice, final SpanContext context) {
938938
if (this.connectionEventProducer != null) {
939-
return getTenantClient().get(authenticatedDevice.getTenantId(), context)
940-
.map(this::getEventSender)
939+
return Optional.ofNullable(authenticatedDevice)
940+
.map(device -> getTenantClient().get(device.getTenantId(), context)
941+
.map(this::getEventSender))
942+
.orElseGet(() -> Future.succeededFuture(null))
941943
.compose(es -> this.connectionEventProducer.connected(
942944
new ConnectionEventProducer.Context() {
943945
@Override
@@ -972,8 +974,10 @@ public TenantClient getTenantClient() {
972974
*/
973975
protected Future<Void> sendDisconnectedEvent(final String remoteId, final Device authenticatedDevice, final SpanContext context) {
974976
if (this.connectionEventProducer != null) {
975-
return getTenantClient().get(authenticatedDevice.getTenantId(), context)
976-
.map(this::getEventSender)
977+
return Optional.ofNullable(authenticatedDevice)
978+
.map(device -> getTenantClient().get(device.getTenantId(), context)
979+
.map(this::getEventSender))
980+
.orElseGet(() -> Future.succeededFuture(null))
977981
.compose(es -> this.connectionEventProducer.disconnected(
978982
new ConnectionEventProducer.Context() {
979983
@Override

‎adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/AbstractMessageSenderConnectionEventProducer.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2021 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2016, 2022 Contributors to the Eclipse Foundation
33
*
44
* See the NOTICE file(s) distributed with this work for additional
55
* information regarding copyright ownership.
@@ -12,6 +12,8 @@
1212
*******************************************************************************/
1313
package org.eclipse.hono.adapter.monitoring;
1414

15+
import java.util.Optional;
16+
1517
import org.eclipse.hono.auth.Device;
1618
import org.eclipse.hono.util.EventConstants;
1719
import org.eclipse.hono.util.RegistrationAssertion;
@@ -84,13 +86,15 @@ private Future<Void> sendNotificationEvent(
8486
payload.put("data", data);
8587
}
8688

87-
return context.getMessageSenderClient().sendEvent(
88-
tenant,
89-
new RegistrationAssertion(deviceId),
90-
EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE,
91-
payload.toBuffer(),
92-
null,
93-
spanContext);
89+
return Optional.ofNullable(context.getMessageSenderClient())
90+
.map(client -> client.sendEvent(
91+
tenant,
92+
new RegistrationAssertion(deviceId),
93+
EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE,
94+
payload.toBuffer(),
95+
null,
96+
spanContext))
97+
.orElseGet(Future::succeededFuture);
9498
});
9599
}
96100
}

‎adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/ConnectionEventProducer.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016, 2021 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2016, 2022 Contributors to the Eclipse Foundation
33
*
44
* See the NOTICE file(s) distributed with this work for additional
55
* information regarding copyright ownership.
@@ -49,9 +49,13 @@ interface Context {
4949

5050
/**
5151
* Gets the client for sending connection events downstream.
52+
* <p>
53+
* If no client is available in this context, {@code null} is returned.
54+
* <p>
55+
* A returned client here is required to be initialized and started.
5256
*
5357
* @return The instance of the message sender client which the {@link ConnectionEventProducer} should
54-
* use. This client has to be initialized and started.
58+
* use or {@code null} if no client is available.
5559
*/
5660
EventSender getMessageSenderClient();
5761
/**

‎site/homepage/content/release-notes.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ description = "Information about changes in recent Hono releases. Includes new f
1212
password hashes were created in lowercase instead of uppercase. This has been fixed.
1313
* The native executable of the Command Router component did not start when configured with an embedded cache.
1414
This has been fixed.
15+
* There was an issue trying to send connection events concerning unauthenticated MQTT/AMQP devices. This has been fixed.
1516

1617
## 1.11.1
1718

0 commit comments

Comments
 (0)
Please sign in to comment.