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;
}
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;
}
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();
}));
}
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);
}
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;
}
Aggregations