GUACAMOLE-1048: Leverage exception conversion provided by GuacamoleStatus for "error" instruction handling.
This commit is contained in:
1 parent
e2f2b715a4
commit
0889e4f2d2
2 files changed
+60
-72
No files matched your search
+60
-17
@@ -21,12 +21,13 @@ package org.apache.guacamole.protocol;
|
||||
|
||||
import java.util.List;
|
||||
import org.apache.guacamole.GuacamoleException;
|
||||
import org.apache.guacamole.GuacamoleServerErrorInstructionException;
|
||||
import org.apache.guacamole.GuacamoleServerException;
|
||||
import org.apache.guacamole.io.GuacamoleReader;
|
||||
import org.apache.guacamole.io.GuacamoleWriter;
|
||||
import org.apache.guacamole.net.DelegatingGuacamoleSocket;
|
||||
import org.apache.guacamole.net.GuacamoleSocket;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* A GuacamoleSocket which pre-configures the connection based on a given
|
||||
@@ -40,6 +41,12 @@ import org.apache.guacamole.net.GuacamoleSocket;
|
||||
*/
|
||||
public class ConfiguredGuacamoleSocket extends DelegatingGuacamoleSocket {
|
||||
|
||||
/**
|
||||
* Logger for this class.
|
||||
*/
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(ConfiguredGuacamoleSocket.class);
|
||||
|
||||
/**
|
||||
* The configuration to use when performing the Guacamole protocol
|
||||
* handshake.
|
||||
@@ -62,23 +69,59 @@ public class ConfiguredGuacamoleSocket extends DelegatingGuacamoleSocket {
|
||||
GuacamoleProtocolVersion.VERSION_1_0_0;
|
||||
|
||||
/**
|
||||
* Parses the arguments for the Guacamole "error" server instruction and returns
|
||||
* the corresponding exception.
|
||||
* @param args The arguments as provided by the server instruction.
|
||||
* @return An instance of {@link GuacamoleServerErrorInstructionException} configured
|
||||
* with the server-provided arguments, or a generic {@link GuacamoleServerException} if
|
||||
* the specified arguments are invalid.
|
||||
* Parses the given "error" instruction, throwing a GuacamoleException that
|
||||
* corresponds to its status code and message.
|
||||
*
|
||||
* @param instruction
|
||||
* The "error" instruction to parse.
|
||||
*
|
||||
* @throws GuacamoleException
|
||||
* A GuacamoleException that corresponds to the status code and message
|
||||
* present within the given "error" instruction.
|
||||
*/
|
||||
private static GuacamoleServerException parseServerErrorInstructionArgs(List<String> args) {
|
||||
try {
|
||||
if (args.size() >= 2) {
|
||||
int code = Integer.parseInt(args.get(1));
|
||||
GuacamoleStatus status = GuacamoleStatus.fromGuacamoleStatusCode(code);
|
||||
return new GuacamoleServerErrorInstructionException(args.get(0), status);
|
||||
}
|
||||
} catch (NumberFormatException ignored) {}
|
||||
private static void handleReceivedError(GuacamoleInstruction instruction)
|
||||
throws GuacamoleException {
|
||||
|
||||
// Provide reasonable default error message for invalid "error"
|
||||
// instructions that fail to provide one
|
||||
String message = "Internal error within guacd / protocol handling.";
|
||||
|
||||
// Consider all error instructions without a corresponding status code
|
||||
// to be server errors
|
||||
GuacamoleStatus status = GuacamoleStatus.SERVER_ERROR;
|
||||
|
||||
// Parse human-readable message from "error" instruction, warning if no
|
||||
// message was given
|
||||
List<String> args = instruction.getArgs();
|
||||
if (args.size() >= 1)
|
||||
message = args.get(0);
|
||||
else
|
||||
logger.debug("Received \"error\" instruction with no corresponding message.");
|
||||
|
||||
// Parse the status code from the received error instruction, warning
|
||||
// if the status code is missing or invalid
|
||||
if (args.size() >= 2) {
|
||||
try {
|
||||
|
||||
// Translate numeric status code into a GuacamoleStatus
|
||||
int statusCode = Integer.parseInt(args.get(1));
|
||||
GuacamoleStatus parsedStatus = GuacamoleStatus.fromGuacamoleStatusCode(statusCode);
|
||||
if (parsedStatus != null)
|
||||
status = parsedStatus;
|
||||
else
|
||||
logger.debug("Received \"error\" instruction with unknown/invalid status code: {}", statusCode);
|
||||
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
logger.debug("Received \"error\" instruction with non-numeric status code.", e);
|
||||
}
|
||||
}
|
||||
else
|
||||
logger.debug("Received \"error\" instruction without status code.");
|
||||
|
||||
// Convert parsed status code and message to a GuacamoleException
|
||||
throw status.toException(message);
|
||||
|
||||
return new GuacamoleServerException("Invalid error instruction arguments received: " + args);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,7 +150,7 @@ public class ConfiguredGuacamoleSocket extends DelegatingGuacamoleSocket {
|
||||
if ("disconnect".equals(instruction.getOpcode()))
|
||||
throw new GuacamoleServerException("Server disconnected while waiting for \"" + opcode + "\".");
|
||||
if ("error".equals(instruction.getOpcode()))
|
||||
throw parseServerErrorInstructionArgs(instruction.getArgs());
|
||||
handleReceivedError(instruction);
|
||||
|
||||
// Ensure instruction has expected opcode
|
||||
if (!instruction.getOpcode().equals(opcode))
|
||||
|
||||
Reference in new issue
Block a user