}
MessageEvent e = (MessageEvent) evt;
Object originalMessage = e.getMessage();
Object decodedMessage = decode(ctx, ctx.getChannel(), originalMessage);
// Generate SASL response to server using Channel-local SASL client.
SaslNettyClient saslNettyClient = NettyClient.SASL.get(ctx.getChannel());
if (saslNettyClient == null) {
throw new Exception("handleUpstream: saslNettyClient was unexpectedly " +
"null for channel: " + ctx.getChannel());
}
if (decodedMessage.getClass() == SaslCompleteRequest.class) {
if (LOG.isDebugEnabled()) {
LOG.debug("handleUpstream: Server has sent us the SaslComplete " +
"message. Allowing normal work to proceed.");
}
synchronized (saslNettyClient.getAuthenticated()) {
saslNettyClient.getAuthenticated().notify();
}
if (!saslNettyClient.isComplete()) {
LOG.error("handleUpstream: Server returned a Sasl-complete message, " +
"but as far as we can tell, we are not authenticated yet.");
throw new Exception("handleUpstream: Server returned a " +
"Sasl-complete message, but as far as " +
"we can tell, we are not authenticated yet.");
}
// Remove SaslClientHandler and replace LengthFieldBasedFrameDecoder
// from client pipeline.
ctx.getPipeline().remove(this);
ctx.getPipeline().replace("length-field-based-frame-decoder",
"fixed-length-frame-decoder",
new FixedLengthFrameDecoder(RequestServerHandler.RESPONSE_BYTES));
return;
}
SaslTokenMessageRequest serverToken =
(SaslTokenMessageRequest) decodedMessage;
if (LOG.isDebugEnabled()) {
LOG.debug("handleUpstream: Responding to server's token of length: " +
serverToken.getSaslToken().length);
}
// Generate SASL response (but we only actually send the response if it's
// non-null.
byte[] responseToServer = saslNettyClient.saslResponse(serverToken);
if (responseToServer == null) {
// If we generate a null response, then authentication has completed (if
// not, warn), and return without sending a response back to the server.
if (LOG.isDebugEnabled()) {
LOG.debug("handleUpstream: Response to server is null: " +
"authentication should now be complete.");
}
if (!saslNettyClient.isComplete()) {
LOG.warn("handleUpstream: Generated a null response, " +
"but authentication is not complete.");
}
return;
} else {