if (val == null) {
sendMessage(ctx, getFailedNegotiation("illegal handshake"));
return;
}
final EJObject ejObject = val.isObject();
if (ejObject == null) {
return;
}
final EJValue ejValue = ejObject.get(MessageParts.CommandType.name());
if (ejValue.isNull()) {
sendMessage(ctx, getFailedNegotiation("illegal handshake"));
}
final String commandType = ejValue.isString().stringValue();
// this client apparently wants to connect.
if (BusCommand.Associate.name().equals(commandType)) {
final String sessionKey = ejObject.get(MessageParts.ConnectionSessionKey.name()).isString().stringValue();
// has this client already attempted a connection, and is in a wait verify state
if (sessionKey != null && (session = svc.getBus().getSessionBySessionId(sessionKey)) != null) {
final LocalContext localContext = LocalContext.get(session);
if (localContext.hasAttribute(SESSION_ATTR_WS_STATUS) &&
WEBSOCKET_ACTIVE.equals(localContext.getAttribute(String.class, SESSION_ATTR_WS_STATUS))) {
final MessageQueue queueBySession = svc.getBus().getQueueBySession(sessionKey);
queueBySession.setDeliveryHandler(DirectDeliveryHandler.createFor(new NettyQueueChannel(ctx.channel())));
// open the channel
activeChannels.put(ctx.channel(), session);
ctx.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture channelFuture) throws Exception {
activeChannels.remove(ctx.channel());
queueBySession.setDeliveryHandlerToDefault();
}
});
// set the session queue into direct channel mode.
localContext.removeAttribute(SESSION_ATTR_WS_STATUS);
// service.schedule(new Runnable() {
// @Override
// public void run() {
// ctx.getChannel().close();
// }
// }, 5, TimeUnit.SECONDS);
return;
}
// check the activation key matches.
final EJString activationKey = ejObject.get(MessageParts.WebSocketToken.name()).isString();
if (activationKey == null || !WebSocketTokenManager.verifyOneTimeToken(session, activationKey.stringValue())) {
// nope. go away!
sendMessage(ctx, getFailedNegotiation("bad negotiation key"));
}
else {