Search in sources :

Example 1 with MessageAnnotations

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

the class ClientMessageSupport method convertFromOutsideMessage.

// ----- Internal Implementation
private static <E> ClientMessage<E> convertFromOutsideMessage(Message<E> source) throws ClientException {
    Header header = new Header();
    header.setDurable(source.durable());
    header.setPriority(source.priority());
    header.setTimeToLive(source.timeToLive());
    header.setFirstAcquirer(source.firstAcquirer());
    header.setDeliveryCount(source.deliveryCount());
    Properties properties = new Properties();
    properties.setMessageId(source.messageId());
    properties.setUserId(source.userId() != null ? new Binary(source.userId()) : null);
    properties.setTo(source.to());
    properties.setSubject(source.subject());
    properties.setReplyTo(source.replyTo());
    properties.setCorrelationId(source.correlationId());
    properties.setContentType(source.contentType());
    properties.setContentEncoding(source.contentEncoding());
    properties.setAbsoluteExpiryTime(source.absoluteExpiryTime());
    properties.setCreationTime(source.creationTime());
    properties.setGroupId(source.groupId());
    properties.setGroupSequence(source.groupSequence());
    properties.setReplyToGroupId(source.replyToGroupId());
    final MessageAnnotations messageAnnotations;
    if (source.hasAnnotations()) {
        messageAnnotations = new MessageAnnotations(new LinkedHashMap<>());
        source.forEachAnnotation((key, value) -> {
            messageAnnotations.getValue().put(Symbol.valueOf(key), value);
        });
    } else {
        messageAnnotations = null;
    }
    final ApplicationProperties applicationProperties;
    if (source.hasProperties()) {
        applicationProperties = new ApplicationProperties(new LinkedHashMap<>());
        source.forEachProperty((key, value) -> {
            applicationProperties.getValue().put(key, value);
        });
    } else {
        applicationProperties = null;
    }
    final Footer footer;
    if (source.hasFooters()) {
        footer = new Footer(new LinkedHashMap<>());
        source.forEachFooter((key, value) -> {
            footer.getValue().put(Symbol.valueOf(key), value);
        });
    } else {
        footer = null;
    }
    ClientMessage<E> message = new ClientMessage<>(createSectionFromValue(source.body()));
    message.header(header);
    message.properties(properties);
    message.annotations(messageAnnotations);
    message.applicationProperties(applicationProperties);
    message.footer(footer);
    return message;
}
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) Binary(org.apache.qpid.protonj2.types.Binary) Properties(org.apache.qpid.protonj2.types.messaging.Properties) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with MessageAnnotations

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

the class StreamReceiverTest method testStreamReceiverMessageThrowsOnAnyMessageModificationAPI.

