Add DecoderResult.UNFINISHED to represent the case where a decoder generated a message that was not decoded completely / Remove partial failure in DecoderResult which is not very useful but confusing

This commit is contained in:
Trustin Lee 2013-03-12 13:02:50 +09:00
parent acde9a3f8e
commit c660002b4e
5 changed files with 43 additions and 50 deletions

View File

@ -175,7 +175,7 @@ public class HttpObjectAggregator extends MessageToMessageDecoder<HttpObject> {
final boolean last; final boolean last;
if (!chunk.getDecoderResult().isSuccess()) { if (!chunk.getDecoderResult().isSuccess()) {
currentMessage.setDecoderResult( currentMessage.setDecoderResult(
DecoderResult.partialFailure(chunk.getDecoderResult().cause())); DecoderResult.failure(chunk.getDecoderResult().cause()));
last = true; last = true;
} else { } else {
last = chunk instanceof LastHttpContent; last = chunk instanceof LastHttpContent;

View File

@ -447,7 +447,7 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder<HttpObjectDecod
private HttpMessage invalidMessage(Exception cause) { private HttpMessage invalidMessage(Exception cause) {
checkpoint(State.BAD_MESSAGE); checkpoint(State.BAD_MESSAGE);
if (message != null) { if (message != null) {
message.setDecoderResult(DecoderResult.partialFailure(cause)); message.setDecoderResult(DecoderResult.failure(cause));
} else { } else {
message = createInvalidMessage(); message = createInvalidMessage();
message.setDecoderResult(DecoderResult.failure(cause)); message.setDecoderResult(DecoderResult.failure(cause));

View File

@ -37,7 +37,7 @@ public class HttpInvalidMessageTest {
HttpRequest req = (HttpRequest) ch.readInbound(); HttpRequest req = (HttpRequest) ch.readInbound();
DecoderResult dr = req.getDecoderResult(); DecoderResult dr = req.getDecoderResult();
assertFalse(dr.isSuccess()); assertFalse(dr.isSuccess());
assertFalse(dr.isPartialFailure()); assertFalse(dr.isFailure());
ensureInboundTrafficDiscarded(ch); ensureInboundTrafficDiscarded(ch);
} }
@ -51,7 +51,7 @@ public class HttpInvalidMessageTest {
HttpRequest req = (HttpRequest) ch.readInbound(); HttpRequest req = (HttpRequest) ch.readInbound();
DecoderResult dr = req.getDecoderResult(); DecoderResult dr = req.getDecoderResult();
assertFalse(dr.isSuccess()); assertFalse(dr.isSuccess());
assertTrue(dr.isPartialFailure()); assertTrue(dr.isFailure());
assertEquals("Good Value", req.headers().get("Good_Name")); assertEquals("Good Value", req.headers().get("Good_Name"));
assertEquals("/maybe-something", req.getUri()); assertEquals("/maybe-something", req.getUri());
ensureInboundTrafficDiscarded(ch); ensureInboundTrafficDiscarded(ch);
@ -64,7 +64,7 @@ public class HttpInvalidMessageTest {
HttpResponse res = (HttpResponse) ch.readInbound(); HttpResponse res = (HttpResponse) ch.readInbound();
DecoderResult dr = res.getDecoderResult(); DecoderResult dr = res.getDecoderResult();
assertFalse(dr.isSuccess()); assertFalse(dr.isSuccess());
assertFalse(dr.isPartialFailure()); assertFalse(dr.isFailure());
ensureInboundTrafficDiscarded(ch); ensureInboundTrafficDiscarded(ch);
} }
@ -78,7 +78,7 @@ public class HttpInvalidMessageTest {
HttpResponse res = (HttpResponse) ch.readInbound(); HttpResponse res = (HttpResponse) ch.readInbound();
DecoderResult dr = res.getDecoderResult(); DecoderResult dr = res.getDecoderResult();
assertFalse(dr.isSuccess()); assertFalse(dr.isSuccess());
assertTrue(dr.isPartialFailure()); assertTrue(dr.isFailure());
assertEquals("Maybe OK", res.getStatus().reasonPhrase()); assertEquals("Maybe OK", res.getStatus().reasonPhrase());
assertEquals("Good Value", res.headers().get("Good_Name")); assertEquals("Good Value", res.headers().get("Good_Name"));
ensureInboundTrafficDiscarded(ch); ensureInboundTrafficDiscarded(ch);
@ -97,7 +97,7 @@ public class HttpInvalidMessageTest {
HttpContent chunk = (HttpContent) ch.readInbound(); HttpContent chunk = (HttpContent) ch.readInbound();
DecoderResult dr = chunk.getDecoderResult(); DecoderResult dr = chunk.getDecoderResult();
assertFalse(dr.isSuccess()); assertFalse(dr.isSuccess());
assertFalse(dr.isPartialFailure()); assertFalse(dr.isFailure());
ensureInboundTrafficDiscarded(ch); ensureInboundTrafficDiscarded(ch);
} }

View File

@ -15,71 +15,68 @@
*/ */
package io.netty.handler.codec; package io.netty.handler.codec;
import io.netty.util.Signal;
public class DecoderResult { public class DecoderResult {
public static final DecoderResult SUCCESS = new DecoderResult(false, null); protected static final Signal SIGNAL_UNFINISHED = new Signal(DecoderResult.class.getName() + ".UNFINISHED");
protected static final Signal SIGNAL_SUCCESS = new Signal(DecoderResult.class.getName() + ".SUCCESS");
public static final DecoderResult UNFINISHED = new DecoderResult(SIGNAL_UNFINISHED);
public static final DecoderResult SUCCESS = new DecoderResult(SIGNAL_SUCCESS);
public static DecoderResult failure(Throwable cause) { public static DecoderResult failure(Throwable cause) {
if (cause == null) { if (cause == null) {
throw new NullPointerException("cause"); throw new NullPointerException("cause");
} }
return new DecoderResult(false, cause); return new DecoderResult(cause);
} }
public static DecoderResult partialFailure(Throwable cause) { private final Throwable cause;
protected DecoderResult(Throwable cause) {
if (cause == null) { if (cause == null) {
throw new NullPointerException("cause"); throw new NullPointerException("cause");
} }
return new DecoderResult(true, cause);
}
private final boolean partial;
private final Throwable cause;
protected DecoderResult(boolean partial, Throwable cause) {
if (partial && cause == null) {
throw new IllegalArgumentException("successful result cannot be partial.");
}
this.partial = partial;
this.cause = cause; this.cause = cause;
} }
public boolean isFinished() {
return cause != SIGNAL_UNFINISHED;
}
public boolean isSuccess() { public boolean isSuccess() {
return cause == null; return cause == SIGNAL_SUCCESS;
} }
public boolean isFailure() { public boolean isFailure() {
return cause != null; return cause != SIGNAL_SUCCESS && cause != SIGNAL_UNFINISHED;
}
public boolean isCompleteFailure() {
return cause != null && !partial;
}
public boolean isPartialFailure() {
return partial;
} }
public Throwable cause() { public Throwable cause() {
return cause; if (isFailure()) {
return cause;
} else {
return null;
}
} }
@Override @Override
public String toString() { public String toString() {
if (isSuccess()) { if (isFinished()) {
return "success"; if (isSuccess()) {
} return "success";
}
String cause = cause().toString(); String cause = cause().toString();
StringBuilder buf = new StringBuilder(cause.length() + 17); StringBuilder buf = new StringBuilder(cause.length() + 17);
if (isPartialFailure()) { buf.append("failure(");
buf.append("partial_"); buf.append(cause);
} buf.append(')');
buf.append("failure(");
buf.append(cause);
buf.append(')');
return buf.toString(); return buf.toString();
} else {
return "unfinished";
}
} }
} }

View File

@ -132,11 +132,7 @@ public class HttpSnoopServerHandler extends ChannelInboundMessageHandlerAdapter<
return; return;
} }
buf.append(".. WITH A "); buf.append(".. WITH DECODER FAILURE: ");
if (result.isPartialFailure()) {
buf.append("PARTIAL ");
}
buf.append("DECODER FAILURE: ");
buf.append(result.cause()); buf.append(result.cause());
buf.append("\r\n"); buf.append("\r\n");
} }