Search in sources :

Example 6 with AmqpValue

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

the class MessageHelper method getPayload.

/**
 * Gets a message's body as String object that can be used for constructing a JsonObject or bind a POJO using
 * jackson-databind e.g.
 *
 * @param msg The AMQP 1.0 message to parse the body of.
 * @return The message body parsed into a JSON object or {@code null} if the message does not have a <em>Data</em>
 *         nor an <em>AmqpValue</em> section.
 * @throws NullPointerException if the message is {@code null}.
 */
public static String getPayload(final Message msg) {
    Objects.requireNonNull(msg);
    if (msg.getBody() == null) {
        LOG.trace("message has no body");
        return null;
    }
    if (msg.getBody() instanceof Data) {
        Data body = (Data) msg.getBody();
        return new String(body.getValue().getArray(), StandardCharsets.UTF_8);
    } else if (msg.getBody() instanceof AmqpValue) {
        AmqpValue body = (AmqpValue) msg.getBody();
        if (body.getValue() instanceof String) {
            return (String) body.getValue();
        }
    }
    LOG.debug("unsupported body type [{}]", msg.getBody().getClass().getName());
    return null;
}
Also used : Data(org.apache.qpid.proton.amqp.messaging.Data) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 7 with AmqpValue

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

the class RequestResponseApiConstants method getAmqpReply.

/**
 * Creates an AMQP message from a response to a service invocation.
 *
 * @param endpoint The service endpoint that the operation has been invoked on.
 * @param response The response message.
 * @return The AMQP message.
 * @throws NullPointerException if endpoint is {@code null}.
 */
public static final Message getAmqpReply(final String endpoint, final EventBusMessage response) {
    Objects.requireNonNull(endpoint);
    Objects.requireNonNull(response);
    final Object correlationId = response.getCorrelationId();
    if (correlationId == null) {
        throw new IllegalArgumentException("response must contain correlation ID");
    } else {
        final String tenantId = response.getTenant();
        final String deviceId = response.getDeviceId();
        final Integer status = response.getStatus();
        final boolean isApplCorrelationId = response.isAppCorrelationId();
        final String cacheDirective = response.getCacheDirective();
        final JsonObject payload = response.getJsonPayload();
        final ResourceIdentifier address = ResourceIdentifier.from(endpoint, tenantId, deviceId);
        final Message message = ProtonHelper.message();
        message.setMessageId(UUID.randomUUID().toString());
        message.setCorrelationId(correlationId);
        message.setAddress(address.toString());
        final Map<String, Object> map = new HashMap<>();
        map.put(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
        map.put(MessageHelper.APP_PROPERTY_STATUS, status);
        if (deviceId != null) {
            map.put(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId);
        }
        if (cacheDirective != null) {
            map.put(MessageHelper.APP_PROPERTY_CACHE_CONTROL, cacheDirective);
        }
        message.setApplicationProperties(new ApplicationProperties(map));
        if (isApplCorrelationId) {
            final Map<Symbol, Object> annotations = new HashMap<>();
            annotations.put(Symbol.valueOf(MessageHelper.ANNOTATION_X_OPT_APP_CORRELATION_ID), true);
            message.setMessageAnnotations(new MessageAnnotations(annotations));
        }
        if (payload != null) {
            message.setContentType(CONTENT_TYPE_APPLICATION_JSON);
            message.setBody(new AmqpValue(payload.encode()));
        }
        return message;
    }
}
Also used : Message(org.apache.qpid.proton.message.Message) HashMap(java.util.HashMap) Symbol(org.apache.qpid.proton.amqp.Symbol) JsonObject(io.vertx.core.json.JsonObject) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) MessageAnnotations(org.apache.qpid.proton.amqp.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) JsonObject(io.vertx.core.json.JsonObject)

Example 8 with AmqpValue

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

the class ExampleReceiver method handleMessage.

private void handleMessage(final String endpoint, final Message msg) {
    final String deviceId = MessageHelper.getDeviceId(msg);
    final Section body = msg.getBody();
    String content = null;
    if (body instanceof Data) {
        content = ((Data) msg.getBody()).getValue().toString();
    } else if (body instanceof AmqpValue) {
        content = ((AmqpValue) msg.getBody()).getValue().toString();
    }
    LOG.info("received {} message [device: {}, content-type: {}]: {}", endpoint, deviceId, msg.getContentType(), content);
    if (msg.getApplicationProperties() != null) {
        LOG.info("... with application properties: {}", msg.getApplicationProperties().getValue());
    }
}
Also used : Data(org.apache.qpid.proton.amqp.messaging.Data) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 9 with AmqpValue

use of org.apache.qpid.proton.amqp.messaging.AmqpValue in project vertx-proton by vert-x3.

the class ProtonClientTest method getMessageBody.

private Object getMessageBody(TestContext context, Message msg) {
    Section body = msg.getBody();
    context.assertNotNull(body);
    context.assertTrue(body instanceof AmqpValue);
    return ((AmqpValue) body).getValue();
}
Also used : Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 10 with AmqpValue

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

the class AbstractRequestResponseClientTest method testCreateAndSendRequestSendsProperRequestMessage.

/**
 * Verifies that the client creates and sends a message based on provided headers and payload
 * and sets a timer for canceling the request if no response is received.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testCreateAndSendRequestSendsProperRequestMessage(final TestContext ctx) {
    // GIVEN a request-response client that times out requests after 200 ms
    client.setRequestTimeout(200);
    // WHEN sending a request message with some headers and payload
    final JsonObject payload = new JsonObject().put("key", "value");
    final Map<String, Object> props = Collections.singletonMap("test-key", "test-value");
    client.createAndSendRequest("get", props, payload, s -> {
    });
    // THEN the message is sent and the message being sent contains the headers as application properties
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    verify(sender).send(messageCaptor.capture(), any(Handler.class));
    assertThat(messageCaptor.getValue(), is(notNullValue()));
    assertThat(messageCaptor.getValue().getBody(), is(notNullValue()));
    assertThat(messageCaptor.getValue().getBody(), instanceOf(AmqpValue.class));
    final AmqpValue body = (AmqpValue) messageCaptor.getValue().getBody();
    assertThat(body.getValue(), is(payload.encode()));
    assertThat(messageCaptor.getValue().getApplicationProperties(), is(notNullValue()));
    assertThat(messageCaptor.getValue().getApplicationProperties().getValue().get("test-key"), is("test-value"));
    // and a timer has been set to time out the request after 200 ms
    verify(vertx).setTimer(eq(200L), any(Handler.class));
}
Also used : Message(org.apache.qpid.proton.message.Message) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) JsonObject(io.vertx.core.json.JsonObject) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Test(org.junit.Test)

Aggregations

AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)14 Message (org.apache.qpid.proton.message.Message)10 Test (org.junit.Test)6 Section (org.apache.qpid.proton.amqp.messaging.Section)5 JsonObject (io.vertx.core.json.JsonObject)4 Vertx (io.vertx.core.Vertx)3 ProtonConnection (io.vertx.proton.ProtonConnection)3 Handler (io.vertx.core.Handler)2 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)2 Data (org.apache.qpid.proton.amqp.messaging.Data)2 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 ProtonClient (io.vertx.proton.ProtonClient)1 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)1 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)1 ProtonReceiver (io.vertx.proton.ProtonReceiver)1 ProtonSender (io.vertx.proton.ProtonSender)1 ProtonServer (io.vertx.proton.ProtonServer)1 HashMap (java.util.HashMap)1 Objects (java.util.Objects)1