Search in sources :

Example 31 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-service-bus-java by Azure.

the class RequestResponseUtils method createRequestMessageFromValueBody.

private static Message createRequestMessageFromValueBody(String operation, Object valueBody, Duration timeout, String associatedLinkName) {
    Message requestMessage = Message.Factory.create();
    requestMessage.setBody(new AmqpValue(valueBody));
    HashMap applicationPropertiesMap = new HashMap();
    applicationPropertiesMap.put(ClientConstants.REQUEST_RESPONSE_OPERATION_NAME, operation);
    applicationPropertiesMap.put(ClientConstants.REQUEST_RESPONSE_TIMEOUT, timeout.toMillis());
    if (!StringUtil.isNullOrEmpty(associatedLinkName)) {
        applicationPropertiesMap.put(ClientConstants.REQUEST_RESPONSE_ASSOCIATED_LINK_NAME, associatedLinkName);
    }
    requestMessage.setApplicationProperties(new ApplicationProperties(applicationPropertiesMap));
    return requestMessage;
}
Also used : Message(org.apache.qpid.proton.message.Message) HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 32 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project azure-service-bus-java by Azure.

the class MessageConverter method convertBrokeredMessageToAmqpMessage.

public static org.apache.qpid.proton.message.Message convertBrokeredMessageToAmqpMessage(Message brokeredMessage) {
    org.apache.qpid.proton.message.Message amqpMessage = Proton.message();
    if (brokeredMessage.getBody() != null) {
        amqpMessage.setBody(new Data(new Binary(brokeredMessage.getBody())));
    }
    if (brokeredMessage.getProperties() != null) {
        amqpMessage.setApplicationProperties(new ApplicationProperties(brokeredMessage.getProperties()));
    }
    if (brokeredMessage.getTimeToLive() != null) {
        amqpMessage.setTtl(brokeredMessage.getTimeToLive().toMillis());
    }
    amqpMessage.setMessageId(brokeredMessage.getMessageId());
    amqpMessage.setContentType(brokeredMessage.getContentType());
    amqpMessage.setCorrelationId(brokeredMessage.getCorrelationId());
    amqpMessage.setSubject(brokeredMessage.getLabel());
    amqpMessage.getProperties().setTo(brokeredMessage.getTo());
    amqpMessage.setReplyTo(brokeredMessage.getReplyTo());
    amqpMessage.setReplyToGroupId(brokeredMessage.getReplyToSessionId());
    amqpMessage.setGroupId(brokeredMessage.getSessionId());
    Map<Symbol, Object> messageAnnotationsMap = new HashMap<Symbol, Object>();
    if (brokeredMessage.getScheduledEnqueuedTimeUtc() != null) {
        messageAnnotationsMap.put(Symbol.valueOf(ClientConstants.SCHEDULEDENQUEUETIMENAME), Date.from(brokeredMessage.getScheduledEnqueuedTimeUtc()));
    }
    if (!StringUtil.isNullOrEmpty(brokeredMessage.getPartitionKey())) {
        messageAnnotationsMap.put(Symbol.valueOf(ClientConstants.PARTITIONKEYNAME), brokeredMessage.getPartitionKey());
    }
    amqpMessage.setMessageAnnotations(new MessageAnnotations(messageAnnotationsMap));
    return amqpMessage;
}
Also used : HashMap(java.util.HashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Data(org.apache.qpid.proton.amqp.messaging.Data) Binary(org.apache.qpid.proton.amqp.Binary)

Example 33 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.

the class VertxBasedAmqpProtocolAdapterTest method testUploadCommandResponseSucceeds.

/**
 * Verify that the AMQP adapter forwards command responses downstream.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUploadCommandResponseSucceeds(final VertxTestContext ctx) {
    // GIVEN an AMQP adapter
    givenAnAdapter(properties);
    final CommandResponseSender responseSender = givenACommandResponseSenderForAnyTenant();
    when(responseSender.sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any())).thenReturn(Future.succeededFuture());
    // which is enabled for the test tenant
    final TenantObject tenantObject = givenAConfiguredTenant(TEST_TENANT_ID, true);
    // WHEN an unauthenticated device publishes a command response
    final String replyToAddress = String.format("%s/%s/%s", getCommandResponseEndpoint(), TEST_TENANT_ID, Commands.getDeviceFacingReplyToId("test-reply-id", TEST_DEVICE, MessagingType.amqp));
    final Map<String, Object> propertyMap = new HashMap<>();
    propertyMap.put(MessageHelper.APP_PROPERTY_STATUS, 200);
    final ApplicationProperties props = new ApplicationProperties(propertyMap);
    final Buffer payload = Buffer.buffer("some payload");
    final Message message = getFakeMessage(replyToAddress, payload);
    message.setCorrelationId("correlation-id");
    message.setApplicationProperties(props);
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    adapter.onMessageReceived(AmqpContext.fromMessage(delivery, message, span, null)).onComplete(ctx.succeeding(ok -> {
        ctx.verify(() -> {
            // THEN the adapter forwards the command response message downstream
            verify(responseSender).sendCommandResponse(eq(tenantObject), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any());
            // and reports the forwarded message
            verify(metrics).reportCommand(eq(Direction.RESPONSE), eq(TEST_TENANT_ID), eq(tenantObject), eq(ProcessingOutcome.FORWARDED), eq(payload.length()), any());
        });
        ctx.completeNow();
    }));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ProcessingOutcome(org.eclipse.hono.service.metric.MetricsTags.ProcessingOutcome) MessagingType(org.eclipse.hono.util.MessagingType) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) CommandContext(org.eclipse.hono.client.command.CommandContext) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) EventConstants(org.eclipse.hono.util.EventConstants) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) Mockito.mock(org.mockito.Mockito.mock) VertxTestContext(io.vertx.junit5.VertxTestContext) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Target(org.apache.qpid.proton.amqp.messaging.Target) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Commands(org.eclipse.hono.client.command.Commands) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AsyncResult(io.vertx.core.AsyncResult) CommandConstants(org.eclipse.hono.util.CommandConstants) Direction(org.eclipse.hono.service.metric.MetricsTags.Direction) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) ProtonHelper(io.vertx.proton.ProtonHelper) Released(org.apache.qpid.proton.amqp.messaging.Released) SpanContext(io.opentracing.SpanContext) Mockito.never(org.mockito.Mockito.never) ConnectionAttemptOutcome(org.eclipse.hono.service.metric.MetricsTags.ConnectionAttemptOutcome) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ProtonReceiver(io.vertx.proton.ProtonReceiver) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) Timeout(io.vertx.junit5.Timeout) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType) NotificationType(org.eclipse.hono.notification.NotificationType) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Binary(org.apache.qpid.proton.amqp.Binary) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) Data(org.apache.qpid.proton.amqp.messaging.Data) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) ProtonQoS(io.vertx.proton.ProtonQoS) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Buffer(io.vertx.core.buffer.Buffer) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) Mockito.inOrder(org.mockito.Mockito.inOrder) ProtonSender(io.vertx.proton.ProtonSender) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) QoS(org.eclipse.hono.util.QoS) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) ConnectionLimitManager(org.eclipse.hono.adapter.limiting.ConnectionLimitManager) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Constants(org.eclipse.hono.util.Constants) ProtocolAdapterTestSupport(org.eclipse.hono.adapter.test.ProtocolAdapterTestSupport) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl) Message(org.apache.qpid.proton.message.Message) HttpUtils(org.eclipse.hono.service.http.HttpUtils) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) InOrder(org.mockito.InOrder) Promise(io.vertx.core.Promise) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Source(org.apache.qpid.proton.amqp.transport.Source) Adapter(org.eclipse.hono.util.Adapter) Handler(io.vertx.core.Handler) Record(org.apache.qpid.proton.engine.Record) SpanContext(io.opentracing.SpanContext) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) HashMap(java.util.HashMap) CommandResponse(org.eclipse.hono.client.command.CommandResponse) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test)

Example 34 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.

the class ProtonBasedInternalCommandConsumerTest method testHandleCommandMessageWithHandlerForGateway.

/**
 * Verifies that the consumer handles a valid message, targeted at a gateway, by invoking the matching command
 * handler.
 */
@Test
void testHandleCommandMessageWithHandlerForGateway() {
    final String deviceId = "4711";
    final String gatewayId = "gw-1";
    final String correlationId = "the-correlation-id";
    final Message message = ProtonHelper.message("input data");
    message.setAddress(String.format("%s/%s/%s", CommandConstants.COMMAND_ENDPOINT, Constants.DEFAULT_TENANT, deviceId));
    message.setSubject("doThis");
    message.setCorrelationId(correlationId);
    message.setApplicationProperties(new ApplicationProperties(Collections.singletonMap(MessageHelper.APP_PROPERTY_CMD_VIA, gatewayId)));
    final Handler<CommandContext> commandHandler = VertxMockSupport.mockHandler();
    commandHandlers.putCommandHandler(Constants.DEFAULT_TENANT, gatewayId, null, commandHandler, context);
    internalCommandConsumer.handleCommandMessage(mock(ProtonDelivery.class), message);
    final ArgumentCaptor<CommandContext> commandContextCaptor = ArgumentCaptor.forClass(CommandContext.class);
    verify(commandHandler).handle(commandContextCaptor.capture());
    assertThat(commandContextCaptor.getValue()).isNotNull();
    // assert that command is directed at the gateway
    assertThat(commandContextCaptor.getValue().getCommand().getGatewayId()).isEqualTo(gatewayId);
    assertThat(commandContextCaptor.getValue().getCommand().getDeviceId()).isEqualTo(deviceId);
}
Also used : Message(org.apache.qpid.proton.message.Message) CommandContext(org.eclipse.hono.client.command.CommandContext) ProtonDelivery(io.vertx.proton.ProtonDelivery) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) Test(org.junit.jupiter.api.Test)

