Search in sources :

Example 6 with MessageAnnotations

use of org.apache.qpid.protonj2.types.messaging.MessageAnnotations in project qpid-protonj2 by apache.

the class ProtonReceiverTest method testReceiveComplexEncodedAMQPMessageAndDecode.

@Test
public void testReceiveComplexEncodedAMQPMessageAndDecode() throws IOException {
    final String SERIALIZED_JAVA_OBJECT_CONTENT_TYPE = "application/x-java-serialized-object";
    final String JMS_MSG_TYPE = "x-opt-jms-msg-type";
    Engine engine = EngineFactory.PROTON.createNonSaslEngine();
    engine.errorHandler(result -> failure = result.failureCause());
    ProtonTestConnector peer = createTestPeer(engine);
    peer.expectAMQPHeader().respondWithAMQPHeader();
    peer.expectOpen().respond().withContainerId("driver");
    peer.expectBegin().respond();
    peer.expectAttach().respond();
    peer.expectFlow().withDrain(false).withLinkCredit(1);
    Connection connection = engine.start().open();
    Session session = connection.session().open();
    Receiver receiver = session.receiver("test").open().addCredit(1);
    peer.waitForScriptToComplete();
    final AtomicReference<IncomingDelivery> received = new AtomicReference<>();
    receiver.deliveryReadHandler(delivery -> {
        received.set(delivery);
        delivery.disposition(Accepted.getInstance(), true);
    });
    SimplePojo expectedContent = new SimplePojo(UUID.randomUUID());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(expectedContent);
    oos.flush();
    oos.close();
    byte[] bytes = baos.toByteArray();
    peer.expectDisposition().withState().accepted().withSettled(true);
    peer.remoteTransfer().withDeliveryTag(new byte[] { 0 }).withDeliveryId(0).withProperties().withContentType(SERIALIZED_JAVA_OBJECT_CONTENT_TYPE).also().withMessageAnnotations().withAnnotation("x-opt-jms-msg-type", (byte) 1).also().withBody().withData(bytes).also().now();
    peer.waitForScriptToComplete();
    assertNotNull(received.get());
    ProtonBuffer buffer = received.get().readAll();
    MessageAnnotations annotations;
    Properties properties;
    Section<?> body;
    try {
        annotations = (MessageAnnotations) decoder.readObject(buffer, decoderState);
        assertNotNull(annotations);
        assertTrue(annotations.getValue().containsKey(Symbol.valueOf(JMS_MSG_TYPE)));
    } catch (Exception ex) {
        fail("Should not encounter error on decode of MessageAnnotations: " + ex);
    } finally {
        decoderState.reset();
    }
    try {
        properties = (Properties) decoder.readObject(buffer, decoderState);
        assertNotNull(properties);
        assertEquals(SERIALIZED_JAVA_OBJECT_CONTENT_TYPE, properties.getContentType());
    } catch (Exception ex) {
        fail("Should not encounter error on decode of Properties: " + ex);
    } finally {
        decoderState.reset();
    }
    try {
        body = (Section<?>) decoder.readObject(buffer, decoderState);
        assertNotNull(body);
        assertTrue(body instanceof Data);
        Data payload = (Data) body;
        assertEquals(bytes.length, payload.getBinary().getLength());
    } catch (Exception ex) {
        fail("Should not encounter error on decode of Body section: " + ex);
    } finally {
        decoderState.reset();
    }
    peer.expectClose().respond();
    connection.close();
    peer.waitForScriptToComplete();
    assertNull(failure);
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) Connection(org.apache.qpid.protonj2.engine.Connection) Receiver(org.apache.qpid.protonj2.engine.Receiver) AtomicReference(java.util.concurrent.atomic.AtomicReference) Data(org.apache.qpid.protonj2.types.messaging.Data) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Properties(org.apache.qpid.protonj2.types.messaging.Properties) SimplePojo(org.apache.qpid.protonj2.engine.util.SimplePojo) EngineFailedException(org.apache.qpid.protonj2.engine.exceptions.EngineFailedException) EngineShutdownException(org.apache.qpid.protonj2.engine.exceptions.EngineShutdownException) IOException(java.io.IOException) IncomingDelivery(org.apache.qpid.protonj2.engine.IncomingDelivery) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) ProtonTestConnector(org.apache.qpid.protonj2.test.driver.ProtonTestConnector) Engine(org.apache.qpid.protonj2.engine.Engine) Session(org.apache.qpid.protonj2.engine.Session) Test(org.junit.jupiter.api.Test)

