use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.
the class BaseOperationCommand method isAddressValid.
protected boolean isAddressValid(CommandContext ctx) {
final ModelNode request = new ModelNode();
final ModelNode address = request.get(Util.ADDRESS);
address.setEmptyList();
request.get(Util.OPERATION).set(Util.VALIDATE_ADDRESS);
final ModelNode addressValue = request.get(Util.VALUE);
for (OperationRequestAddress.Node node : requiredAddress) {
addressValue.add(node.getType(), node.getName());
}
final ModelNode response;
try {
response = ctx.getModelControllerClient().execute(request);
} catch (IOException e) {
return false;
}
final ModelNode result = response.get(Util.RESULT);
if (!result.isDefined()) {
return false;
}
final ModelNode valid = result.get(Util.VALID);
if (!valid.isDefined()) {
return false;
}
return valid.asBoolean();
}
use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.
the class DefaultPrefixFormatter method format.
/* (non-Javadoc)
* @see org.jboss.as.cli.PrefixFormatter#format(org.jboss.as.cli.Prefix)
*/
@Override
public String format(OperationRequestAddress prefix) {
Iterator<Node> iterator = prefix.iterator();
if (!iterator.hasNext()) {
return "/";
}
StringBuilder builder = new StringBuilder();
builder.append('/');
Node next = iterator.next();
builder.append(next.getType());
if (next.getName() != null) {
builder.append('=').append(next.getName());
}
while (iterator.hasNext()) {
builder.append('/');
next = iterator.next();
builder.append(next.getType());
if (next.getName() != null) {
builder.append('=').append(next.getName());
}
}
return builder.toString();
}
use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.
the class Util method buildRequest.
public static ModelNode buildRequest(CommandContext ctx, final OperationRequestAddress address, String operation) throws CommandFormatException {
final ModelNode request = new ModelNode();
request.get(Util.OPERATION).set(operation);
final ModelNode addressNode = request.get(Util.ADDRESS);
if (address.isEmpty()) {
addressNode.setEmptyList();
} else {
if (address.endsOnType()) {
throw new CommandFormatException("The address ends on a type: " + address.getNodeType());
}
for (OperationRequestAddress.Node node : address) {
addressNode.add(node.getType(), node.getName());
}
}
return request;
}
use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.
the class Util method toOperationRequest.
private static ModelNode toOperationRequest(CommandContext ctx, ParsedCommandLine parsedLine, Attachments attachments, boolean description) throws CommandFormatException {
if (parsedLine.getFormat() != OperationFormat.INSTANCE) {
throw new OperationFormatException("The line does not follow the operation request format");
}
ModelNode request = new ModelNode();
ModelNode addressNode = request.get(Util.ADDRESS);
if (parsedLine.getAddress().isEmpty()) {
addressNode.setEmptyList();
} else {
Iterator<Node> iterator = parsedLine.getAddress().iterator();
while (iterator.hasNext()) {
OperationRequestAddress.Node node = iterator.next();
if (node.getName() != null) {
addressNode.add(node.getType(), node.getName());
} else if (iterator.hasNext()) {
throw new OperationFormatException("The node name is not specified for type '" + node.getType() + "'");
}
}
}
final String operationName = parsedLine.getOperationName();
if (operationName == null || operationName.isEmpty()) {
throw new OperationFormatException("The operation name is missing or the format of the operation request is wrong.");
}
request.get(Util.OPERATION).set(operationName);
ModelNode outcome = null;
if (description) {
outcome = (ModelNode) ctx.get(Scope.REQUEST, DESCRIPTION_RESPONSE);
if (outcome == null) {
outcome = retrieveDescription(ctx, request, false);
if (outcome != null) {
ctx.set(Scope.REQUEST, DESCRIPTION_RESPONSE, outcome);
}
}
if (outcome != null) {
if (!outcome.has(Util.RESULT)) {
throw new CommandFormatException("Failed to perform " + Util.READ_OPERATION_DESCRIPTION + " to validate the request: result is not available.");
} else {
outcome = outcome.get(Util.RESULT).get(Util.REQUEST_PROPERTIES);
}
}
}
for (String propName : parsedLine.getPropertyNames()) {
String value = parsedLine.getPropertyValue(propName);
if (propName == null || propName.trim().isEmpty())
throw new OperationFormatException("The argument name is not specified: '" + propName + "'");
if (value == null || value.trim().isEmpty()) {
throw new OperationFormatException("The argument value is not specified for " + propName + ": '" + value + "'");
}
final ModelNode toSet = ArgumentValueConverter.DEFAULT.fromString(ctx, value);
if (outcome != null && outcome.hasDefined(propName)) {
try {
ModelNode p = outcome.get(propName);
if (p.hasDefined("type")) {
applyReplacements(ctx, propName, toSet, p, p.get("type").asType(), attachments);
}
} catch (Throwable ex) {
// System.err.println("FAULTY " + propName + " " + outcome.get(propName));
throw ex;
}
}
request.get(propName).set(toSet);
}
if (parsedLine.getLastHeaderName() != null) {
throw new OperationFormatException("Header '" + parsedLine.getLastHeaderName() + "' is not complete.");
}
final Collection<ParsedOperationRequestHeader> headers = parsedLine.getHeaders();
if (headers != null && !headers.isEmpty()) {
final ModelNode headersNode = request.get(Util.OPERATION_HEADERS);
for (ParsedOperationRequestHeader header : headers) {
header.addTo(ctx, headersNode);
}
}
return request;
}
use of org.jboss.as.cli.operation.OperationRequestAddress.Node in project wildfly-core by wildfly.
the class AddressOnlyParsingTestCase method testSlashNodeName.
@Test
public void testSlashNodeName() throws Exception {
DefaultCallbackHandler handler = new DefaultCallbackHandler();
parser.parse("/subsystem=undertow/server=default-server/host=default-host/location=\\/", handler);
OperationRequestAddress address = handler.getAddress();
assertNotNull(address);
Iterator<Node> nodes = address.iterator();
assertTrue(nodes.hasNext());
Node node = nodes.next();
assertEquals("subsystem", node.getType());
assertEquals("undertow", node.getName());
assertTrue(nodes.hasNext());
node = nodes.next();
assertEquals("server", node.getType());
assertEquals("default-server", node.getName());
assertTrue(nodes.hasNext());
node = nodes.next();
assertEquals("host", node.getType());
assertEquals("default-host", node.getName());
assertTrue(nodes.hasNext());
node = nodes.next();
assertEquals("location", node.getType());
assertEquals("/", node.getName());
assertFalse(nodes.hasNext());
}
Aggregations