Search in sources :

Example 31 with DecoderException

use of io.netty.handler.codec.DecoderException in project camel by apache.

the class HL7MLLPNettyDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    ByteBuf buf = (ByteBuf) super.decode(ctx, buffer);
    if (buf != null) {
        try {
            int pos = buf.bytesBefore((byte) config.getStartByte());
            if (pos >= 0) {
                ByteBuf msg = buf.readerIndex(pos + 1).slice();
                LOG.debug("Message ends with length {}", msg.readableBytes());
                return config.isProduceString() ? asString(msg) : asByteArray(msg);
            } else {
                throw new DecoderException("Did not find start byte " + (int) config.getStartByte());
            }
        } finally {
            // We need to release the buf here to avoid the memory leak
            buf.release();
        }
    }
    // Message not complete yet - return null to be called again
    LOG.debug("No complete messages yet at position {}", buffer.readableBytes());
    return null;
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) ByteBuf(io.netty.buffer.ByteBuf)

Example 32 with DecoderException

use of io.netty.handler.codec.DecoderException in project bookkeeper by apache.

the class PerChannelBookieClient method exceptionCaught.

/**
 * Called by netty when an exception happens in one of the netty threads
 * (mostly due to what we do in the netty threads).
 */
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    exceptionCounter.inc();
    if (cause instanceof CorruptedFrameException || cause instanceof TooLongFrameException) {
        LOG.error("Corrupted frame received from bookie: {}", ctx.channel().remoteAddress());
        ctx.close();
        return;
    }
    if (cause instanceof AuthHandler.AuthenticationException) {
        LOG.error("Error authenticating connection", cause);
        errorOutOutstandingEntries(BKException.Code.UnauthorizedAccessException);
        Channel c = ctx.channel();
        if (c != null) {
            closeChannel(c);
        }
        return;
    }
    if (cause instanceof DecoderException && cause.getCause() instanceof SSLHandshakeException) {
        LOG.error("TLS handshake failed", cause);
        errorOutPendingOps(BKException.Code.SecurityException);
        Channel c = ctx.channel();
        if (c != null) {
            closeChannel(c);
        }
    }
    if (cause instanceof IOException) {
        LOG.warn("Exception caught on:{} cause:", ctx.channel(), cause);
        ctx.close();
        return;
    }
    synchronized (this) {
        if (state == ConnectionState.CLOSED) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Unexpected exception caught by bookie client channel handler, " + "but the client is closed, so it isn't important", cause);
            }
        } else {
            LOG.error("Unexpected exception caught by bookie client channel handler", cause);
        }
    }
    // Since we are a library, cant terminate App here, can we?
    ctx.close();
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) CorruptedFrameException(io.netty.handler.codec.CorruptedFrameException) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) IOException(java.io.IOException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 33 with DecoderException

use of io.netty.handler.codec.DecoderException in project zuul by Netflix.

the class Http2StreamErrorHandler method exceptionCaught.

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (cause instanceof StreamException) {
        StreamException streamEx = (StreamException) cause;
        ctx.writeAndFlush(new DefaultHttp2ResetFrame(streamEx.error()));
    } else if (cause instanceof DecoderException) {
        ctx.writeAndFlush(new DefaultHttp2ResetFrame(Http2Error.PROTOCOL_ERROR));
    } else {
        super.exceptionCaught(ctx, cause);
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) DefaultHttp2ResetFrame(io.netty.handler.codec.http2.DefaultHttp2ResetFrame)

Example 34 with DecoderException

use of io.netty.handler.codec.DecoderException in project LanternServer by LanternPowered.

the class MessageCodecHandler method processMessage.

private void processMessage(Message message, List<Object> output, Protocol protocol, ProtocolState state, CodecContext context) {
    if (message == NullMessage.INSTANCE) {
        return;
    }
    if (message instanceof BulkMessage) {
        ((BulkMessage) message).getMessages().forEach(message1 -> processMessage(message1, output, protocol, state, context));
        return;
    }
    final MessageRegistration messageRegistration = (MessageRegistration) protocol.inbound().findByMessageType(message.getClass()).orElseThrow(() -> new DecoderException("The returned message type is not attached to the used protocol state (" + state.toString() + ")!"));
    final List<Processor> processors = messageRegistration.getProcessors();
    // Only process if there are processors found
    if (!processors.isEmpty()) {
        for (Processor processor : processors) {
            // The processor should handle the output messages
            processor.process(context, message, output);
        }
    } else {
        final Optional<Handler> optHandler = messageRegistration.getHandler();
        if (optHandler.isPresent()) {
            // Add the message to the output
            output.add(new HandlerMessage(message, optHandler.get()));
        } else {
            output.add(message);
        }
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) BulkMessage(org.lanternpowered.server.network.message.BulkMessage) MessageRegistration(org.lanternpowered.server.network.message.MessageRegistration) Processor(org.lanternpowered.server.network.message.processor.Processor) Handler(org.lanternpowered.server.network.message.handler.Handler) HandlerMessage(org.lanternpowered.server.network.message.HandlerMessage)

Example 35 with DecoderException

use of io.netty.handler.codec.DecoderException in project LanternServer by LanternPowered.

the class MessageCompressionHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    int index = msg.readerIndex();
    int uncompressedSize = readVarInt(msg);
    if (uncompressedSize == 0) {
        // Message is uncompressed
        int length = msg.readableBytes();
        if (length >= this.compressionThreshold) {
            // Invalid
            throw new DecoderException("Received uncompressed message of size " + length + " greater than threshold " + this.compressionThreshold);
        }
        ByteBuf buf = ctx.alloc().buffer(length);
        msg.readBytes(buf, length);
        out.add(buf);
    } else {
        // Message is compressed
        byte[] sourceData = new byte[msg.readableBytes()];
        msg.readBytes(sourceData);
        this.inflater.setInput(sourceData);
        byte[] destData = new byte[uncompressedSize];
        int resultLength = this.inflater.inflate(destData);
        this.inflater.reset();
        if (resultLength == 0) {
            // Might be a leftover from before compression was enabled (no compression header)
            // UncompressedSize is likely to be < threshold
            msg.readerIndex(index);
            msg.retain();
            out.add(msg);
        } else if (resultLength != uncompressedSize) {
            throw new DecoderException("Received compressed message claiming to be of size " + uncompressedSize + " but actually " + resultLength);
        } else {
            out.add(Unpooled.wrappedBuffer(destData));
        }
    }
}
Also used : DecoderException(io.netty.handler.codec.DecoderException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

DecoderException (io.netty.handler.codec.DecoderException)46 ByteBuf (io.netty.buffer.ByteBuf)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 ClosedChannelException (java.nio.channels.ClosedChannelException)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 Test (org.junit.jupiter.api.Test)3 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)2 DomainNameMappingBuilder (io.netty.util.DomainNameMappingBuilder)2 IOException (java.io.IOException)2 SSLException (javax.net.ssl.SSLException)2 Test (org.junit.Test)2 Vector3d (com.flowpowered.math.vector.Vector3d)1 Vector3i (com.flowpowered.math.vector.Vector3i)1 ApiError (com.nike.backstopper.apierror.ApiError)1 ApiErrorWithMetadata (com.nike.backstopper.apierror.ApiErrorWithMetadata)1 CircuitBreakerException (com.nike.fastbreak.exception.CircuitBreakerException)1 Pair (com.nike.internal.util.Pair)1