use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection in project qpid-broker-j by apache.
the class TxnCoordinatorReceivingLinkEndpoint method receiveDelivery.
@Override
protected Error receiveDelivery(Delivery delivery) {
// Only interested in the amqp-value section that holds the message to the coordinator
try (QpidByteBuffer payload = delivery.getPayload()) {
List<EncodingRetainingSection<?>> sections = getSectionDecoder().parseAll(payload);
boolean amqpValueSectionFound = false;
for (EncodingRetainingSection section : sections) {
try {
if (section instanceof AmqpValueSection) {
if (amqpValueSectionFound) {
throw new ConnectionScopedRuntimeException("Received more than one AmqpValue sections");
}
amqpValueSectionFound = true;
Object command = section.getValue();
Session_1_0 session = getSession();
AMQPConnection_1_0<?> connection = session.getConnection();
connection.receivedComplete();
if (command instanceof Declare) {
final IdentifiedTransaction txn = connection.createIdentifiedTransaction();
_createdTransactions.put(txn.getId(), txn.getServerTransaction());
long notificationRepeatPeriod = getSession().getContextValue(Long.class, Session.TRANSACTION_TIMEOUT_NOTIFICATION_REPEAT_PERIOD);
connection.registerTransactionTickers(txn.getServerTransaction(), this::doTimeoutAction, notificationRepeatPeriod);
Declared state = new Declared();
state.setTxnId(Session_1_0.integerToTransactionId(txn.getId()));
updateDisposition(delivery.getDeliveryTag(), state, true);
} else if (command instanceof Discharge) {
Discharge discharge = (Discharge) command;
Error error = discharge(discharge.getTxnId(), Boolean.TRUE.equals(discharge.getFail()));
final DeliveryState outcome;
if (error == null) {
outcome = new Accepted();
} else if (Arrays.asList(getSource().getOutcomes()).contains(Rejected.REJECTED_SYMBOL)) {
final Rejected rejected = new Rejected();
rejected.setError(error);
outcome = rejected;
error = null;
} else {
outcome = null;
}
if (error == null) {
updateDisposition(delivery.getDeliveryTag(), outcome, true);
}
return error;
} else {
throw new ConnectionScopedRuntimeException(String.format("Received unknown command '%s'", command.getClass().getSimpleName()));
}
}
} finally {
section.dispose();
}
}
if (!amqpValueSectionFound) {
throw new ConnectionScopedRuntimeException("Received no AmqpValue section");
}
} catch (AmqpErrorException e) {
return e.getError();
}
return null;
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection in project qpid-broker-j by apache.
the class MessageConverter_to_1_0 method createMessageAnnotation.
public static MessageAnnotations createMessageAnnotation(final EncodingRetainingSection<?> bodySection, final String contentMimeType) {
MessageAnnotations messageAnnotations = null;
final Symbol key = Symbol.valueOf("x-opt-jms-msg-type");
if (contentMimeType != null) {
if (TEXT_CONTENT_TYPES.matcher(contentMimeType).matches()) {
messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, TEXT_MESSAGE.getType()));
} else if (BYTES_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, BYTES_MESSAGE.getType()));
} else if (MAP_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
if (isSectionValidForJmsMap(bodySection)) {
messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, MAP_MESSAGE.getType()));
}
} else if (LIST_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
if (isSectionValidForJmsList(bodySection)) {
messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, STREAM_MESSAGE.getType()));
}
} else if (OBJECT_MESSAGE_CONTENT_TYPES.matcher(contentMimeType).matches()) {
messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, OBJECT_MESSAGE.getType()));
}
} else if (bodySection instanceof AmqpValueSection && bodySection.getValue() == null) {
messageAnnotations = new MessageAnnotations(Collections.singletonMap(key, MESSAGE.getType()));
}
return messageAnnotations;
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection in project qpid-broker-j by apache.
the class Interaction method transferPayload.
private void transferPayload(final Transfer transfer, final Object payload) {
AmqpValue amqpValue = new AmqpValue(payload);
final AmqpValueSection section = amqpValue.createEncodingRetainingSection();
try (QpidByteBuffer encodedForm = section.getEncodedForm()) {
transfer.setPayload(encodedForm);
} finally {
section.dispose();
}
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection in project qpid-broker-j by apache.
the class DecodeErrorTest method buildInvalidMessage.
private QpidByteBuffer buildInvalidMessage() {
final List<QpidByteBuffer> payloads = new ArrayList<>();
try {
final Header header = new Header();
header.setTtl(UnsignedInteger.valueOf(10000L));
final HeaderSection headerSection = header.createEncodingRetainingSection();
try {
payloads.add(headerSection.getEncodedForm());
} finally {
headerSection.dispose();
}
final StringWriter stringWriter = new StringWriter("string in between message sections");
final QpidByteBuffer encodedString = QpidByteBuffer.allocate(stringWriter.getEncodedSize());
stringWriter.writeToBuffer(encodedString);
encodedString.flip();
payloads.add(encodedString);
final Map<Symbol, Object> annotationMap = Collections.singletonMap(Symbol.valueOf("foo"), "bar");
final DeliveryAnnotations annotations = new DeliveryAnnotations(annotationMap);
final DeliveryAnnotationsSection deliveryAnnotationsSection = annotations.createEncodingRetainingSection();
try {
payloads.add(deliveryAnnotationsSection.getEncodedForm());
} finally {
deliveryAnnotationsSection.dispose();
}
final AmqpValueSection payload = new AmqpValue(getTestName()).createEncodingRetainingSection();
try {
payloads.add(payload.getEncodedForm());
} finally {
payload.dispose();
}
return QpidByteBuffer.concatenate(payloads);
} finally {
payloads.forEach(QpidByteBuffer::dispose);
}
}
use of org.apache.qpid.server.protocol.v1_0.type.messaging.AmqpValueSection in project qpid-broker-j by apache.
the class MessageConverter_from_1_0 method convertBodyToObject.
static Object convertBodyToObject(final Message_1_0 serverMessage) {
final SectionDecoderImpl sectionDecoder = new SectionDecoderImpl(MessageConverter_v1_0_to_Internal.TYPE_REGISTRY.getSectionDecoderRegistry());
Object bodyObject = null;
List<EncodingRetainingSection<?>> sections = null;
try {
try (QpidByteBuffer allData = serverMessage.getContent()) {
sections = sectionDecoder.parseAll(allData);
}
final int size = sections == null ? 0 : sections.size();
final List<EncodingRetainingSection<?>> bodySections = new ArrayList<>(size);
final ListIterator<EncodingRetainingSection<?>> iterator = sections == null ? Collections.emptyListIterator() : sections.listIterator();
EncodingRetainingSection<?> previousSection = null;
while (iterator.hasNext()) {
final EncodingRetainingSection<?> section = iterator.next();
if (section instanceof AmqpValueSection || section instanceof DataSection || section instanceof AmqpSequenceSection) {
if (previousSection != null && (previousSection.getClass() != section.getClass() || section instanceof AmqpValueSection)) {
throw new MessageConversionException("Message is badly formed and has multiple body section which are not all Data or not all AmqpSequence");
} else {
previousSection = section;
}
bodySections.add(section);
}
}
// In 1.0 of the spec, it is illegal to have message with no body but AMQP-127 asks to have that restriction lifted
if (!bodySections.isEmpty()) {
final EncodingRetainingSection<?> firstBodySection = bodySections.get(0);
if (firstBodySection instanceof AmqpValueSection) {
bodyObject = convertValue(firstBodySection.getValue());
} else if (firstBodySection instanceof DataSection) {
int totalSize = 0;
for (EncodingRetainingSection<?> section : bodySections) {
totalSize += ((DataSection) section).getValue().getArray().length;
}
final byte[] bodyData = new byte[totalSize];
final ByteBuffer buf = ByteBuffer.wrap(bodyData);
for (EncodingRetainingSection<?> section : bodySections) {
buf.put(((DataSection) section).getValue().asByteBuffer());
}
bodyObject = bodyData;
} else {
final ArrayList<Object> totalSequence = new ArrayList<>();
for (EncodingRetainingSection<?> section : bodySections) {
totalSequence.addAll(((AmqpSequenceSection) section).getValue());
}
bodyObject = convertValue(totalSequence);
}
}
} catch (AmqpErrorException e) {
throw new ConnectionScopedRuntimeException(e);
} finally {
if (sections != null) {
sections.forEach(EncodingRetaining::dispose);
}
}
return bodyObject;
}
Aggregations