[#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 deda8f15a2
commit 17280116c4
3 changed files with 17 additions and 24 deletions

View File

@ -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");
}
}

View File

@ -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++) {

View File

@ -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");
}
}