Example 7 with MessageAnnotations

use of org.apache.qpid.protonj2.types.messaging.MessageAnnotations in project qpid-protonj2 by apache.

the class ClientMessageSupport method encodeMessage.

public static ProtonBuffer encodeMessage(Encoder encoder, EncoderState encoderState, ProtonBufferAllocator allocator, AdvancedMessage<?> message, Map<String, Object> deliveryAnnotations) throws ClientException {
    ProtonBuffer buffer = allocator.allocate();
    Header header = message.header();
    MessageAnnotations messageAnnotations = message.annotations();
    Properties properties = message.properties();
    ApplicationProperties applicationProperties = message.applicationProperties();
    Footer footer = message.footer();
    if (header != null) {
        encoder.writeObject(buffer, encoderState, header);
    }
    if (deliveryAnnotations != null) {
        encoder.writeObject(buffer, encoderState, new DeliveryAnnotations(StringUtils.toSymbolKeyedMap(deliveryAnnotations)));
    }
    if (messageAnnotations != null) {
        encoder.writeObject(buffer, encoderState, messageAnnotations);
    }
    if (properties != null) {
        encoder.writeObject(buffer, encoderState, properties);
    }
    if (applicationProperties != null) {
        encoder.writeObject(buffer, encoderState, applicationProperties);
    }
    message.forEachBodySection(section -> encoder.writeObject(buffer, encoderState, section));
    if (footer != null) {
        encoder.writeObject(buffer, encoderState, footer);
    }
    return buffer;
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) Header(org.apache.qpid.protonj2.types.messaging.Header) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) Footer(org.apache.qpid.protonj2.types.messaging.Footer) DeliveryAnnotations(org.apache.qpid.protonj2.types.messaging.DeliveryAnnotations) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) Properties(org.apache.qpid.protonj2.types.messaging.Properties) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties)

Example 8 with MessageAnnotations

use of org.apache.qpid.protonj2.types.messaging.MessageAnnotations in project qpid-protonj2 by apache.

the class ClientMessageTest method testCreateEmptyAdvanced.

@Test
public void testCreateEmptyAdvanced() throws ClientException {
    AdvancedMessage<String> message = ClientMessage.createAdvancedMessage();
    assertNull(message.body());
    assertNotNull(message.bodySections());
    assertTrue(message.bodySections().isEmpty());
    assertFalse(message.hasProperties());
    assertFalse(message.hasFooters());
    assertFalse(message.hasAnnotations());
    assertNull(message.header());
    assertNull(message.annotations());
    assertNull(message.applicationProperties());
    assertNull(message.footer());
    Header header = new Header();
    Properties properties = new Properties();
    MessageAnnotations ma = new MessageAnnotations(new LinkedHashMap<>());
    ApplicationProperties ap = new ApplicationProperties(new LinkedHashMap<>());
    Footer ft = new Footer(new LinkedHashMap<>());
    message.header(header);
    message.properties(properties);
    message.annotations(ma);
    message.applicationProperties(ap);
    message.footer(ft);
    assertSame(header, message.header());
    assertSame(properties, message.properties());
    assertSame(ma, message.annotations());
    assertSame(ap, message.applicationProperties());
    assertSame(ft, message.footer());
}
Also used : Header(org.apache.qpid.protonj2.types.messaging.Header) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) Footer(org.apache.qpid.protonj2.types.messaging.Footer) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) Properties(org.apache.qpid.protonj2.types.messaging.Properties) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) Test(org.junit.jupiter.api.Test)

Example 9 with MessageAnnotations

use of org.apache.qpid.protonj2.types.messaging.MessageAnnotations in project qpid-protonj2 by apache.

the class StreamReceiverTest method testTryReadSectionBeyondWhatIsEncodedIntoMessage.

