use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class ErrorSubscription method fromTopic.
/**
* Creates a error subscription object for the given topic.
* <p>
* If the authenticated device is given, it is used to either validate the tenant and device-id
* given via the topic or, if the topic doesn't contain these values, the authenticated device
* is used to provide tenant and device-id for the created error subscription object.
*
* @param topic The topic to subscribe for errors.
* @param qos The quality-of-service level for the subscription.
* @param authenticatedDevice The authenticated device or {@code null}.
* @return The ErrorSubscription object or {@code null} if the topic does not match the rules.
* @throws NullPointerException if topic or qos is {@code null}.
*/
public static ErrorSubscription fromTopic(final String topic, final MqttQoS qos, final Device authenticatedDevice) {
Objects.requireNonNull(topic);
Objects.requireNonNull(qos);
try {
final ResourceIdentifier topicResource = validateTopic(topic);
return new ErrorSubscription(topicResource, authenticatedDevice, qos);
} catch (final IllegalArgumentException e) {
LOG.debug(e.getMessage());
return null;
}
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMapMessageSucceedsIfNoMapperIsSet.
/**
* Verifies that the result returned by the mapping service contains the
* original payload and target address if no downstream mapper has been defined for
* the gateway.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapMessageSucceedsIfNoMapperIsSet(final VertxTestContext ctx) {
config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
final ResourceIdentifier targetAddress = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, "gateway");
final MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final MqttContext context = newContext(message, span, new Device(TEST_TENANT_ID, "gateway"));
messageMapping.mapDownstreamMessage(context, targetAddress, new RegistrationAssertion("gateway")).onComplete(ctx.succeeding(mappedMessage -> {
ctx.verify(() -> {
assertThat(mappedMessage.getTargetAddress()).isEqualTo(targetAddress);
assertThat(mappedMessage.getPayload()).isEqualTo(message.payload());
assertThat(mappedMessage.getAdditionalProperties()).isEmpty();
verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMapMessageSucceedsIfNoMapperEndpointIsConfigured.
/**
* Verifies that the result returned by the mapping service contains the
* original payload and target address if no downstream mapper endpoint has been configured
* for the adapter.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapMessageSucceedsIfNoMapperEndpointIsConfigured(final VertxTestContext ctx) {
final ResourceIdentifier targetAddress = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, "gateway");
final MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final MqttContext context = newContext(message, span, new Device(TEST_TENANT_ID, "gateway"));
final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setDownstreamMessageMapper("mapper");
messageMapping.mapDownstreamMessage(context, targetAddress, assertion).onComplete(ctx.succeeding(mappedMessage -> {
ctx.verify(() -> {
assertThat(mappedMessage.getTargetAddress()).isEqualTo(targetAddress);
assertThat(mappedMessage.getPayload()).isEqualTo(message.payload());
assertThat(mappedMessage.getAdditionalProperties()).isEmpty();
verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class CommandSubscription method validateTopic.
private static ResourceIdentifier validateTopic(final String topic) {
Objects.requireNonNull(topic);
if (!ResourceIdentifier.isValid(topic)) {
throw new IllegalArgumentException("topic filter or its first segment must not be empty");
}
final ResourceIdentifier resource = ResourceIdentifier.fromString(topic);
if (resource.length() != 5 || !"#".equals(resource.elementAt(4))) {
throw new IllegalArgumentException(ERROR_MSG_FILTER_FORMAT);
}
if (!CommandConstants.isCommandEndpoint(resource.getEndpoint())) {
throw new IllegalArgumentException(ERROR_MSG_ENDPOINT_NAME);
}
if (!CommandConstants.COMMAND_RESPONSE_REQUEST_PART.equals(resource.elementAt(3)) && !CommandConstants.COMMAND_RESPONSE_REQUEST_PART_SHORT.equals(resource.elementAt(3))) {
throw new IllegalArgumentException(ERROR_MSG_REQUEST_SEGMENT_NAME);
}
return resource;
}
use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.
the class AmqpAdapterClientCommandConsumer method create.
/**
* Creates a new command consumer.
* <p>
* The underlying receiver link will be created with its <em>autoAccept</em> property set to {@code true} and with
* the connection's default pre-fetch size.
*
* @param con The connection to the server.
* @param messageHandler The handler to invoke with every message received.
* @return A future indicating the outcome of the creation attempt.
* @throws NullPointerException if any of the parameters are {@code null}.
*/
public static Future<CommandConsumer> create(final HonoConnection con, final BiConsumer<ProtonDelivery, Message> messageHandler) {
Objects.requireNonNull(con);
Objects.requireNonNull(messageHandler);
final ResourceIdentifier address = ResourceIdentifier.from(CommandConstants.NORTHBOUND_COMMAND_REQUEST_ENDPOINT, null, null);
return createCommandConsumer(con, messageHandler, address);
}
Aggregations