use of com.couchbase.client.core.cnc.events.io.SaslAuthenticationFailedEvent in project couchbase-jvm-clients by couchbase.
the class SaslAuthenticationHandler method failConnect.
/**
* Refactored method which is called from many places to fail the connection
* process because of an issue during SASL auth.
*
* <p>Usually errors during auth are very problematic and as a result we cannot continue
* with this channel connect attempt.</p>
*
* @param ctx the channel context.
*/
private void failConnect(final ChannelHandlerContext ctx, final String message, final ByteBuf lastPacket, final Throwable cause, final short status) {
Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
byte[] packetCopy = Bytes.EMPTY_BYTE_ARRAY;
Map<String, Object> serverContext = null;
if (lastPacket != null) {
if (MemcacheProtocol.verifyResponse(lastPacket)) {
// This is a proper response, try to extract server context
Optional<ByteBuf> body = MemcacheProtocol.body(lastPacket);
if (body.isPresent()) {
byte[] content = ByteBufUtil.getBytes(body.get());
try {
serverContext = Mapper.decodeInto(content, new TypeReference<Map<String, Object>>() {
});
} catch (Exception ex) {
// Ignore, no displayable content
}
}
} else {
// This is not a proper memcache response, store the raw packet for debugging purposes
int ridx = lastPacket.readerIndex();
lastPacket.readerIndex(lastPacket.writerIndex());
packetCopy = new byte[lastPacket.readableBytes()];
lastPacket.readBytes(packetCopy);
lastPacket.readerIndex(ridx);
}
}
KeyValueIoErrorContext errorContext = new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, serverContext);
endpointContext.environment().eventBus().publish(new SaslAuthenticationFailedEvent(latency.orElse(Duration.ZERO), errorContext, message, packetCopy));
interceptedConnectPromise.tryFailure(new AuthenticationFailureException(message, errorContext, cause));
ctx.pipeline().remove(this);
}
Aggregations