mike-jumper commented on a change in pull request #500:
URL: https://github.com/apache/guacamole-client/pull/500#discussion_r575728206



##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/GuacamoleServerErrorCommandException.java
##########
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.guacamole;
+
+import org.apache.guacamole.protocol.GuacamoleStatus;
+
+/**
+ * The exception thrown when the Guacamole server explicitly sends an error
+ * command to the client. The error message and status code reflect the 
arguments
+ * of the error command as determined by the server.
+ */
+public class GuacamoleServerErrorCommandException extends 
GuacamoleServerException {

Review comment:
       Regarding `GuacamoleServerErrorCommandException` and its description, 
"error command" isn't terminology used within the Guacamole protocol. In this 
case, the correct term would be "error instruction", but I don't think simply 
renaming things to `GuacamoleServerErrorInstructionException` would be a good 
choice, as that too closely ties the error to the vehicle carrying it. The 
naming of the exception and the documentation describing the exception should 
capture the context, rather than the means.
   
   I suggest something like `GuacamoleHandshakeException`, for an error that 
prevents the Guacamole protocol handshake from continuing. I'm sure there are 
other possibilities.

##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/protocol/ConfiguredGuacamoleSocket.java
##########
@@ -83,14 +111,24 @@ private GuacamoleInstruction expect(GuacamoleReader 
reader, String opcode)
         if (instruction == null)
             throw new GuacamoleServerException("End of stream while waiting 
for \"" + opcode + "\".");
 
+        // Handle server control commands

Review comment:
       instructions*

##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/protocol/ConfiguredGuacamoleSocket.java
##########
@@ -63,12 +64,39 @@
      */
     private GuacamoleProtocolVersion protocolVersion =
             GuacamoleProtocolVersion.VERSION_1_0_0;
-    
+
+    /**
+     * Parses the arguments for the Guacamole "error" server command and 
returns
+     * the corresponding exception.
+     * @param args The arguments as provided by the server command.
+     * @return An instance of {@link GuacamoleServerErrorCommandException} 
configured
+     *         with the server-provided arguments, or {@literal null} if the 
specified
+     *         arguments are invalid.
+     */
+    private static GuacamoleServerErrorCommandException 
parseServerErrorCommandArgs(List<String> args) {
+        if (args == null || args.size() != 2)
+            return null;

Review comment:
       1. It's pretty common for instructions to be extended down the line by 
adding new arguments, relying on older implementations to ignore any additional 
arguments. I think it would be better here to test `args.size() < 2`.
   2. Under what circumstance would `args` be `null`?

##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/protocol/ConfiguredGuacamoleSocket.java
##########
@@ -83,14 +111,24 @@ private GuacamoleInstruction expect(GuacamoleReader 
reader, String opcode)
         if (instruction == null)
             throw new GuacamoleServerException("End of stream while waiting 
for \"" + opcode + "\".");
 
+        // Handle server control commands
+        if ("disconnect".equals(instruction.getOpcode()))
+            throw new GuacamoleServerException("Server disconnected while 
waiting for \"" + opcode + "\".");
+        if ("error".equals(instruction.getOpcode())) {
+            GuacamoleServerErrorCommandException e = 
parseServerErrorCommandArgs(instruction.getArgs());
+            if (e == null)
+                throw new GuacamoleServerException("Invalid command received 
from server: " + instruction);

Review comment:
       These are referred to as instructions, not commands.

##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/GuacamoleServerErrorCommandException.java
##########
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.guacamole;
+
+import org.apache.guacamole.protocol.GuacamoleStatus;
+
+/**
+ * The exception thrown when the Guacamole server explicitly sends an error
+ * command to the client. The error message and status code reflect the 
arguments
+ * of the error command as determined by the server.
+ */
+public class GuacamoleServerErrorCommandException extends 
GuacamoleServerException {
+    /**
+     * The Guacamole protocol status code, as determined by the server;
+     */
+    private final GuacamoleStatus status;
+
+    /**
+     * Creates a new GuacamoleServerException with the given message and 
status.
+     *
+     * @param message The error message, as determined by the server where the 
error
+     *               originated.
+     * @param status The status code, as determined by the server where the 
error originated.

Review comment:
       Please follow the style used for documentation elsewhere in the code. 
For example:
   
   
https://github.com/apache/guacamole-client/blob/0091bb1aea14c567c8166f0ed8eadf7c31b6bd6e/guacamole-common/src/main/java/org/apache/guacamole/GuacamoleUpstreamUnavailableException.java#L30-L42
   
   The style you've used here is similar to what we used to do, but we migrated 
away from that since it can be difficult to read, particularly for long 
variable names. New code should use the new style.

##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/GuacamoleServerErrorCommandException.java
##########
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.guacamole;
+
+import org.apache.guacamole.protocol.GuacamoleStatus;
+
+/**
+ * The exception thrown when the Guacamole server explicitly sends an error
+ * command to the client. The error message and status code reflect the 
arguments
+ * of the error command as determined by the server.
+ */
+public class GuacamoleServerErrorCommandException extends 
GuacamoleServerException {
+    /**
+     * The Guacamole protocol status code, as determined by the server;
+     */
+    private final GuacamoleStatus status;
+
+    /**
+     * Creates a new GuacamoleServerException with the given message and 
status.

Review comment:
       This should note the name of the exception in question, not 
`GuacamoleServerException`.

##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/protocol/ConfiguredGuacamoleSocket.java
##########
@@ -63,12 +64,39 @@
      */
     private GuacamoleProtocolVersion protocolVersion =
             GuacamoleProtocolVersion.VERSION_1_0_0;
-    
+
+    /**
+     * Parses the arguments for the Guacamole "error" server command and 
returns
+     * the corresponding exception.
+     * @param args The arguments as provided by the server command.
+     * @return An instance of {@link GuacamoleServerErrorCommandException} 
configured
+     *         with the server-provided arguments, or {@literal null} if the 
specified
+     *         arguments are invalid.
+     */
+    private static GuacamoleServerErrorCommandException 
parseServerErrorCommandArgs(List<String> args) {
+        if (args == null || args.size() != 2)
+            return null;
+
+        int code;
+        try {
+            code = Integer.parseInt(args.get(1));
+        } catch (NumberFormatException e) {
+            return null;
+        }
+        GuacamoleStatus status = GuacamoleStatus.fromGuacamoleStatusCode(code);
+        return (status == null)
+                ? null
+                : new GuacamoleServerErrorCommandException(args.get(0), 
status);
+    }
+
     /**
      * Waits for the instruction having the given opcode, returning that
      * instruction once it has been read. If the instruction is never read,
      * an exception is thrown.
-     * 
+     *
+     * Respects server control commands that are allowed during the handshake

Review comment:
       instructions*

##########
File path: 
guacamole-common/src/main/java/org/apache/guacamole/protocol/ConfiguredGuacamoleSocket.java
##########
@@ -83,14 +111,24 @@ private GuacamoleInstruction expect(GuacamoleReader 
reader, String opcode)
         if (instruction == null)
             throw new GuacamoleServerException("End of stream while waiting 
for \"" + opcode + "\".");
 
+        // Handle server control commands
+        if ("disconnect".equals(instruction.getOpcode()))
+            throw new GuacamoleServerException("Server disconnected while 
waiting for \"" + opcode + "\".");
+        if ("error".equals(instruction.getOpcode())) {
+            GuacamoleServerErrorCommandException e = 
parseServerErrorCommandArgs(instruction.getArgs());
+            if (e == null)
+                throw new GuacamoleServerException("Invalid command received 
from server: " + instruction);

Review comment:
       Might be better to let it be the responsibility of the function parsing 
the `error` instruction to decide whether the instruction is valid and throw an 
appropriate exception if it isn't. This would simplify the logic here and more 
cleanly separate concerns.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to