/*
* Copyright (c) 2005 Your Corporation. All Rights Reserved.
*/
package org.activemq.transport.stomp;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.IOException;
import java.util.Properties;
import javax.jms.JMSException;
import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ActiveMQTextMessage;
class Send implements Command
{
private final HeaderParser parser = new HeaderParser();
private final StompWireFormat format;
Send(StompWireFormat format)
{
this.format = format;
}
public PacketEnvelope build(String commandLine, DataInput in) throws IOException
{
Properties headers = parser.parse(in);
String destination = headers.getProperty(Stomp.Headers.Send.DESTINATION);
ActiveMQTextMessage text = new ActiveMQTextMessage();
text.setJMSMessageID(StompWireFormat.PACKET_IDS.generateId());
ActiveMQDestination d = DestinationNamer.convert(destination);
text.setJMSDestination(d);
text.setJMSClientID(format.getClientId());
if (format.isInTransaction())
{
text.setTransactionIDString(format.getTransactionId());
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
byte b;
while ((b = in.readByte()) != 0)
{
bytes.write(b);
}
bytes.close();
String body = new String(bytes.toByteArray());
try
{
text.setText(body);
}
catch (JMSException e)
{
throw new RuntimeException("Something is really wrong, we instantiated this thing!");
}
return new PacketEnvelope(text, headers);
}
}