Example 35 with ApplicationProperties

use of org.apache.qpid.proton.amqp.messaging.ApplicationProperties in project hono by eclipse.

the class RequestResponseApiConstants method getAmqpReply.

/**
 * Creates an AMQP message from a result to a service invocation.
 *
 * @param endpoint The service endpoint that the operation has been invoked on.
 * @param tenantId The id of the tenant (may be {@code null}).
 * @param request The request message.
 * @param result The result message.
 * @return The AMQP message.
 * @throws NullPointerException if endpoint, request or result is {@code null}.
 * @throws IllegalArgumentException if the result does not contain a correlation ID.
 */
public static final Message getAmqpReply(final String endpoint, final String tenantId, final Message request, final RequestResponseResult<JsonObject> result) {
    Objects.requireNonNull(endpoint);
    Objects.requireNonNull(request);
    Objects.requireNonNull(result);
    final Object correlationId = MessageHelper.getCorrelationId(request);
    if (correlationId == null) {
        throw new IllegalArgumentException("request must contain correlation ID");
    }
    final String deviceId = MessageHelper.getDeviceId(request);
    final ResourceIdentifier address = ResourceIdentifier.from(endpoint, tenantId, deviceId);
    final Message message = ProtonHelper.message();
    message.setMessageId(UUID.randomUUID().toString());
    message.setCorrelationId(correlationId.toString());
    message.setAddress(address.toString());
    final Map<String, Object> map = new HashMap<>();
    map.put(MessageHelper.APP_PROPERTY_STATUS, result.getStatus());
    if (tenantId != null) {
        map.put(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
    }
    if (deviceId != null) {
        map.put(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
    }
    if (result.getCacheDirective() != null) {
        map.put(MessageHelper.APP_PROPERTY_CACHE_CONTROL, result.getCacheDirective().toString());
    }
    message.setApplicationProperties(new ApplicationProperties(map));
    MessageHelper.setJsonPayload(message, result.getPayload());
    return message;
}
Also used : Message(org.apache.qpid.proton.message.Message) HashMap(java.util.HashMap) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

ApplicationProperties (org.apache.qpid.proton.amqp.messaging.ApplicationProperties)41 HashMap (java.util.HashMap)25 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)14 Properties (org.apache.qpid.proton.amqp.messaging.Properties)12 AMQPMessage (org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage)11 Message (org.apache.qpid.proton.message.Message)11 Binary (org.apache.qpid.proton.amqp.Binary)10 MessageAnnotations (org.apache.qpid.proton.amqp.messaging.MessageAnnotations)10 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)9 Test (org.junit.Test)9 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)8 Data (org.apache.qpid.proton.amqp.messaging.Data)8 Symbol (org.apache.qpid.proton.amqp.Symbol)7 Section (org.apache.qpid.proton.amqp.messaging.Section)7 Map (java.util.Map)6 Header (org.apache.qpid.proton.amqp.messaging.Header)6 Test (org.junit.jupiter.api.Test)5 Buffer (io.vertx.core.buffer.Buffer)3 JsonObject (io.vertx.core.json.JsonObject)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3