use of org.jboss.as.cli.ModelNodeFormatter in project wildfly-core by wildfly.
the class ReadAttributeHandler method handleResponse.
@Override
protected void handleResponse(CommandContext ctx, ModelNode response, boolean composite) throws CommandFormatException {
if (!Util.isSuccess(response)) {
throw new CommandFormatException(Util.getFailureDescription(response));
}
if (!response.hasDefined(Util.RESULT)) {
return;
}
final ModelNode result = response.get(Util.RESULT);
if (composite) {
final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
final StringBuilder valueBuf = new StringBuilder();
if (result.hasDefined(Util.STEP_1)) {
final ModelNode stepOutcome = result.get(Util.STEP_1);
if (Util.isSuccess(stepOutcome)) {
if (stepOutcome.hasDefined(Util.RESULT)) {
final ModelNode valueResult = stepOutcome.get(Util.RESULT);
final ModelNodeFormatter formatter = ModelNodeFormatter.Factory.forType(valueResult.getType());
formatter.format(valueBuf, 0, valueResult);
} else {
valueBuf.append("n/a");
}
table.addLine(new String[] { "value", valueBuf.toString() });
} else {
throw new CommandFormatException("Failed to get resource description: " + response);
}
}
if (result.hasDefined(Util.STEP_2)) {
final ModelNode stepOutcome = result.get(Util.STEP_2);
if (Util.isSuccess(stepOutcome)) {
if (stepOutcome.hasDefined(Util.RESULT)) {
final ModelNode descrResult = stepOutcome.get(Util.RESULT);
if (descrResult.hasDefined(Util.ATTRIBUTES)) {
ModelNode attributes = descrResult.get(Util.ATTRIBUTES);
final String name = this.name.getValue(ctx.getParsedCommandLine());
if (name == null) {
throw new CommandFormatException("Attribute name is not available in handleResponse.");
} else if (attributes.hasDefined(name)) {
final ModelNode descr = attributes.get(name);
for (String prop : descr.keys()) {
table.addLine(new String[] { prop, descr.get(prop).asString() });
}
} else {
throw new CommandFormatException("Attribute description is not available.");
}
} else {
throw new CommandFormatException("The resource doesn't provide attribute descriptions.");
}
} else {
throw new CommandFormatException("Result is not available for read-resource-description request: " + response);
}
} else {
throw new CommandFormatException("Failed to get resource description: " + response);
}
}
ctx.printLine(table.toString(true));
} else {
final ModelNodeFormatter formatter = ModelNodeFormatter.Factory.forType(result.getType());
final StringBuilder buf = new StringBuilder();
formatter.format(buf, 0, result);
ctx.printLine(buf.toString());
}
}
use of org.jboss.as.cli.ModelNodeFormatter in project wildfly-core by wildfly.
the class ReadAttributeHandler method formatResponse.
protected StringBuilder formatResponse(CommandContext ctx, ModelNode opResponse, boolean composite, StringBuilder buf) throws CommandFormatException {
if (!opResponse.hasDefined(Util.RESULT)) {
return null;
}
final ModelNode result = opResponse.get(Util.RESULT);
if (composite) {
final Set<String> keys;
try {
keys = result.keys();
} catch (Exception e) {
throw new CommandFormatException("Failed to get step results from a composite operation response " + opResponse);
}
for (String key : keys) {
final ModelNode stepResponse = result.get(key);
// TODO nested composite ops aren't expected for now
buf = formatResponse(ctx, stepResponse, false, buf);
}
} else {
final ModelNodeFormatter formatter = ModelNodeFormatter.Factory.forType(result.getType());
if (buf == null) {
buf = new StringBuilder();
}
formatter.format(buf, 0, result);
}
return buf;
}
use of org.jboss.as.cli.ModelNodeFormatter in project wildfly-core by wildfly.
the class GenericTypeOperationHandler method formatResponse.
protected StringBuilder formatResponse(CommandContext ctx, ModelNode opResponse, boolean composite, StringBuilder buf) throws CommandFormatException {
if (opResponse.hasDefined(Util.RESULT)) {
final ModelNode result = opResponse.get(Util.RESULT);
if (composite) {
final Set<String> keys;
try {
keys = result.keys();
} catch (Exception e) {
throw new CommandFormatException("Failed to get step results from a composite operation response " + opResponse);
}
for (String key : keys) {
final ModelNode stepResponse = result.get(key);
// TODO nested composite ops aren't expected for now
buf = formatResponse(ctx, stepResponse, false, buf);
}
} else {
final ModelNodeFormatter formatter = ModelNodeFormatter.Factory.forType(result.getType());
if (buf == null) {
buf = new StringBuilder();
}
formatter.format(buf, 0, result);
}
}
if (opResponse.hasDefined(Util.RESPONSE_HEADERS)) {
final ModelNode headers = opResponse.get(Util.RESPONSE_HEADERS);
final Set<String> keys = headers.keys();
final SimpleTable table = new SimpleTable(2, ctx.getTerminalWidth());
for (String key : keys) {
table.addLine(new String[] { key + ':', headers.get(key).asString() });
}
if (buf == null) {
buf = new StringBuilder();
} else {
buf.append(Util.LINE_SEPARATOR);
}
table.append(buf, false);
}
return buf;
}
Aggregations