Create VerifyArgumentValidator

This commit is contained in:
chubtub 2024-03-06 10:30:59 -05:00
parent 6b76d873e6
commit d83c34f4ad
3 changed files with 60 additions and 2 deletions

View File

@ -11,7 +11,7 @@ import java.util.List;
* Commander is a class that handles the command line arguments for the SWID
* Tags gateway by implementing the JCommander package.
*/
@Parameters(parametersValidators = CreateArgumentValidator.class)
@Parameters(parametersValidators = {CreateArgumentValidator.class, VerifyArgumentValidator.class})
public class Commander {
@Parameter(description = "This parameter catches all unrecognized arguments.")

View File

@ -27,7 +27,7 @@ public class CreateArgumentValidator implements IParametersValidator {
public void validate(Map<String, Object> parameters) throws ParameterException {
if (isValueNotNull(parameters,"--create")) {
if (isValueNotNull(parameters,"--verify")) {
errorMessage += "Create and verify cannot be called together. ";
throw new ParameterException("Create and verify cannot be called together.");
} else {
for (String arg : requiredArgs) {
if (!isValueNotNull(parameters, arg)) {

View File

@ -0,0 +1,58 @@
package hirs.swid.utils;
import com.beust.jcommander.IParametersValidator;
import com.beust.jcommander.ParameterException;
import lombok.extern.log4j.Log4j2;
import java.util.Map;
/**
* This class handles validating all arguments in the context of a verify function.
* The input arguments are checked that --create is not also selected and that all
* required inputs for --verify are present.
*/
@Log4j2
public class VerifyArgumentValidator implements IParametersValidator {
String[] requiredArgs = {"--rimel", "--publicCertificate", "--truststore"};
String errorMessage = "";
/**
* This method validates the input parameter map.
* @param parameters
* Name-value-pairs of all parameters (e.g. "-host":"localhost").
*
* @throws ParameterException
*/
@Override
public void validate(Map<String, Object> parameters) throws ParameterException {
if (isValueNotNull(parameters,"--verify")) {
if (isValueNotNull(parameters,"--create")) {
throw new ParameterException("Create and verify cannot be called together.");
} else {
for (String arg : requiredArgs) {
if (!isValueNotNull(parameters, arg)) {
errorMessage += arg + " is required to verify a base RIM. ";
}
}
}
}
if (!errorMessage.isEmpty()) {
throw new ParameterException(errorMessage);
}
}
/**
* This method checks the given key for a null value
* @param parameters map
* @param key the key to check
* @return true if not null, else false
*/
private boolean isValueNotNull(Map<String, Object> parameters, String key) {
Object object = parameters.get(key);
if (object == null) {
return false;
} else {
return true;
}
}
}