Search in sources :

Example 1 with ErrorData

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);
}
Also used : ChannelContext(io.scalecube.streams.ChannelContext) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) Logger(org.slf4j.Logger) ErrorData(io.scalecube.streams.ErrorData) LoggerFactory(org.slf4j.LoggerFactory) ChannelSupport(io.scalecube.streams.netty.ChannelSupport) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Sharable(io.netty.channel.ChannelHandler.Sharable) Event(io.scalecube.streams.Event) Qualifier(io.scalecube.streams.Qualifier) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Q_ERROR_NAMESPACE(io.scalecube.streams.Qualifier.Q_ERROR_NAMESPACE) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) StreamMessage(io.scalecube.streams.StreamMessage) ChannelContext(io.scalecube.streams.ChannelContext) Qualifier(io.scalecube.streams.Qualifier) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) ErrorData(io.scalecube.streams.ErrorData)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 Sharable (io.netty.channel.ChannelHandler.Sharable)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 ChannelContext (io.scalecube.streams.ChannelContext)1 ErrorData (io.scalecube.streams.ErrorData)1 Event (io.scalecube.streams.Event)1 Qualifier (io.scalecube.streams.Qualifier)1 Q_ERROR_NAMESPACE (io.scalecube.streams.Qualifier.Q_ERROR_NAMESPACE)1 StreamMessage (io.scalecube.streams.StreamMessage)1 ChannelSupport (io.scalecube.streams.netty.ChannelSupport)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1