From b814ba9ca8df2db9298a3ccc9dee2b1427ce1df6 Mon Sep 17 00:00:00 2001 From: Carsten Lohmann Date: Thu, 19 May 2022 06:40:22 +0200 Subject: [PATCH] Apply fixes/improvements regarding code inspection findings. Signed-off-by: Carsten Lohmann --- .../lora/providers/KerlinkProvider.java | 4 ---- .../lora/providers/OrbiwiseProvider.java | 2 -- ...AbstractVertxBasedMqttProtocolAdapter.java | 3 +-- .../hono/client/amqp/GenericSenderLink.java | 20 ++++++++----------- .../client/amqp/RequestResponseClient.java | 10 ++++------ .../amqp/ProtonBasedCommandContext.java | 11 +++++----- .../ProtonBasedInternalCommandSender.java | 10 ++++------ .../KafkaBasedInternalCommandConsumer.java | 3 +-- .../base/HonoExampleApplicationBase.java | 2 +- .../hono/service/metric/MetricsTags.java | 2 +- .../base/jdbc/config/JdbcProperties.java | 2 +- .../AbstractTenantManagementService.java | 2 +- site/documentation/content/api/Metrics.md | 2 +- .../hono/test/ConfigMappingSupport.java | 2 +- 14 files changed, 29 insertions(+), 46 deletions(-) diff --git a/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/KerlinkProvider.java b/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/KerlinkProvider.java index ad42f90ee8..b8674c5e52 100644 --- a/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/KerlinkProvider.java +++ b/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/KerlinkProvider.java @@ -103,15 +103,11 @@ protected Buffer getPayload(final JsonObject loraMessage) { switch (encodingType) { case ENCODING_TYPE_HEX: return payload - .filter(String.class::isInstance) - .map(String.class::cast) .map(s -> Buffer.buffer(BaseEncoding.base16().decode(s.toUpperCase()))) .orElseThrow(() -> new LoraProviderMalformedPayloadException("message does not contain HEX encoded payload property")); default: case ENCODING_TYPE_BASE64: return payload - .filter(String.class::isInstance) - .map(String.class::cast) .map(s -> Buffer.buffer(Base64.getDecoder().decode(s))) .orElseThrow(() -> new LoraProviderMalformedPayloadException("message does not contain BASE64 encoded payload property")); } diff --git a/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/OrbiwiseProvider.java b/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/OrbiwiseProvider.java index 42637076ab..33ee081e93 100644 --- a/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/OrbiwiseProvider.java +++ b/adapters/lora-vertx-quarkus/src/main/java/org/eclipse/hono/adapter/lora/providers/OrbiwiseProvider.java @@ -65,8 +65,6 @@ protected String getDevEui(final JsonObject loraMessage) { Objects.requireNonNull(loraMessage); return LoraUtils.getChildObject(loraMessage, FIELD_ORBIWISE_DEVICE_EUI, String.class) - .filter(String.class::isInstance) - .map(String.class::cast) .map(String::toUpperCase) .orElseThrow(() -> new LoraProviderMalformedPayloadException("message does not contain String valued device ID property")); } diff --git a/adapters/mqtt-vertx-base/src/main/java/org/eclipse/hono/adapter/mqtt/AbstractVertxBasedMqttProtocolAdapter.java b/adapters/mqtt-vertx-base/src/main/java/org/eclipse/hono/adapter/mqtt/AbstractVertxBasedMqttProtocolAdapter.java index c5dbee7e0f..ea2c9869c1 100644 --- a/adapters/mqtt-vertx-base/src/main/java/org/eclipse/hono/adapter/mqtt/AbstractVertxBasedMqttProtocolAdapter.java +++ b/adapters/mqtt-vertx-base/src/main/java/org/eclipse/hono/adapter/mqtt/AbstractVertxBasedMqttProtocolAdapter.java @@ -962,8 +962,7 @@ private Future uploadMessage( }).recover(t -> { - if (ClientErrorException.class.isInstance(t)) { - final ClientErrorException e = (ClientErrorException) t; + if (t instanceof ClientErrorException e) { log.debug("cannot process message [endpoint: {}] from device [tenantId: {}, deviceId: {}]: {} - {}", endpoint, tenantObject.getTenantId(), deviceId, e.getErrorCode(), e.getMessage()); } else { diff --git a/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/GenericSenderLink.java b/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/GenericSenderLink.java index 759628b37d..bf4e07da1a 100644 --- a/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/GenericSenderLink.java +++ b/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/GenericSenderLink.java @@ -541,19 +541,17 @@ protected void handleSendMessageTimeout( private Future mapUnacceptedOutcomeToErrorResult(final ProtonDelivery delivery) { final DeliveryState remoteState = delivery.getRemoteState(); - if (Accepted.class.isInstance(remoteState)) { + if (remoteState instanceof Accepted) { throw new IllegalStateException("delivery is expected to be rejected, released or modified here, not accepted"); } ServiceInvocationException e = null; - if (Rejected.class.isInstance(remoteState)) { - final Rejected rejected = (Rejected) remoteState; + if (remoteState instanceof Rejected rejected) { e = Optional.ofNullable(rejected.getError()) .map(ErrorConverter::fromTransferError) .orElseGet(() -> new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); - } else if (Released.class.isInstance(remoteState)) { + } else if (remoteState instanceof Released) { e = new MessageNotProcessedException(); - } else if (Modified.class.isInstance(remoteState)) { - final Modified modified = (Modified) remoteState; + } else if (remoteState instanceof Modified modified) { if (modified.getUndeliverableHere()) { e = new MessageUndeliverableException(); } else { @@ -593,14 +591,13 @@ protected final void logUpdatedDeliveryState(final Span currentSpan, final Messa final String messageId = message.getMessageId() != null ? message.getMessageId().toString() : ""; final String messageAddress = getMessageAddress(message); final DeliveryState remoteState = delivery.getRemoteState(); - if (Accepted.class.isInstance(remoteState)) { + if (remoteState instanceof Accepted) { log.trace("message [ID: {}, address: {}] accepted by peer", messageId, messageAddress); currentSpan.log("message accepted by peer"); Tags.HTTP_STATUS.set(currentSpan, HttpURLConnection.HTTP_ACCEPTED); } else { final Map events = new HashMap<>(); - if (Rejected.class.isInstance(remoteState)) { - final Rejected rejected = (Rejected) delivery.getRemoteState(); + if (remoteState instanceof Rejected rejected) { Tags.HTTP_STATUS.set(currentSpan, HttpURLConnection.HTTP_BAD_REQUEST); if (rejected.getError() == null) { logMessageSendingError("message [ID: {}, address: {}] rejected by peer", messageId, messageAddress); @@ -611,13 +608,12 @@ protected final void logUpdatedDeliveryState(final Span currentSpan, final Messa events.put(Fields.MESSAGE, String.format("message rejected by peer: %s, %s", rejected.getError().getCondition(), rejected.getError().getDescription())); } - } else if (Released.class.isInstance(remoteState)) { + } else if (remoteState instanceof Released) { logMessageSendingError("message [ID: {}, address: {}] not accepted by peer, remote state: {}", messageId, messageAddress, remoteState.getClass().getSimpleName()); Tags.HTTP_STATUS.set(currentSpan, HttpURLConnection.HTTP_UNAVAILABLE); events.put(Fields.MESSAGE, "message not accepted by peer, remote state: " + remoteState); - } else if (Modified.class.isInstance(remoteState)) { - final Modified modified = (Modified) delivery.getRemoteState(); + } else if (remoteState instanceof Modified modified) { logMessageSendingError("message [ID: {}, address: {}] not accepted by peer, remote state: {}", messageId, messageAddress, modified); Tags.HTTP_STATUS.set(currentSpan, modified.getUndeliverableHere() ? HttpURLConnection.HTTP_NOT_FOUND diff --git a/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/RequestResponseClient.java b/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/RequestResponseClient.java index b218213efb..439214c952 100644 --- a/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/RequestResponseClient.java +++ b/clients/amqp-common/src/main/java/org/eclipse/hono/client/amqp/RequestResponseClient.java @@ -647,8 +647,7 @@ private Future sendRequest( final Promise failedResult = Promise.promise(); final DeliveryState remoteState = deliveryUpdated.getRemoteState(); sample.completed(remoteState); - if (Rejected.class.isInstance(remoteState)) { - final Rejected rejected = (Rejected) remoteState; + if (remoteState instanceof Rejected rejected) { if (rejected.getError() != null) { LOG.debug("service did not accept request [target address: {}, subject: {}, correlation ID: {}]: {}", requestTargetAddress, request.getSubject(), correlationId, rejected.getError()); @@ -660,7 +659,7 @@ private Future sendRequest( failedResult.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST)); cancelRequest(correlationId, failedResult.future()); } - } else if (Accepted.class.isInstance(remoteState)) { + } else if (remoteState instanceof Accepted) { LOG.trace("service has accepted request [target address: {}, subject: {}, correlation ID: {}]", requestTargetAddress, request.getSubject(), correlationId); currentSpan.log("request accepted by peer"); @@ -673,15 +672,14 @@ private Future sendRequest( requestTargetAddress, request.getSubject(), correlationId); } } - } else if (Released.class.isInstance(remoteState)) { + } else if (remoteState instanceof Released) { LOG.debug("service did not accept request [target address: {}, subject: {}, correlation ID: {}], remote state: {}", requestTargetAddress, request.getSubject(), correlationId, remoteState); failedResult.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE)); cancelRequest(correlationId, failedResult.future()); - } else if (Modified.class.isInstance(remoteState)) { + } else if (remoteState instanceof Modified modified) { LOG.debug("service did not accept request [target address: {}, subject: {}, correlation ID: {}], remote state: {}", requestTargetAddress, request.getSubject(), correlationId, remoteState); - final Modified modified = (Modified) deliveryUpdated.getRemoteState(); failedResult.fail(modified.getUndeliverableHere() ? new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND) : new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE)); cancelRequest(correlationId, failedResult.future()); diff --git a/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedCommandContext.java b/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedCommandContext.java index 73da033aa9..535b2ccee0 100644 --- a/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedCommandContext.java +++ b/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedCommandContext.java @@ -144,23 +144,22 @@ private void updateDelivery(final DeliveryState deliveryState) { deliveryState, delivery.getRemoteState()); TracingHelper.logError(getTracingSpan(), msg); LOG.info("{} [{}]", msg, getCommand()); - } else if (Accepted.class.isInstance(deliveryState)) { + } else if (deliveryState instanceof Accepted) { LOG.trace("accepted command message [{}]", getCommand()); span.log("accepted command for device"); - } else if (Released.class.isInstance(deliveryState)) { + } else if (deliveryState instanceof Released) { LOG.debug("released command message [{}]", getCommand()); TracingHelper.logError(span, "released command for device"); - } else if (Modified.class.isInstance(deliveryState)) { - final Modified modified = (Modified) deliveryState; + } else if (deliveryState instanceof Modified modified) { LOG.debug("modified command message [{}]", getCommand()); TracingHelper.logError(span, "modified command for device" + (Boolean.TRUE.equals(modified.getDeliveryFailed()) ? "; delivery failed" : "") + (Boolean.TRUE.equals(modified.getUndeliverableHere()) ? "; undeliverable here" : "")); - } else if (Rejected.class.isInstance(deliveryState)) { - final ErrorCondition errorCondition = ((Rejected) deliveryState).getError(); + } else if (deliveryState instanceof Rejected rejected) { + final ErrorCondition errorCondition = rejected.getError(); LOG.debug("rejected command message [error: {}, command: {}]", errorCondition, getCommand()); TracingHelper.logError(span, "rejected command for device" + ((errorCondition != null && errorCondition.getDescription() != null) diff --git a/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedInternalCommandSender.java b/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedInternalCommandSender.java index 3f4d44708c..ce2ee05d74 100644 --- a/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedInternalCommandSender.java +++ b/clients/command-amqp/src/main/java/org/eclipse/hono/client/command/amqp/ProtonBasedInternalCommandSender.java @@ -86,17 +86,15 @@ public Future sendCommand( final DeliveryState remoteState = delivery.getRemoteState(); LOG.trace("command [{}] sent to downstream peer; remote state of delivery: {}", commandContext.getCommand(), remoteState); - if (Accepted.class.isInstance(remoteState)) { + if (remoteState instanceof Accepted) { commandContext.accept(); - } else if (Rejected.class.isInstance(remoteState)) { - final Rejected rejected = (Rejected) remoteState; + } else if (remoteState instanceof Rejected rejected) { commandContext.reject(Optional.ofNullable(rejected.getError()) .map(ErrorCondition::getDescription) .orElse(null)); - } else if (Released.class.isInstance(remoteState)) { + } else if (remoteState instanceof Released) { commandContext.release(); - } else if (Modified.class.isInstance(remoteState)) { - final Modified modified = (Modified) remoteState; + } else if (remoteState instanceof Modified modified) { commandContext.modify(modified.getDeliveryFailed(), modified.getUndeliverableHere()); } return (Void) null; diff --git a/clients/command-kafka/src/main/java/org/eclipse/hono/client/command/kafka/KafkaBasedInternalCommandConsumer.java b/clients/command-kafka/src/main/java/org/eclipse/hono/client/command/kafka/KafkaBasedInternalCommandConsumer.java index 438661facd..7228f4101b 100644 --- a/clients/command-kafka/src/main/java/org/eclipse/hono/client/command/kafka/KafkaBasedInternalCommandConsumer.java +++ b/clients/command-kafka/src/main/java/org/eclipse/hono/client/command/kafka/KafkaBasedInternalCommandConsumer.java @@ -84,6 +84,7 @@ public class KafkaBasedInternalCommandConsumer implements InternalCommandConsume private final Supplier>> consumerCreator; private final Supplier> kafkaAdminClientCreator; private final String adapterInstanceId; + private final Duration pollTimeout; private final CommandHandlers commandHandlers; private final Tracer tracer; private final CommandResponseSender commandResponseSender; @@ -99,8 +100,6 @@ public class KafkaBasedInternalCommandConsumer implements InternalCommandConsume private Admin adminClient; private Context context; private KafkaClientMetricsSupport metricsSupport; - - private final Duration pollTimeout; private long retryCreateTopicTimerId; /** diff --git a/examples/hono-client-examples/src/main/java/org/eclipse/hono/vertx/example/base/HonoExampleApplicationBase.java b/examples/hono-client-examples/src/main/java/org/eclipse/hono/vertx/example/base/HonoExampleApplicationBase.java index b96f4b5d9a..dc546bd759 100644 --- a/examples/hono-client-examples/src/main/java/org/eclipse/hono/vertx/example/base/HonoExampleApplicationBase.java +++ b/examples/hono-client-examples/src/main/java/org/eclipse/hono/vertx/example/base/HonoExampleApplicationBase.java @@ -199,7 +199,7 @@ protected void consumeData() { Optional.ofNullable(telemetryConsumer) .map(MessageConsumer::close) .ifPresent(closeFutures::add); - Optional.ofNullable(client) + Optional.of(client) .map(Lifecycle::stop) .ifPresent(closeFutures::add); diff --git a/service-base/src/main/java/org/eclipse/hono/service/metric/MetricsTags.java b/service-base/src/main/java/org/eclipse/hono/service/metric/MetricsTags.java index d49dfb5d35..5997c962e8 100644 --- a/service-base/src/main/java/org/eclipse/hono/service/metric/MetricsTags.java +++ b/service-base/src/main/java/org/eclipse/hono/service/metric/MetricsTags.java @@ -287,7 +287,7 @@ public enum QoS { static final String TAG_NAME = "qos"; - private Tag tag; + private final Tag tag; QoS() { this.tag = null; diff --git a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java index 7c79782d39..4f0b067fa5 100644 --- a/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java +++ b/services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/config/JdbcProperties.java @@ -58,7 +58,7 @@ public JdbcProperties(final JdbcOptions options) { setDriverClass(options.driverClass()); options.maximumPoolSize().ifPresent(this::setMaximumPoolSize); options.password().ifPresent(this::setPassword); - options.tableName().ifPresent(this::setTableName);; + options.tableName().ifPresent(this::setTableName); setUrl(options.url()); options.username().ifPresent(this::setUsername); } diff --git a/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/AbstractTenantManagementService.java b/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/AbstractTenantManagementService.java index 7a47fd37a6..1cd377a75b 100644 --- a/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/AbstractTenantManagementService.java +++ b/services/device-registry-base/src/main/java/org/eclipse/hono/deviceregistry/service/tenant/AbstractTenantManagementService.java @@ -225,7 +225,7 @@ public final Future> createTenant( .compose(ok -> processCreateTenant(tenantIdValue, tenantObj, span)) .onSuccess(result -> notificationSender.handle(new TenantChangeNotification(LifecycleChange.CREATE, tenantIdValue, Instant.now(), tenantObj.isEnabled()))) - .recover(t -> DeviceRegistryUtils.mapError(t, tenantId.get())); + .recover(t -> DeviceRegistryUtils.mapError(t, tenantIdValue)); } @Override diff --git a/site/documentation/content/api/Metrics.md b/site/documentation/content/api/Metrics.md index 2b17a18385..7415511e3d 100644 --- a/site/documentation/content/api/Metrics.md +++ b/site/documentation/content/api/Metrics.md @@ -62,7 +62,7 @@ Additional tags used for metrics reported by protocol adapters are: | *direction* | `one-way`, `request`, `response` | The direction in which a Command & Control message is being sent:
`one-way` indicates a command sent to a device for which the sending application doesn't expect to receive a response.
`request` indicates a command request message sent to a device.
`response` indicates a command response received from a device. | | *qos* | `0`, `1`, `unknown` | The quality of service used for a telemetry or event message.
`0` indicates *at most once*,
`1` indicates *at least once* and
`none` indicates unknown delivery semantics. | | *status* | `forwarded`, `unprocessable`, `undeliverable` | The processing status of a message.
`forwarded` indicates that the message has been forwarded to a downstream consumer
`unprocessable` indicates that the message has not been processed not forwarded, e.g. because the message was malformed
`undeliverable` indicates that the message could not be forwarded, e.g. because there is no downstream consumer or due to an infrastructure problem | -| *tenant* | *string* | The identifier of the tenant that the metric is being reported for | +| *tenant* | *string* | The identifier of the tenant that the metric is being reported for. | | *ttd* | `command`, `expired`, `none` | A status indicating the outcome of processing a TTD value contained in a message received from a device.
`command` indicates that a command for the device has been included in the response to the device's request for uploading the message.
`expired` indicates that a response without a command has been sent to the device.
`none` indicates that either no TTD value has been specified by the device or that the protocol adapter does not support it. | | *type* | `telemetry`, `event` | The type of (downstream) message that the metric is being reported for. | diff --git a/test-utils/core-test-utils/src/main/java/org/eclipse/hono/test/ConfigMappingSupport.java b/test-utils/core-test-utils/src/main/java/org/eclipse/hono/test/ConfigMappingSupport.java index ff631a7e4c..0a3ee4e4ac 100644 --- a/test-utils/core-test-utils/src/main/java/org/eclipse/hono/test/ConfigMappingSupport.java +++ b/test-utils/core-test-utils/src/main/java/org/eclipse/hono/test/ConfigMappingSupport.java @@ -27,7 +27,7 @@ public final class ConfigMappingSupport { private ConfigMappingSupport() { - // prevent instantation + // prevent instantiation } /**