io.tarantool.driver.handlers.TarantoolAuthenticationResponseHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cartridge-driver Show documentation
Show all versions of cartridge-driver Show documentation
Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework
package io.tarantool.driver.handlers;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.tarantool.driver.exceptions.TarantoolServerException;
import io.tarantool.driver.protocol.TarantoolErrorResult;
import io.tarantool.driver.protocol.TarantoolResponse;
import java.util.concurrent.CompletableFuture;
/**
* Basic Tarantool server authentication response handler. Completes connection or makes exception out of Tarantool
* server error
*
* @author Alexey Kuzin
*/
public class TarantoolAuthenticationResponseHandler extends SimpleChannelInboundHandler {
private final CompletableFuture connectionFuture;
public TarantoolAuthenticationResponseHandler(CompletableFuture connectionFuture) {
super();
this.connectionFuture = connectionFuture;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, TarantoolResponse tarantoolResponse) throws Exception {
if (!connectionFuture.isDone()) {
switch (tarantoolResponse.getResponseType()) {
case IPROTO_NOT_OK:
TarantoolErrorResult errorResult = new TarantoolErrorResult(tarantoolResponse.getSyncId(),
tarantoolResponse.getResponseCode(), tarantoolResponse.getBody().getData());
connectionFuture.completeExceptionally(
new TarantoolServerException(errorResult.getErrorCode(), errorResult.getErrorMessage()));
break;
case IPROTO_OK:
connectionFuture.complete(ctx.channel());
}
}
ctx.pipeline().remove(this); // authorize once per channel
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
connectionFuture.completeExceptionally(cause);
}
}