From 080895b1b57d99a4142cdf228662f13f76a1002f Mon Sep 17 00:00:00 2001 From: Matheus Buschermoehle Date: Wed, 27 Nov 2024 22:58:24 -0300 Subject: [PATCH] refactor: add centralized validation inside Op enum --- cli/src/main/java/hudson/cli/PlainCLIProtocol.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cli/src/main/java/hudson/cli/PlainCLIProtocol.java b/cli/src/main/java/hudson/cli/PlainCLIProtocol.java index 1737e231206c..d3f453448264 100644 --- a/cli/src/main/java/hudson/cli/PlainCLIProtocol.java +++ b/cli/src/main/java/hudson/cli/PlainCLIProtocol.java @@ -77,6 +77,12 @@ private enum Op { Op(boolean clientSide) { this.clientSide = clientSide; } + + void validate(boolean isClient) throws ProtocolException { + if (this.clientSide != isClient) { + throw new ProtocolException("Operation not allowed on this side: " + this); + } + } } interface Output extends Closeable { @@ -273,7 +279,7 @@ abstract static class ServerSide extends EitherSide { @Override protected final boolean handle(Op op, DataInputStream dis) throws IOException { - assert op.clientSide; + op.validate(false); switch (op) { case ARG: onArg(dis.readUTF()); @@ -332,7 +338,7 @@ abstract static class ClientSide extends EitherSide { @Override protected boolean handle(Op op, DataInputStream dis) throws IOException { - assert !op.clientSide; + op.validate(true); switch (op) { case EXIT: onExit(dis.readInt());