{
final MessageConverter converter = MessageConverterRegistry.getConverter(serverMessage.getClass(), Message_1_0.class);
message = (Message_1_0) converter.convert(serverMessage, queueEntry.getQueue().getVirtualHost());
}
Transfer transfer = new Transfer();
//TODO
List<ByteBuffer> fragments = message.getFragments();
ByteBuffer payload;
if(fragments.size() == 1)
{
payload = fragments.get(0);
}
else
{
int size = 0;
for(ByteBuffer fragment : fragments)
{
size += fragment.remaining();
}
payload = ByteBuffer.allocate(size);
for(ByteBuffer fragment : fragments)
{
payload.put(fragment.duplicate());
}
payload.flip();
}
if(queueEntry.getDeliveryCount() != 0)
{
payload = payload.duplicate();
ValueHandler valueHandler = new ValueHandler(_typeRegistry);
Header oldHeader = null;
try
{
ByteBuffer encodedBuf = payload.duplicate();
Object value = valueHandler.parse(payload);
if(value instanceof Header)
{
oldHeader = (Header) value;
}
else
{
payload.position(0);
}
}
catch (AmqpErrorException e)
{
//TODO
throw new RuntimeException(e);
}
Header header = new Header();
if(oldHeader != null)
{
header.setDurable(oldHeader.getDurable());
header.setPriority(oldHeader.getPriority());
header.setTtl(oldHeader.getTtl());
}
header.setDeliveryCount(UnsignedInteger.valueOf(queueEntry.getDeliveryCount()));
_sectionEncoder.reset();
_sectionEncoder.encodeObject(header);
Binary encodedHeader = _sectionEncoder.getEncoding();
ByteBuffer oldPayload = payload;
payload = ByteBuffer.allocate(oldPayload.remaining() + encodedHeader.getLength());
payload.put(encodedHeader.getArray(),encodedHeader.getArrayOffset(),encodedHeader.getLength());
payload.put(oldPayload);
payload.flip();
}
transfer.setPayload(payload);
byte[] data = new byte[8];
ByteBuffer.wrap(data).putLong(_deliveryTag++);
final Binary tag = new Binary(data);
transfer.setDeliveryTag(tag);
synchronized(_link.getLock())
{
if(_link.isAttached())
{
if(SenderSettleMode.SETTLED.equals(getEndpoint().getSendingSettlementMode()))
{
transfer.setSettled(true);
}
else
{
UnsettledAction action = _acquires
? new DispositionAction(tag, queueEntry)
: new DoNothingAction(tag, queueEntry);
_link.addUnsettled(tag, action, queueEntry);
}
if(_transactionId != null)
{
TransactionalState state = new TransactionalState();
state.setTxnId(_transactionId);
transfer.setState(state);
}
// TODO - need to deal with failure here
if(_acquires && _transactionId != null)
{
ServerTransaction txn = _link.getTransaction(_transactionId);