Search in sources :

Example 76 with ResourceIdentifier

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;
    }
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Example 77 with ResourceIdentifier

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();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) HttpResponse(io.vertx.ext.web.client.HttpResponse) WebClient(io.vertx.ext.web.client.WebClient) Command(org.eclipse.hono.client.command.Command) MultiMap(io.vertx.core.MultiMap) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Constants(org.eclipse.hono.util.Constants) RETURNS_SELF(org.mockito.Mockito.RETURNS_SELF) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) MapperEndpoint(org.eclipse.hono.config.MapperEndpoint) ServerErrorException(org.eclipse.hono.client.ServerErrorException) HttpHeaders(io.vertx.core.http.HttpHeaders) Mockito.times(org.mockito.Mockito.times) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Device(org.eclipse.hono.auth.Device) Mockito.verify(org.mockito.Mockito.verify) HttpRequest(io.vertx.ext.web.client.HttpRequest) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) Mockito.withSettings(org.mockito.Mockito.withSettings) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Device(org.eclipse.hono.auth.Device) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Test(org.junit.jupiter.api.Test)

Example 78 with ResourceIdentifier

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();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) HttpResponse(io.vertx.ext.web.client.HttpResponse) WebClient(io.vertx.ext.web.client.WebClient) Command(org.eclipse.hono.client.command.Command) MultiMap(io.vertx.core.MultiMap) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Constants(org.eclipse.hono.util.Constants) RETURNS_SELF(org.mockito.Mockito.RETURNS_SELF) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) MapperEndpoint(org.eclipse.hono.config.MapperEndpoint) ServerErrorException(org.eclipse.hono.client.ServerErrorException) HttpHeaders(io.vertx.core.http.HttpHeaders) Mockito.times(org.mockito.Mockito.times) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Device(org.eclipse.hono.auth.Device) Mockito.verify(org.mockito.Mockito.verify) HttpRequest(io.vertx.ext.web.client.HttpRequest) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) Mockito.withSettings(org.mockito.Mockito.withSettings) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Device(org.eclipse.hono.auth.Device) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Test(org.junit.jupiter.api.Test)

Example 79 with ResourceIdentifier

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;
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Example 80 with ResourceIdentifier

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);
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Aggregations

ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)82 Message (org.apache.qpid.proton.message.Message)30 Future (io.vertx.core.Future)24 HttpURLConnection (java.net.HttpURLConnection)22 MessageHelper (org.eclipse.hono.util.MessageHelper)22 ClientErrorException (org.eclipse.hono.client.ClientErrorException)20 Test (org.junit.Test)20 Test (org.junit.jupiter.api.Test)19 Handler (io.vertx.core.Handler)18 Map (java.util.Map)18 Span (io.opentracing.Span)17 Buffer (io.vertx.core.buffer.Buffer)17 SpanContext (io.opentracing.SpanContext)16 Constants (org.eclipse.hono.util.Constants)16 Promise (io.vertx.core.Promise)15 Objects (java.util.Objects)14 AsyncResult (io.vertx.core.AsyncResult)13 Vertx (io.vertx.core.Vertx)13 ProtonConnection (io.vertx.proton.ProtonConnection)13 ProtonReceiver (io.vertx.proton.ProtonReceiver)13