if (text.length() == 0) return;
@SuppressWarnings("unchecked") final EJObject val = JSONDecoder.decode(text).isObject();
final LocalContext localSessionContext = LocalContext.get(session);
QueueSession cometSession = localSessionContext.getAttribute(QueueSession.class, WEBSOCKET_SESSION_ALIAS);
// this is not an active channel.
if (cometSession == null) {
final String commandType = val.get(MessageParts.CommandType.name()).isString().stringValue();
// this client apparently wants to connect.
if (BusCommand.Associate.name().equals(commandType)) {
final String sessionKey = val.get(MessageParts.ConnectionSessionKey.name()).isString().stringValue();
// has this client already attempted a connection, and is in a wait verify state
if (sessionKey != null && (cometSession = service.getBus().getSessionBySessionId(sessionKey)) != null) {
final LocalContext localCometSession = LocalContext.get(cometSession);
if (localCometSession.hasAttribute(WebSocketServerHandler.SESSION_ATTR_WS_STATUS) &&
WebSocketServerHandler.WEBSOCKET_ACTIVE.equals(localCometSession.getAttribute(String.class, WebSocketServerHandler.SESSION_ATTR_WS_STATUS))) {
// set the session queue into direct channel mode.
final MessageQueue queue = service.getBus().getQueue(cometSession);
queue.setDeliveryHandler(DirectDeliveryHandler.createFor(new SimpleEventChannelWrapped(socket)));
localSessionContext.setAttribute(WEBSOCKET_SESSION_ALIAS, cometSession);
cometSession.removeAttribute(WebSocketServerHandler.SESSION_ATTR_WS_STATUS);
return;
}
// check the activation key matches.
final EJString activationKey = val.get(MessageParts.WebSocketToken.name()).isString();
if (activationKey == null || !WebSocketTokenManager.verifyOneTimeToken(cometSession, activationKey.stringValue())) {
// nope. go away!
sendMessage(new SimpleEventChannelWrapped(socket), getFailedNegotiation("bad negotiation key"));
}
else {
// the key matches. now we send the reverse challenge to prove this client is actually
// already talking to the bus over the COMET channel.
final String reverseToken = WebSocketTokenManager.getNewOneTimeToken(cometSession);
localCometSession.setAttribute(WebSocketServerHandler.SESSION_ATTR_WS_STATUS, WebSocketServerHandler.WEBSOCKET_AWAIT_ACTIVATION);
// send the challenge.
sendMessage(new SimpleEventChannelWrapped(socket), getReverseChallenge(reverseToken));
return;
}