}
// Setup for specific user agents.
byte[] kickStartBytesToStream = null;
String userAgentValue = req.getHeader(UserAgentManager.USER_AGENT_HEADER_NAME);
UserAgentSettings agentSettings = userAgentManager.match(userAgentValue);
if (agentSettings != null)
{
synchronized (session)
{
session.maxConnectionsPerSession = agentSettings.getMaxStreamingConnectionsPerSession();
}
int kickStartBytes = agentSettings.getKickstartBytes();
if (kickStartBytes > 0)
{
// Determine the minimum number of actual bytes that need to be sent to
// kickstart, taking into account transfer-encoding overhead.
try
{
int chunkLengthHeaderSize = Integer.toHexString(kickStartBytes).getBytes("ASCII").length;
int chunkOverhead = chunkLengthHeaderSize + 4; // 4 for the 2 wrapping CRLF tokens.
int minimumKickstartBytes = kickStartBytes - chunkOverhead;
kickStartBytesToStream = new byte[(minimumKickstartBytes > 0) ? minimumKickstartBytes :
kickStartBytes];
}
catch (UnsupportedEncodingException ignore)
{
kickStartBytesToStream = new byte[kickStartBytes];
}
Arrays.fill(kickStartBytesToStream, NULL_BYTE);
}
}
// Now, check with the session before granting the streaming connection.
synchronized(session)
{
++session.streamingConnectionsCount;
if (session.streamingConnectionsCount == session.maxConnectionsPerSession)
{
thisThreadCanStream = true; // This thread got the last spot in the session.
session.canStream = false;
}
else if (session.streamingConnectionsCount > session.maxConnectionsPerSession)
{
thisThreadCanStream = false; // This thread was beaten out for the last spot.
--session.streamingConnectionsCount;
synchronized(lock)
{
// Decrement the endpoint count because we're not going to grant the streaming right to the client.
--streamingClientsCount;
}
}
else
{
// We haven't hit the limit yet, allow this thread to stream.
thisThreadCanStream = true;
}
}
// If the thread cannot wait due to session streaming connection
// limit, inform the client and return.
if (!thisThreadCanStream)
{
if (Log.isInfo())
log.info("Endpoint with id '" + getId() + "' cannot grant streaming connection to FlexClient with id '"
+ flexClient.getId() + "' because " + UserAgentManager.MAX_STREAMING_CONNECTIONS_PER_SESSION + " limit of '" + session.maxConnectionsPerSession
+ ((agentSettings != null) ? "' for user-agent '" + agentSettings.getMatchOn() + "'" : "") + " has been reached." );
try
{
// Return an HTTP status code 400.
res.sendError(HttpServletResponse.SC_BAD_REQUEST);
}