Skip to content

Commit

Permalink
refactor: add centralized validation inside Op enum
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbus committed Nov 28, 2024
1 parent 2f7440a commit 080895b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cli/src/main/java/hudson/cli/PlainCLIProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 080895b

Please sign in to comment.