Search in sources :

Example 1 with ModelNodeFormatter

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());
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode) ModelNodeFormatter(org.jboss.as.cli.ModelNodeFormatter)

Example 2 with ModelNodeFormatter

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;
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) ModelNode(org.jboss.dmr.ModelNode) ModelNodeFormatter(org.jboss.as.cli.ModelNodeFormatter) IOException(java.io.IOException) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) CommandFormatException(org.jboss.as.cli.CommandFormatException)

Example 3 with ModelNodeFormatter

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;
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode) ModelNodeFormatter(org.jboss.as.cli.ModelNodeFormatter) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandFormatException(org.jboss.as.cli.CommandFormatException)

Aggregations

CommandFormatException (org.jboss.as.cli.CommandFormatException)3 ModelNodeFormatter (org.jboss.as.cli.ModelNodeFormatter)3 ModelNode (org.jboss.dmr.ModelNode)3 IOException (java.io.IOException)2 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)2 SimpleTable (org.jboss.as.cli.util.SimpleTable)2 CommandLineException (org.jboss.as.cli.CommandLineException)1