[#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,
This commit is contained in:
Norman Maurer 2014-07-10 06:53:51 +02:00
parent 639db72839
commit 52cb55d388
3 changed files with 17 additions and 24 deletions

View File

@ -255,7 +255,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
int tooLongFrameLength = this.tooLongFrameLength; int tooLongFrameLength = this.tooLongFrameLength;
this.tooLongFrameLength = 0; this.tooLongFrameLength = 0;
if (!failFast) { if (!failFast) {
fail(ctx, tooLongFrameLength); fail(tooLongFrameLength);
} }
return null; return null;
} }
@ -263,7 +263,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
if (minFrameLength > maxFrameLength) { if (minFrameLength > maxFrameLength) {
// Discard read frame. // Discard read frame.
buffer.skipBytes(minFrameLength + minDelimLength); buffer.skipBytes(minFrameLength + minDelimLength);
fail(ctx, minFrameLength); fail(minFrameLength);
return null; return null;
} }
@ -283,7 +283,7 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {
buffer.skipBytes(buffer.readableBytes()); buffer.skipBytes(buffer.readableBytes());
discardingTooLongFrame = true; discardingTooLongFrame = true;
if (failFast) { if (failFast) {
fail(ctx, tooLongFrameLength); fail(tooLongFrameLength);
} }
} }
} else { } 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) { if (frameLength > 0) {
ctx.fireExceptionCaught( throw new TooLongFrameException(
new TooLongFrameException(
"frame length exceeds " + maxFrameLength + "frame length exceeds " + maxFrameLength +
": " + frameLength + " - discarded")); ": " + frameLength + " - discarded");
} else { } else {
ctx.fireExceptionCaught( throw new TooLongFrameException(
new TooLongFrameException(
"frame length exceeds " + maxFrameLength + "frame length exceeds " + maxFrameLength +
" - discarding")); " - discarding");
} }
} }

View File

@ -94,14 +94,11 @@ public class JsonObjectDecoder extends ByteToMessageDecoder {
if (wrtIdx > maxObjectLength) { if (wrtIdx > maxObjectLength) {
// buffer size exceeded maxObjectLength; discarding the complete buffer. // buffer size exceeded maxObjectLength; discarding the complete buffer.
ctx.fireExceptionCaught(
new TooLongFrameException(
"object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded")
);
in.skipBytes(in.readableBytes()); in.skipBytes(in.readableBytes());
reset(); reset();
return; throw new TooLongFrameException(
"object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded");
} }
for (/* use current idx */; idx < wrtIdx; idx++) { for (/* use current idx */; idx < wrtIdx; idx++) {

View File

@ -90,8 +90,8 @@ public class XmlFrameDecoder extends ByteToMessageDecoder {
if (bufferLength > maxFrameLength) { if (bufferLength > maxFrameLength) {
// bufferLength exceeded maxFrameLength; dropping frame // bufferLength exceeded maxFrameLength; dropping frame
fail(ctx, bufferLength);
in.skipBytes(in.readableBytes()); in.skipBytes(in.readableBytes());
fail(bufferLength);
return; 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) { if (frameLength > 0) {
ctx.fireExceptionCaught( throw new TooLongFrameException(
new TooLongFrameException( "frame length exceeds " + maxFrameLength + ": " + frameLength + " - discarded");
"frame length exceeds " + maxFrameLength + ": " + frameLength + " - discarded"));
} else { } else {
ctx.fireExceptionCaught( throw new TooLongFrameException(
new TooLongFrameException( "frame length exceeds " + maxFrameLength + " - discarding");
"frame length exceeds " + maxFrameLength + " - discarding"));
} }
} }