Skip to content

Commit

Permalink
Apply code improvements.
Browse files Browse the repository at this point in the history
Signed-off-by: Carsten Lohmann <[email protected]>
  • Loading branch information
calohmn committed Sep 6, 2022
1 parent e214795 commit 2be112b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#*******************************************************************************
# Copyright (c) 2020 Contributors to the Eclipse Foundation
# Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
Expand Down Expand Up @@ -43,25 +43,25 @@ echo "# Using device registry: ${REGISTRY_IP}"

# register new tenant
if [ -z "$TENANT_TO_USE" ] ; then
TENANT_TO_USE=$(curl --fail -X POST http://$REGISTRY_IP:28080/v1/tenants 2>/dev/null | jq -r .id)
TENANT_TO_USE=$(curl --fail -X POST "http://${REGISTRY_IP}:28080/v1/tenants" 2>/dev/null | jq -r .id)
echo "# Registered new tenant: ${TENANT_TO_USE}"
else
echo "# Using configured tenant: ${TENANT_TO_USE}"
fi

# register new gateway
GATEWAY_TO_CREATE=$(curl --fail -X POST http://$REGISTRY_IP:28080/v1/devices/$TENANT_TO_USE/$GATEWAY_TO_CREATE -d '{"enabled":true}' -H "Content-Type: application/json" 2>/dev/null | jq -r .id)
GATEWAY_TO_CREATE=$(curl --fail -X POST "http://${REGISTRY_IP}:28080/v1/devices/${TENANT_TO_USE}/${GATEWAY_TO_CREATE}" -d '{"enabled":true}' -H "Content-Type: application/json" 2>/dev/null | jq -r .id)

# set credentials for gateway
curl --fail -X PUT -H "content-type: application/json" --data-binary '[{
"type": "hashed-password",
"auth-id": "'$GATEWAY_TO_CREATE'",
"secrets": [{ "pwd-plain": "'$GATEWAY_PASSWORD'" }]
}]' http://$REGISTRY_IP:28080/v1/credentials/$TENANT_TO_USE/$GATEWAY_TO_CREATE
curl --fail -X PUT -H "content-type: application/json" --data-binary "[{
\"type\": \"hashed-password\",
\"auth-id\": \"${GATEWAY_TO_CREATE}\",
\"secrets\": [{ \"pwd-plain\": \"${GATEWAY_PASSWORD}\" }]
}]" "http://${REGISTRY_IP}:28080/v1/credentials/${TENANT_TO_USE}/${GATEWAY_TO_CREATE}"
HONO_CLIENT_AMQP_PASSWORD=$GATEWAY_PASSWORD

# register demo device
HONO_DEMO_DEVICE_DEVICE_ID=$(curl --fail -X POST http://$REGISTRY_IP:28080/v1/devices/$TENANT_TO_USE/$DEVICE_TO_CREATE -d '{"enabled":true,"via":["'$GATEWAY_TO_CREATE'"]}' -H "Content-Type: application/json" 2>/dev/null | jq -r .id)
HONO_DEMO_DEVICE_DEVICE_ID=$(curl --fail -X POST "http://${REGISTRY_IP}:28080/v1/devices/${TENANT_TO_USE}/${DEVICE_TO_CREATE}" -d "{\"enabled\":true,\"via\":[\"${GATEWAY_TO_CREATE}\"]}" -H "Content-Type: application/json" 2>/dev/null | jq -r .id)

echo "# --- DONE ---"
echo "# Please copy the following properties into the configuration of your protocol gateway:"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private DownstreamMessage createDirectMethodResponseMessage(final MqttDownstream
final PropertyBag propertyBag = PropertyBag.decode(ctx.topic());
final RequestId requestId = RequestId.decode(propertyBag.getProperty("$rid"));

final String status = ctx.topic().split("\\/")[3];
final String status = ctx.topic().split("/", -1)[3];

final DownstreamMessage result = new CommandResponseMessage(requestId.getReplyId(),
requestId.getCorrelationId(), status, ctx.message().payload());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Contributors to the Eclipse Foundation
* Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -95,20 +95,19 @@ public static String encode(final String replyTo, final Object correlationId) {
Objects.requireNonNull(replyTo);
Objects.requireNonNull(correlationId);

if (!(correlationId instanceof String)) {
throw new IllegalArgumentException("correlation-id must be a string");
} else {
final String correlationIdString = ((String) correlationId);
if (correlationId instanceof String correlationIdString) {
if (correlationIdString.length() > 255) {
throw new IllegalArgumentException("correlationId is too long");
}

final String[] replyToElements = replyTo.split("\\/");
final String[] replyToElements = replyTo.split("/", -1);
if (replyToElements.length <= 3) {
throw new IllegalArgumentException("reply-to address is malformed");
} else {
return String.format("%02x%s%s", correlationIdString.length(), correlationIdString, replyToElements[3]);
}
} else {
throw new IllegalArgumentException("correlation-id must be a string");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ private Future<Device> tryAuthenticationWithClientCertificate(final MqttEndpoint
if (path != null && path.length > 0) {
final Future<Device> authAttempt = authenticateDeviceCertificate(path);
log.debug("authentication with client certificate: {}.",
(authAttempt.succeeded()) ? "succeeded" : "failed");
authAttempt.succeeded() ? "succeeded" : "failed");
return authAttempt;
}
} catch (RuntimeException | SSLPeerUnverifiedException e) {
Expand All @@ -430,7 +430,7 @@ private Future<Device> authenticateWithUsernameAndPassword(final MqttEndpoint en
return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR));
} else {
log.debug("authentication with username/password {}.",
(authenticatedDevice.succeeded()) ? "succeeded" : "failed");
authenticatedDevice.succeeded() ? "succeeded" : "failed");
return authenticatedDevice;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018, 2020 Contributors to the Eclipse Foundation
* Copyright (c) 2018, 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -21,7 +21,7 @@
* The MQTT subscription of devices, to get commands.
*
*/
public class CommandSubscription {
public final class CommandSubscription {

private final String topicFilter;
private final MqttQoS qos;
Expand Down Expand Up @@ -79,7 +79,7 @@ public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (!getClass().isInstance(o)) {
return false;
}
final CommandSubscription that = (CommandSubscription) o;
Expand Down

0 comments on commit 2be112b

Please sign in to comment.