diff --git a/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterBase.java b/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterBase.java index e9d46aba19..aebc3869ad 100644 --- a/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterBase.java +++ b/adapter-base/src/main/java/org/eclipse/hono/adapter/AbstractProtocolAdapterBase.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016, 2021 Contributors to the Eclipse Foundation + * Copyright (c) 2016, 2022 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -936,8 +936,10 @@ public void registerLivenessChecks(final HealthCheckHandler handler) { */ protected Future sendConnectedEvent(final String remoteId, final Device authenticatedDevice, final SpanContext context) { if (this.connectionEventProducer != null) { - return getTenantClient().get(authenticatedDevice.getTenantId(), context) - .map(this::getEventSender) + return Optional.ofNullable(authenticatedDevice) + .map(device -> getTenantClient().get(device.getTenantId(), context) + .map(this::getEventSender)) + .orElseGet(() -> Future.succeededFuture(null)) .compose(es -> this.connectionEventProducer.connected( new ConnectionEventProducer.Context() { @Override @@ -972,8 +974,10 @@ public TenantClient getTenantClient() { */ protected Future sendDisconnectedEvent(final String remoteId, final Device authenticatedDevice, final SpanContext context) { if (this.connectionEventProducer != null) { - return getTenantClient().get(authenticatedDevice.getTenantId(), context) - .map(this::getEventSender) + return Optional.ofNullable(authenticatedDevice) + .map(device -> getTenantClient().get(device.getTenantId(), context) + .map(this::getEventSender)) + .orElseGet(() -> Future.succeededFuture(null)) .compose(es -> this.connectionEventProducer.disconnected( new ConnectionEventProducer.Context() { @Override diff --git a/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/AbstractMessageSenderConnectionEventProducer.java b/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/AbstractMessageSenderConnectionEventProducer.java index e89331eb0b..0076c2952a 100644 --- a/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/AbstractMessageSenderConnectionEventProducer.java +++ b/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/AbstractMessageSenderConnectionEventProducer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016, 2021 Contributors to the Eclipse Foundation + * Copyright (c) 2016, 2022 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -12,6 +12,8 @@ *******************************************************************************/ package org.eclipse.hono.adapter.monitoring; +import java.util.Optional; + import org.eclipse.hono.auth.Device; import org.eclipse.hono.util.EventConstants; import org.eclipse.hono.util.RegistrationAssertion; @@ -84,13 +86,15 @@ private Future sendNotificationEvent( payload.put("data", data); } - return context.getMessageSenderClient().sendEvent( - tenant, - new RegistrationAssertion(deviceId), - EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE, - payload.toBuffer(), - null, - spanContext); + return Optional.ofNullable(context.getMessageSenderClient()) + .map(client -> client.sendEvent( + tenant, + new RegistrationAssertion(deviceId), + EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE, + payload.toBuffer(), + null, + spanContext)) + .orElseGet(Future::succeededFuture); }); } } diff --git a/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/ConnectionEventProducer.java b/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/ConnectionEventProducer.java index a64e46152e..57dcde1033 100644 --- a/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/ConnectionEventProducer.java +++ b/adapter-base/src/main/java/org/eclipse/hono/adapter/monitoring/ConnectionEventProducer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2016, 2021 Contributors to the Eclipse Foundation + * Copyright (c) 2016, 2022 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional * information regarding copyright ownership. @@ -49,9 +49,13 @@ interface Context { /** * Gets the client for sending connection events downstream. + *

+ * If no client is available in this context, {@code null} is returned. + *

+ * A returned client here is required to be initialized and started. * * @return The instance of the message sender client which the {@link ConnectionEventProducer} should - * use. This client has to be initialized and started. + * use or {@code null} if no client is available. */ EventSender getMessageSenderClient(); /** diff --git a/site/homepage/content/release-notes.md b/site/homepage/content/release-notes.md index 15d78bad07..11aee0afc6 100644 --- a/site/homepage/content/release-notes.md +++ b/site/homepage/content/release-notes.md @@ -12,6 +12,7 @@ description = "Information about changes in recent Hono releases. Includes new f password hashes were created in lowercase instead of uppercase. This has been fixed. * The native executable of the Command Router component did not start when configured with an embedded cache. This has been fixed. +* There was an issue trying to send connection events concerning unauthenticated MQTT/AMQP devices. This has been fixed. ## 1.11.1