From 52cb55d3887034df778575799fdd00f174495809 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 10 Jul 2014 06:53:51 +0200 Subject: [PATCH] [#2643] Throw TooLongFrameException instead of using fireExceptionCaught Motivation: It's not always the case that there is another handler in the pipeline that will intercept the exceptionCaught event because sometimes users just sub-class. In this case the exception will just hit the end of the pipeline. Modification: Throw the TooLongFrameException so that sub-classes can handle it in the exceptionCaught(...) method directly. Result: Sub-classes can correctly handle the exception, --- .../codec/DelimiterBasedFrameDecoder.java | 18 ++++++++---------- .../handler/codec/json/JsonObjectDecoder.java | 9 +++------ .../handler/codec/xml/XmlFrameDecoder.java | 14 ++++++-------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java b/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java index 2dbd897ad6..0cb276bca5 100644 --- a/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java @@ -255,7 +255,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder { int tooLongFrameLength = this.tooLongFrameLength; this.tooLongFrameLength = 0; if (!failFast) { - fail(ctx, tooLongFrameLength); + fail(tooLongFrameLength); } return null; } @@ -263,7 +263,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder { if (minFrameLength > maxFrameLength) { // Discard read frame. buffer.skipBytes(minFrameLength + minDelimLength); - fail(ctx, minFrameLength); + fail(minFrameLength); return null; } @@ -283,7 +283,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder { buffer.skipBytes(buffer.readableBytes()); discardingTooLongFrame = true; if (failFast) { - fail(ctx, tooLongFrameLength); + fail(tooLongFrameLength); } } } else { @@ -295,17 +295,15 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder { } } - private void fail(ChannelHandlerContext ctx, long frameLength) { + private void fail(long frameLength) { if (frameLength > 0) { - ctx.fireExceptionCaught( - new TooLongFrameException( + throw new TooLongFrameException( "frame length exceeds " + maxFrameLength + - ": " + frameLength + " - discarded")); + ": " + frameLength + " - discarded"); } else { - ctx.fireExceptionCaught( - new TooLongFrameException( + throw new TooLongFrameException( "frame length exceeds " + maxFrameLength + - " - discarding")); + " - discarding"); } } diff --git a/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java b/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java index 616e70b3ab..32d02f6d75 100644 --- a/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java @@ -94,14 +94,11 @@ public class JsonObjectDecoder extends ByteToMessageDecoder { if (wrtIdx > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. - ctx.fireExceptionCaught( - new TooLongFrameException( - "object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded") - ); - in.skipBytes(in.readableBytes()); reset(); - return; + throw new TooLongFrameException( + "object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded"); + } for (/* use current idx */; idx < wrtIdx; idx++) { diff --git a/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java b/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java index ae499e30a0..c4f7097435 100644 --- a/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java @@ -90,8 +90,8 @@ public class XmlFrameDecoder extends ByteToMessageDecoder { if (bufferLength > maxFrameLength) { // bufferLength exceeded maxFrameLength; dropping frame - fail(ctx, bufferLength); in.skipBytes(in.readableBytes()); + fail(bufferLength); return; } @@ -178,15 +178,13 @@ public class XmlFrameDecoder extends ByteToMessageDecoder { } } - private void fail(ChannelHandlerContext ctx, long frameLength) { + private void fail(long frameLength) { if (frameLength > 0) { - ctx.fireExceptionCaught( - new TooLongFrameException( - "frame length exceeds " + maxFrameLength + ": " + frameLength + " - discarded")); + throw new TooLongFrameException( + "frame length exceeds " + maxFrameLength + ": " + frameLength + " - discarded"); } else { - ctx.fireExceptionCaught( - new TooLongFrameException( - "frame length exceeds " + maxFrameLength + " - discarding")); + throw new TooLongFrameException( + "frame length exceeds " + maxFrameLength + " - discarding"); } }