use of org.apache.qpid.protonj2.types.messaging.Footer 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;
}
use of org.apache.qpid.protonj2.types.messaging.Footer in project qpid-protonj2 by apache.
the class StreamSenderTest method testStreamSenderWritesFooterAfterStreamClosed.
@Test
public void testStreamSenderWritesFooterAfterStreamClosed() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(10).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Sender test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort()).openFuture().get();
StreamSender sender = connection.openStreamSender("test-queue");
StreamSenderMessage message = sender.beginMessage();
final byte[] payload = new byte[] { 0, 1, 2, 3, 4, 5 };
// First frame should include only the bits up to the body
HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
ApplicationPropertiesMatcher apMatcher = new ApplicationPropertiesMatcher(true);
apMatcher.withEntry("ap1", Matchers.equalTo(1));
apMatcher.withEntry("ap2", Matchers.equalTo(2));
apMatcher.withEntry("ap3", Matchers.equalTo(3));
FooterMatcher footerMatcher = new FooterMatcher(false);
footerMatcher.withEntry("f1", Matchers.equalTo(1));
footerMatcher.withEntry("f2", Matchers.equalTo(2));
footerMatcher.withEntry("f3", Matchers.equalTo(3));
EncodedDataMatcher bodyMatcher = new EncodedDataMatcher(payload, true);
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setHeadersMatcher(headerMatcher);
payloadMatcher.setApplicationPropertiesMatcher(apMatcher);
payloadMatcher.setMessageContentMatcher(bodyMatcher);
payloadMatcher.setFootersMatcher(footerMatcher);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withPayload(payloadMatcher).withMore(false).accept();
// Populate all Header values
message.durable(true);
message.priority((byte) 1);
message.timeToLive(65535);
message.firstAcquirer(true);
message.deliveryCount(2);
// Populate message application properties
message.property("ap1", 1);
message.property("ap2", 2);
message.property("ap3", 3);
// Populate message footers
assertFalse(message.hasFooters());
assertFalse(message.hasFooter("f1"));
message.footer("f1", 1);
message.footer("f2", 2);
message.footer("f3", 3);
assertTrue(message.hasFooter("f1"));
assertTrue(message.hasFooters());
OutputStreamOptions bodyOptions = new OutputStreamOptions().completeSendOnClose(true);
OutputStream stream = message.body(bodyOptions);
assertThrows(ClientUnsupportedOperationException.class, () -> message.encode(Collections.emptyMap()));
stream.write(payload);
stream.close();
assertThrows(ClientIllegalStateException.class, () -> message.footer(new Footer(Collections.emptyMap())));
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
assertNotNull(message.tracker());
Wait.assertTrue(() -> message.tracker().settlementFuture().isDone());
assertTrue(message.tracker().settlementFuture().get().settled());
sender.closeAsync().get(10, TimeUnit.SECONDS);
connection.closeAsync().get(10, TimeUnit.SECONDS);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.types.messaging.Footer 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);
}
}
use of org.apache.qpid.protonj2.types.messaging.Footer in project qpid-protonj2 by apache.
the class StreamReceiverTest method testStreamReadOfAllPayloadConsumesTrailingFooterOnClose.
@Test
public void testStreamReadOfAllPayloadConsumesTrailingFooterOnClose() throws Exception {
final byte[] body1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final byte[] body2 = new byte[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
final byte[] payload1 = createEncodedMessage(new Data(body1));
final byte[] payload2 = createEncodedMessage(new Data(body2));
final Footer footers = new Footer(new HashMap<>());
footers.getValue().put(Symbol.valueOf("footer-key"), "test");
final byte[] payload3 = createEncodedMessage(footers);
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().withMaxFrameSize(1000).respond();
peer.expectBegin().withIncomingWindow(1).respond();
peer.expectAttach().ofReceiver().respond();
peer.expectFlow().withIncomingWindow(1).withLinkCredit(10);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withDeliveryTag(new byte[] { 1 }).withMore(true).withMessageFormat(0).withPayload(payload1).queue();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
ConnectionOptions connectionOptions = new ConnectionOptions().maxFrameSize(1000);
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), connectionOptions);
StreamReceiverOptions streamOptions = new StreamReceiverOptions().readBufferSize(2000).autoAccept(false);
StreamReceiver receiver = connection.openStreamReceiver("test-queue", streamOptions);
StreamDelivery delivery = receiver.receive();
assertNotNull(delivery);
StreamReceiverMessage message = delivery.message();
assertNotNull(message);
// Creating the input stream instance should read the first chunk of data from the incoming
// delivery which should result in a new credit being available to expand the session window.
// An additional transfer should be placed into the delivery buffer but not yet read since
// the user hasn't read anything.
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectFlow().withDeliveryCount(0).withIncomingWindow(1).withLinkCredit(10);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withMore(true).withMessageFormat(0).withPayload(payload2).queue();
InputStream bodyStream = message.body();
assertNotNull(bodyStream);
// Once the read of all data completes the session window should be opened and the
// stream should mark the delivery as accepted and settled since we are in auto settle
// mode and there is nothing more to read.
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectFlow().withDeliveryCount(0).withIncomingWindow(1).withLinkCredit(10);
peer.remoteTransfer().withHandle(0).withDeliveryId(0).withMore(false).withMessageFormat(0).withPayload(payload3).queue();
peer.expectFlow().withDeliveryCount(1).withIncomingWindow(1).withLinkCredit(9);
peer.expectDisposition().withFirst(0).withState().accepted().withSettled(true);
byte[] combinedPayloads = new byte[body1.length + body2.length];
bodyStream.read(combinedPayloads);
assertTrue(Arrays.equals(body1, 0, body1.length, combinedPayloads, 0, body1.length));
assertTrue(Arrays.equals(body2, 0, body2.length, combinedPayloads, body1.length, body1.length + body2.length));
bodyStream.close();
Footer footer = message.footer();
assertNotNull(footer);
assertFalse(footer.getValue().isEmpty());
assertTrue(footer.getValue().containsKey(Symbol.valueOf("footer-key")));
assertTrue(message.hasFooters());
assertTrue(message.hasFooter("footer-key"));
message.forEachFooter((key, value) -> {
assertEquals(key, "footer-key");
assertEquals(value, "test");
});
delivery.accept();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
receiver.openFuture().get();
receiver.closeAsync().get();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.types.messaging.Footer in project qpid-protonj2 by apache.
the class ClientMessageTest method testFooterAccessorHandlerNullMapOrEmptyMap.
@Test
public void testFooterAccessorHandlerNullMapOrEmptyMap() {
ClientMessage<String> message = ClientMessage.create();
assertNull(message.footer());
assertNull(message.footer("test"));
assertFalse(message.hasFooter("test"));
assertFalse(message.hasFooters());
message.footer(new Footer(null));
assertNotNull(message.footer());
assertNull(message.footer("test"));
assertFalse(message.hasFooter("test"));
assertFalse(message.hasFooters());
}
Aggregations