@Test
public void testTryReadSectionBeyondWhatIsEncodedIntoMessage() throws Exception {
    Map<Symbol, Object> annotationsMap = new HashMap<>();
    annotationsMap.put(Symbol.valueOf("test-1"), UUID.randomUUID());
    annotationsMap.put(Symbol.valueOf("test-2"), UUID.randomUUID());
    annotationsMap.put(Symbol.valueOf("test-3"), UUID.randomUUID());
    final byte[] payload = createEncodedMessage(new Header(), new MessageAnnotations(annotationsMap));
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        peer.expectFlow();
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
        peer.expectDisposition().withFirst(0).withState().accepted().withSettled(true);
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        final Client container = Client.create();
        final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
        final StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        assertTrue(delivery.completed());
        assertFalse(delivery.aborted());
        StreamReceiverMessage message = delivery.message();
        assertNotNull(message);
        Properties properties = message.properties();
        assertNull(properties);
        Header header = message.header();
        assertNotNull(header);
        MessageAnnotations annotations = message.annotations();
        assertNotNull(annotations);
        assertEquals(annotationsMap, annotations.getValue());
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) HashMap(java.util.HashMap) Symbol(org.apache.qpid.protonj2.types.Symbol) Connection(org.apache.qpid.protonj2.client.Connection) Properties(org.apache.qpid.protonj2.types.messaging.Properties) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) URI(java.net.URI) StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) Header(org.apache.qpid.protonj2.types.messaging.Header) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) Client(org.apache.qpid.protonj2.client.Client) Test(org.junit.jupiter.api.Test)

Example 10 with MessageAnnotations

use of org.apache.qpid.protonj2.types.messaging.MessageAnnotations in project qpid-protonj2 by apache.

the class StreamReceiverTest method testReadHeaderFromStreamMessageWithoutHeaderSection.

@Test
public void testReadHeaderFromStreamMessageWithoutHeaderSection() throws Exception {
    Map<Symbol, Object> annotationsMap = new HashMap<>();
    annotationsMap.put(Symbol.valueOf("test-1"), UUID.randomUUID());
    annotationsMap.put(Symbol.valueOf("test-2"), UUID.randomUUID());
    annotationsMap.put(Symbol.valueOf("test-3"), UUID.randomUUID());
    final byte[] payload = createEncodedMessage(new MessageAnnotations(annotationsMap));
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().withRole(Role.RECEIVER.getValue()).respond();
        peer.expectFlow();
        peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(false).withMessageFormat(0).withPayload(payload).queue();
        peer.expectDisposition().withSettled(true).withState().accepted();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        final Client container = Client.create();
        final Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        final StreamReceiver receiver = connection.openStreamReceiver("test-queue");
        final StreamDelivery delivery = receiver.receive();
        assertNotNull(delivery);
        assertTrue(delivery.completed());
        assertFalse(delivery.aborted());
        StreamReceiverMessage message = delivery.message();
        assertNotNull(message);
        Header header = message.header();
        assertNull(header);
        MessageAnnotations annotations = message.annotations();
        assertNotNull(annotations);
        assertEquals(annotationsMap, annotations.getValue());
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) HashMap(java.util.HashMap) Symbol(org.apache.qpid.protonj2.types.Symbol) Connection(org.apache.qpid.protonj2.client.Connection) URI(java.net.URI) StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) Header(org.apache.qpid.protonj2.types.messaging.Header) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) Client(org.apache.qpid.protonj2.client.Client) Test(org.junit.jupiter.api.Test)

Aggregations

MessageAnnotations (org.apache.qpid.protonj2.types.messaging.MessageAnnotations)19 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)9 InputStream (java.io.InputStream)8 Symbol (org.apache.qpid.protonj2.types.Symbol)8 HashMap (java.util.HashMap)7 ProtonBufferInputStream (org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)7 Header (org.apache.qpid.protonj2.types.messaging.Header)6 Properties (org.apache.qpid.protonj2.types.messaging.Properties)6 Test (org.junit.jupiter.api.Test)6 ApplicationProperties (org.apache.qpid.protonj2.types.messaging.ApplicationProperties)5 Footer (org.apache.qpid.protonj2.types.messaging.Footer)4 URI (java.net.URI)3 Client (org.apache.qpid.protonj2.client.Client)3 Connection (org.apache.qpid.protonj2.client.Connection)3 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)3 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)3 StreamReceiverMessage (org.apache.qpid.protonj2.client.StreamReceiverMessage)3 IOException (java.io.IOException)2 UUID (java.util.UUID)2 MapTypeDecoder (org.apache.qpid.protonj2.codec.decoders.primitives.MapTypeDecoder)2