use of io.scalecube.streams.ErrorData in project scalecube by scalecube.
the class GatewayHttpMessageHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ChannelContext channelContext = ChannelSupport.getChannelContextIfExist(ctx);
if (channelContext == null) {
LOGGER.error("Can't find channel context on channel: {}", ctx.channel());
ctx.channel().close();
return;
}
channelContext.listenWrite().map(Event::getMessageOrThrow).subscribe(message -> {
Qualifier qualifier = Qualifier.fromString(message.qualifier());
FullHttpResponse response;
if (!Q_ERROR_NAMESPACE.equalsIgnoreCase(qualifier.getNamespace())) {
response = message.containsData() ? HttpCodecUtil.okResponse((ByteBuf) message.data()) : HttpCodecUtil.emptyResponse();
} else {
if (message.dataOfType(ErrorData.class)) {
// => ErrorData
response = HttpCodecUtil.errorResponse(qualifier, (ErrorData) message.data());
} else if (message.dataOfType(ByteBuf.class)) {
// => ByteBuf assumed
response = HttpCodecUtil.errorResponse(qualifier, (ByteBuf) message.data());
} else {
response = HttpCodecUtil.emptyErrorResponse(qualifier);
}
}
// Hint: at this point we could add logic on writing headers to the response
ctx.writeAndFlush(response).addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
channelContext.postWriteError(message, future.cause());
} else {
channelContext.postWriteSuccess(message);
}
});
}, throwable -> {
LOGGER.error("Fatal exception occured on channel context: {}, cause: {}", channelContext.getId(), throwable);
ctx.channel().close();
});
super.channelActive(ctx);
}
Aggregations