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