@Test
public void testStreamReceiverMessageThrowsOnAnyMessageModificationAPI() throws Exception {
    final byte[] body = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    final byte[] payload = createEncodedMessage(new Data(body));
    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();
        final StreamReceiverMessage message = delivery.message();
        assertThrows(ClientUnsupportedOperationException.class, () -> message.header(new Header()));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.properties(new Properties()));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.applicationProperties(new ApplicationProperties(null)));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.annotations(new MessageAnnotations(null)));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.footer(new Footer(null)));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.messageFormat(1));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.durable(true));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.priority((byte) 4));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.timeToLive(128));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.firstAcquirer(false));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.deliveryCount(10));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.messageId(10));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.correlationId(10));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.userId(new byte[] { 1 }));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.to("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.subject("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.replyTo("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.contentType("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.contentEncoding("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.absoluteExpiryTime(10));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.creationTime(10));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.groupId("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.groupSequence(10));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.replyToGroupId("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.annotation("test", 1));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.removeAnnotation("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.property("test", 1));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.removeProperty("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.footer("test", 1));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.removeFooter("test"));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.body(InputStream.nullInputStream()));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.addBodySection(new AmqpValue<>("test")));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.bodySections(Collections.emptyList()));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.bodySections());
        assertThrows(ClientUnsupportedOperationException.class, () -> message.clearBodySections());
        assertThrows(ClientUnsupportedOperationException.class, () -> message.forEachBodySection((section) -> {
        }));
        assertThrows(ClientUnsupportedOperationException.class, () -> message.encode(Collections.emptyMap()));
        InputStream bodyStream = message.body();
        assertNotNull(bodyStream.readAllBytes());
        bodyStream.close();
        peer.expectDetach().respond();
        peer.expectEnd().respond();
        peer.expectClose().respond();
        receiver.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Properties(org.apache.qpid.protonj2.types.messaging.Properties) Random(java.util.Random) ImperativeClientTestCase(org.apache.qpid.protonj2.client.test.ImperativeClientTestCase) Future(java.util.concurrent.Future) ClientLinkRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRemotelyClosedException) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Data(org.apache.qpid.protonj2.types.messaging.Data) Map(java.util.Map) URI(java.net.URI) ClientOperationTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException) Accepted(org.apache.qpid.protonj2.test.driver.codec.messaging.Accepted) EncodingCodes(org.apache.qpid.protonj2.codec.EncodingCodes) ReceiverOptions(org.apache.qpid.protonj2.client.ReceiverOptions) SenderOptions(org.apache.qpid.protonj2.client.SenderOptions) Binary(org.apache.qpid.protonj2.types.Binary) Footer(org.apache.qpid.protonj2.types.messaging.Footer) UUID(java.util.UUID) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) ClientUnsupportedOperationException(org.apache.qpid.protonj2.client.exceptions.ClientUnsupportedOperationException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) AmqpValue(org.apache.qpid.protonj2.types.messaging.AmqpValue) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) Wait(org.apache.qpid.protonj2.client.test.Wait) Symbol(org.apache.qpid.protonj2.types.Symbol) ArrayList(java.util.ArrayList) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) DeliveryState(org.apache.qpid.protonj2.client.DeliveryState) UnsignedInteger(org.apache.qpid.protonj2.types.UnsignedInteger) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Role(org.apache.qpid.protonj2.types.transport.Role) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Header(org.apache.qpid.protonj2.types.messaging.Header) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Logger(org.slf4j.Logger) DeliveryAnnotations(org.apache.qpid.protonj2.test.driver.codec.messaging.DeliveryAnnotations) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ClientIllegalStateException(org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException) IOException(java.io.IOException) ClientDeliveryAbortedException(org.apache.qpid.protonj2.client.exceptions.ClientDeliveryAbortedException) StreamReceiverOptions(org.apache.qpid.protonj2.client.StreamReceiverOptions) ErrorCondition(org.apache.qpid.protonj2.client.ErrorCondition) AmqpSequence(org.apache.qpid.protonj2.types.messaging.AmqpSequence) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) InputStream(java.io.InputStream) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) InputStream(java.io.InputStream) Connection(org.apache.qpid.protonj2.client.Connection) Data(org.apache.qpid.protonj2.types.messaging.Data) Properties(org.apache.qpid.protonj2.types.messaging.Properties) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) URI(java.net.URI) AmqpValue(org.apache.qpid.protonj2.types.messaging.AmqpValue) StreamDelivery(org.apache.qpid.protonj2.client.StreamDelivery) Header(org.apache.qpid.protonj2.types.messaging.Header) StreamReceiver(org.apache.qpid.protonj2.client.StreamReceiver) StreamReceiverMessage(org.apache.qpid.protonj2.client.StreamReceiverMessage) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) Footer(org.apache.qpid.protonj2.types.messaging.Footer) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) Client(org.apache.qpid.protonj2.client.Client) Test(org.junit.jupiter.api.Test)

Example 3 with MessageAnnotations

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

the class ClientMessageTest method testMessageAnnotationsAccessorHandlerNullMapOrEmptyMap.

@Test
public void testMessageAnnotationsAccessorHandlerNullMapOrEmptyMap() {
    ClientMessage<String> message = ClientMessage.create();
    assertNull(message.annotations());
    assertNull(message.annotation("test"));
    assertFalse(message.hasAnnotation("test"));
    assertFalse(message.hasAnnotations());
    message.annotations(new MessageAnnotations(null));
    assertNotNull(message.annotations());
    assertNull(message.annotation("test"));
    assertFalse(message.hasAnnotation("test"));
    assertFalse(message.hasAnnotations());
}
Also used : MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) Test(org.junit.jupiter.api.Test)

