Added validity check for commands

This commit is contained in:
delvh 2020-07-23 09:23:29 +02:00
parent 98521aea93
commit 3dd8890d12
No known key found for this signature in database
GPG Key ID: 42B77E634CE94D82

View File

@ -3,6 +3,7 @@ package envoy.client.data.commands;
import java.util.*;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import envoy.util.EnvoyLog;
@ -23,45 +24,58 @@ public class SystemCommandsMap {
private final HashMap<String, SystemCommand> systemCommands = new HashMap<>();
private final Pattern commandBounds = Pattern.compile("^[a-zA-Z0-9_:!\\(\\)\\?\\.\\,\\;\\-]+$");
/**
* Adds a command with according action and the number of arguments that should
* be parsed if the command does not violate API constrictions to the map.
*
* @param command the string that must be inputted to execute the
* given action
* @param action the action that should be performed
* @param numberOfArguments the amount of arguments that need to be parsed for
* the underlying function
* @see SystemCommandsMap#isValidKey(String)
* @since Envoy Client v0.1-beta
*/
public void addCommand(String command, Function<String[], Void> action, int numberOfArguments) {
systemCommands.put(command, new SystemCommand(action, numberOfArguments, ""));
if (isValidKey(command)) systemCommands.put(command, new SystemCommand(action, numberOfArguments, ""));
}
/**
* Adds a command with according action, the number of arguments that should be
* parsed and a description of the system command, if the command does not
* violate API constrictions, to the map.
*
* @param command the string that must be inputted to execute the
* given action
* @param action the action that should be performed
* @param numberOfArguments the amount of arguments that need to be parsed for
* the underlying function
* @param description the description of this {@link SystemCommand}
* @see SystemCommandsMap#isValidKey(String)
* @since Envoy Client v0.1-beta
*/
public void addCommand(String command, Function<String[], Void> action, int numberOfArguments, String description) {
systemCommands.put(command, new SystemCommand(action, numberOfArguments, description));
if (isValidKey(command)) systemCommands.put(command, new SystemCommand(action, numberOfArguments, description));
}
/**
* Adds a new {@link SystemCommand} to the map that does not depend upon
* arguments in the given String
* Adds a command with according action that does not depend on arguments, if
* the command does not violate API constrictions, to the map.
*
* @param command the string that must be inputted to execute the given action
* @param action the action that should be performed. To see why this Function
* takes a {@code String[]}, see {@link SystemCommand}
* @see SystemCommandsMap#isValidKey(String)
* @since Envoy Client v0.1-beta
*/
public void addNoArgCommand(String command, Function<String[], Void> action) { addCommand(command, action, 0); }
/**
* Adds a new {@link SystemCommand} to the map that does not depend upon
* arguments in the given String
* Adds a command with according action that does not depend on arguments and a
* description of that action, if the command does not violate API
* constrictions, to the map.
*
* @param command the string that must be inputted to execute the given
* action
@ -69,11 +83,18 @@ public class SystemCommandsMap {
* Function takes a {@code String[]}, see
* {@link SystemCommand}
* @param description the description of this {@link SystemCommand}
* @see SystemCommandsMap#isValidKey(String)
* @since Envoy Client v0.1-beta
*/
public void addNoArgCommand(String command, Function<String[], Void> action, String description) { addCommand(command, action, 0); }
/**
* Convenience method that does the same as
* {@link SystemCommandsMap#addCommand(String, Function, int)}.
* <p>
* Adds a command with according action and the number of arguments that should
* be parsed if the command does not violate API constrictions to the map.
*
* @param command the string that must be inputted to execute the
* given action
* @param action the action that should be performed. To see why this
@ -81,10 +102,32 @@ public class SystemCommandsMap {
* {@link SystemCommand}
* @param numberOfArguments the amount of arguments that need to be parsed for
* the underlying function
* @see SystemCommandsMap#isValidKey(String)
* @since Envoy Client v0.1-beta
*/
public void add(String command, Function<String[], Void> action, int numberOfArguments) {
systemCommands.put(command, new SystemCommand(action, numberOfArguments, ""));
public void add(String command, Function<String[], Void> action, int numberOfArguments) { addCommand(command, action, numberOfArguments); }
/**
* Examines whether a key can be put in the map and logs it with
* {@code Level.WARNING} if that key violates API constrictions <br>
* (allowed chars are <b>a-zA-Z0-9_:!()?.,;-</b>)
* <p>
* The approach to not throw an exception was taken so that an ugly try-catch
* block for every addition to the system commands map could be avoided, an
* error that
* should only occur during implementation and not in production.
*
* @param command the key to examine
* @return whether this key can be used in the map
* @since Envoy Client v0.1-beta
*/
public final boolean isValidKey(String command) {
final boolean valid = commandBounds.matcher(command).matches();
if (!valid) EnvoyLog.getLogger(SystemCommandsMap.class)
.log(Level.WARNING,
"The command \"" + command + "\" is not valid. It will cause problems in execution: Only the characters " + commandBounds
+ "are allowed");
return valid;
}
/**