Added validity check for commands
This commit is contained in:
parent
fdbec3d652
commit
7bf35977f0
@ -3,6 +3,7 @@ package envoy.client.data.commands;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import envoy.util.EnvoyLog;
|
import envoy.util.EnvoyLog;
|
||||||
@ -23,45 +24,58 @@ public class SystemCommandsMap {
|
|||||||
|
|
||||||
private final HashMap<String, SystemCommand> systemCommands = new HashMap<>();
|
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
|
* @param command the string that must be inputted to execute the
|
||||||
* given action
|
* given action
|
||||||
* @param action the action that should be performed
|
* @param action the action that should be performed
|
||||||
* @param numberOfArguments the amount of arguments that need to be parsed for
|
* @param numberOfArguments the amount of arguments that need to be parsed for
|
||||||
* the underlying function
|
* the underlying function
|
||||||
|
* @see SystemCommandsMap#isValidKey(String)
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public void addCommand(String command, Function<String[], Void> action, int numberOfArguments) {
|
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
|
* @param command the string that must be inputted to execute the
|
||||||
* given action
|
* given action
|
||||||
* @param action the action that should be performed
|
* @param action the action that should be performed
|
||||||
* @param numberOfArguments the amount of arguments that need to be parsed for
|
* @param numberOfArguments the amount of arguments that need to be parsed for
|
||||||
* the underlying function
|
* the underlying function
|
||||||
* @param description the description of this {@link SystemCommand}
|
* @param description the description of this {@link SystemCommand}
|
||||||
|
* @see SystemCommandsMap#isValidKey(String)
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public void addCommand(String command, Function<String[], Void> action, int numberOfArguments, String description) {
|
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
|
* Adds a command with according action that does not depend on arguments, if
|
||||||
* arguments in the given String
|
* the command does not violate API constrictions, to the map.
|
||||||
*
|
*
|
||||||
* @param command the string that must be inputted to execute the given action
|
* @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
|
* @param action the action that should be performed. To see why this Function
|
||||||
* takes a {@code String[]}, see {@link SystemCommand}
|
* takes a {@code String[]}, see {@link SystemCommand}
|
||||||
|
* @see SystemCommandsMap#isValidKey(String)
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public void addNoArgCommand(String command, Function<String[], Void> action) { addCommand(command, action, 0); }
|
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
|
* Adds a command with according action that does not depend on arguments and a
|
||||||
* arguments in the given String
|
* 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
|
* @param command the string that must be inputted to execute the given
|
||||||
* action
|
* action
|
||||||
@ -69,11 +83,18 @@ public class SystemCommandsMap {
|
|||||||
* Function takes a {@code String[]}, see
|
* Function takes a {@code String[]}, see
|
||||||
* {@link SystemCommand}
|
* {@link SystemCommand}
|
||||||
* @param description the description of this {@link SystemCommand}
|
* @param description the description of this {@link SystemCommand}
|
||||||
|
* @see SystemCommandsMap#isValidKey(String)
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public void addNoArgCommand(String command, Function<String[], Void> action, String description) { addCommand(command, action, 0); }
|
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
|
* @param command the string that must be inputted to execute the
|
||||||
* given action
|
* given action
|
||||||
* @param action the action that should be performed. To see why this
|
* @param action the action that should be performed. To see why this
|
||||||
@ -81,10 +102,32 @@ public class SystemCommandsMap {
|
|||||||
* {@link SystemCommand}
|
* {@link SystemCommand}
|
||||||
* @param numberOfArguments the amount of arguments that need to be parsed for
|
* @param numberOfArguments the amount of arguments that need to be parsed for
|
||||||
* the underlying function
|
* the underlying function
|
||||||
|
* @see SystemCommandsMap#isValidKey(String)
|
||||||
* @since Envoy Client v0.1-beta
|
* @since Envoy Client v0.1-beta
|
||||||
*/
|
*/
|
||||||
public void add(String command, Function<String[], Void> action, int numberOfArguments) {
|
public void add(String command, Function<String[], Void> action, int numberOfArguments) { addCommand(command, action, numberOfArguments); }
|
||||||
systemCommands.put(command, new SystemCommand(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user