private void validateRequest(CommandContext ctx, ModelNode request) throws CommandFormatException {
final ModelControllerClient client = ctx.getModelControllerClient();
if(client == null) {
throw new CommandFormatException("No connection to the controller.");
}
final Set<String> keys = request.keys();
if(!keys.contains(Util.OPERATION)) {
throw new CommandFormatException("Request is missing the operation name.");
}
final String operationName = request.get(Util.OPERATION).asString();
if(!keys.contains(Util.ADDRESS)) {
throw new CommandFormatException("Request is missing the address part.");
}
final ModelNode address = request.get(Util.ADDRESS);
if(keys.size() == 2) { // no props
return;
}
final ModelNode opDescrReq = new ModelNode();
opDescrReq.get(Util.ADDRESS).set(address);
opDescrReq.get(Util.OPERATION).set(Util.READ_OPERATION_DESCRIPTION);
opDescrReq.get(Util.NAME).set(operationName);
final ModelNode outcome;
try {
outcome = client.execute(opDescrReq);
} catch(Exception e) {
throw new CommandFormatException("Failed to perform " + Util.READ_OPERATION_DESCRIPTION + " to validate the request: " + e.getLocalizedMessage());
}
if (!Util.isSuccess(outcome)) {
throw new CommandFormatException("Failed to get the list of the operation properties: \"" + Util.getFailureDescription(outcome) + '\"');
}
if(!outcome.has(Util.RESULT)) {
throw new CommandFormatException("Failed to perform " + Util.READ_OPERATION_DESCRIPTION + " to validate the request: result is not available.");
}
final ModelNode result = outcome.get(Util.RESULT);
if(!result.hasDefined(Util.REQUEST_PROPERTIES)) {
if(!(keys.size() == 3 && keys.contains(Util.OPERATION_HEADERS))) {
throw new CommandFormatException("Operation '" + operationName + "' does not expect any property.");
}
} else {
final Set<String> definedProps = result.get(Util.REQUEST_PROPERTIES).keys();
if(definedProps.isEmpty()) {
if(!(keys.size() == 3 && keys.contains(Util.OPERATION_HEADERS))) {
throw new CommandFormatException("Operation '" + operationName + "' does not expect any property.");
}
}
int skipped = 0;
for(String prop : keys) {
if(skipped < 2 && (prop.equals(Util.ADDRESS) || prop.equals(Util.OPERATION))) {
++skipped;
continue;
}
if(!definedProps.contains(prop)) {
if(!Util.OPERATION_HEADERS.equals(prop)) {
throw new CommandFormatException("'" + prop + "' is not found among the supported properties: " + definedProps);
}
}
}
}
}