for (Message message : messages)
((MessageImpl)message).decRef();
}
}
Message pollReply=null;
// Do we need to wait for messages
if (transport!=null)
{
pollReply=transport.getPollReply();
if (pollReply!=null)
{
if (_bayeux.isLogDebug())
_bayeux.logDebug("doPost: transport is polling");
long timeout=client.getTimeout();
if (timeout==0)
timeout=_bayeux.getTimeout();
Continuation continuation=ContinuationSupport.getContinuation(request,client);
// Get messages or wait
synchronized (client)
{
if (!client.hasMessages() && !continuation.isPending()&& received<=1)
{
// save state and suspend
((ContinuationClient)client).setContinuation(continuation);
request.setAttribute(CLIENT_ATTR,client);
request.setAttribute(TRANSPORT_ATTR,transport);
continuation.suspend(timeout);
}
if (!continuation.isPending())
client.access();
continuation.reset();
}
((ContinuationClient)client).setContinuation(null);
transport.setPollReply(null);
pollReply=_bayeux.extendSendMeta(client,pollReply);
}
else if (client!=null)
{
client.access();
}
}
// Send any messages.
if (client!=null)
{
synchronized(client)
{
client.doDeliverListeners();
ArrayQueue<Message> messages= (ArrayQueue)client.getQueue();
int size=messages.size();
boolean flushed=false;
boolean shouldSendMessages= !( pollReply == null && client.isDeliverViaMetaConnectOnly() );
try
{
if (pollReply!=null)
{
// can we bypass response generation?
if (_refsThreshold>0 && size==1 && transport instanceof JSONTransport)
{
MessageImpl message = (MessageImpl)messages.peek();
// is there a response already prepared
ByteBuffer buffer = message.getBuffer();
if (buffer!=null)
{
request.setAttribute("org.mortbay.jetty.ResponseBuffer",buffer);
((MessageImpl)message).decRef();
pollReply=null;
flushed=true;
}
else if (message.getRefs()>=_refsThreshold)
{
byte[] contentBytes = ("["+message.getJSON()+",{\""+Bayeux.SUCCESSFUL_FIELD+"\":true,\""+
Bayeux.CHANNEL_FIELD+"\":\""+Bayeux.META_CONNECT+"\"}]")
.getBytes(StringUtil.__UTF8);
int contentLength = contentBytes.length;
String headerString = "HTTP/1.1 200 OK\r\n"+
"Content-Type: text/json; charset=utf-8\r\n" +
"Content-Length: " + contentLength + "\r\n" +
"\r\n";
byte[] headerBytes = headerString.getBytes(StringUtil.__UTF8);
buffer = ByteBuffer.allocateDirect(headerBytes.length+contentLength);
buffer.put(headerBytes);
buffer.put(contentBytes);
buffer.flip();
message.setBuffer(buffer);
request.setAttribute("org.mortbay.jetty.ResponseBuffer",buffer);
((MessageImpl)message).decRef();
pollReply=null;
flushed=true;
}
}
}
if (!flushed)
{
if ( shouldSendMessages )
{
Message message = null;
for (int i = 0;i<size;i++)
{
message=messages.getUnsafe(i);
message=_bayeux.extendSendClient(null, client, message);
if (message!=null)