Example 4 with MessageAnnotations

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

the class MessageAnnotationsTypeCodecTest method doTestSkipValue.

private void doTestSkipValue(boolean fromStream) throws IOException {
    ProtonBuffer buffer = ProtonByteBufferAllocator.DEFAULT.allocate();
    InputStream stream = new ProtonBufferInputStream(buffer);
    Map<Symbol, Object> map = new HashMap<>();
    map.put(Symbol.valueOf("one"), 1);
    map.put(Symbol.valueOf("two"), Boolean.TRUE);
    map.put(Symbol.valueOf("three"), "test");
    for (int i = 0; i < 10; ++i) {
        encoder.writeObject(buffer, encoderState, new MessageAnnotations(map));
    }
    encoder.writeObject(buffer, encoderState, new Modified());
    for (int i = 0; i < 10; ++i) {
        if (fromStream) {
            StreamTypeDecoder<?> typeDecoder = streamDecoder.readNextTypeDecoder(stream, streamDecoderState);
            assertEquals(MessageAnnotations.class, typeDecoder.getTypeClass());
            typeDecoder.skipValue(stream, streamDecoderState);
        } else {
            TypeDecoder<?> typeDecoder = decoder.readNextTypeDecoder(buffer, decoderState);
            assertEquals(MessageAnnotations.class, typeDecoder.getTypeClass());
            typeDecoder.skipValue(buffer, decoderState);
        }
    }
    final Object result;
    if (fromStream) {
        result = streamDecoder.readObject(stream, streamDecoderState);
    } else {
        result = decoder.readObject(buffer, decoderState);
    }
    assertNotNull(result);
    assertTrue(result instanceof Modified);
    Modified modified = (Modified) result;
    assertFalse(modified.isUndeliverableHere());
    assertFalse(modified.isDeliveryFailed());
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) Modified(org.apache.qpid.protonj2.types.messaging.Modified) HashMap(java.util.HashMap) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) InputStream(java.io.InputStream) Symbol(org.apache.qpid.protonj2.types.Symbol) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)

Example 5 with MessageAnnotations

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

the class MessageAnnotationsTypeCodecTest method doTestDecodeMessageAnnotationsSeries.

private void doTestDecodeMessageAnnotationsSeries(int size, boolean fromStream) throws IOException {
    final Symbol SYMBOL_1 = Symbol.valueOf("test1");
    final Symbol SYMBOL_2 = Symbol.valueOf("test2");
    final Symbol SYMBOL_3 = Symbol.valueOf("test3");
    MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
    annotations.getValue().put(SYMBOL_1, UnsignedByte.valueOf((byte) 128));
    annotations.getValue().put(SYMBOL_2, UnsignedShort.valueOf((short) 128));
    annotations.getValue().put(SYMBOL_3, UnsignedInteger.valueOf(128));
    ProtonBuffer buffer = ProtonByteBufferAllocator.DEFAULT.allocate();
    InputStream stream = new ProtonBufferInputStream(buffer);
    for (int i = 0; i < size; ++i) {
        encoder.writeObject(buffer, encoderState, annotations);
    }
    for (int i = 0; i < size; ++i) {
        final Object result;
        if (fromStream) {
            result = streamDecoder.readObject(stream, streamDecoderState);
        } else {
            result = decoder.readObject(buffer, decoderState);
        }
        assertNotNull(result);
        assertTrue(result instanceof MessageAnnotations);
        MessageAnnotations readAnnotations = (MessageAnnotations) result;
        Map<Symbol, Object> resultMap = readAnnotations.getValue();
        assertEquals(annotations.getValue().size(), resultMap.size());
        assertEquals(resultMap.get(SYMBOL_1), UnsignedByte.valueOf((byte) 128));
        assertEquals(resultMap.get(SYMBOL_2), UnsignedShort.valueOf((short) 128));
        assertEquals(resultMap.get(SYMBOL_3), UnsignedInteger.valueOf(128));
    }
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) Symbol(org.apache.qpid.protonj2.types.Symbol) MessageAnnotations(org.apache.qpid.protonj2.types.messaging.MessageAnnotations) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) InputStream(java.io.InputStream) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)

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