Search in sources :

Example 11 with SimpleTable

use of org.jboss.as.cli.util.SimpleTable in project wildfly-core by wildfly.

the class BaseOperationCommand method displayResponseHeaders.

protected void displayResponseHeaders(CommandContext ctx, ModelNode response) {
    if (response.has(Util.RESPONSE_HEADERS)) {
        final ModelNode headers = response.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() });
        }
        final StringBuilder buf = new StringBuilder();
        table.append(buf, true);
        ctx.printLine(buf.toString());
    }
}
Also used : SimpleTable(org.jboss.as.cli.util.SimpleTable) ModelNode(org.jboss.dmr.ModelNode)

Example 12 with SimpleTable

use of org.jboss.as.cli.util.SimpleTable in project wildfly-core by wildfly.

the class ConnectionInfoHandler method doHandle.

/* (non-Javadoc)
     * @see org.jboss.as.cli.CommandHandler#doHandle(org.jboss.as.cli.CommandContext)
     */
@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    final ModelControllerClient client = ctx.getModelControllerClient();
    if (client == null) {
        ctx.printLine("<connect to the controller and re-run the connection-info command to see the connection information>\n");
    } else {
        ConnectionInfo connInfo = ctx.getConnectionInfo();
        String username = null;
        final ModelNode req = new ModelNode();
        req.get(Util.OPERATION).set("whoami");
        req.get(Util.ADDRESS).setEmptyList();
        req.get("verbose").set(true);
        try {
            final ModelNode response = client.execute(req);
            if (Util.isSuccess(response)) {
                if (response.hasDefined(Util.RESULT)) {
                    final ModelNode result = response.get(Util.RESULT);
                    if (result.hasDefined("identity")) {
                        username = result.get("identity").get("username").asString();
                    }
                    if (result.hasDefined("mapped-roles")) {
                        String strRoles = result.get("mapped-roles").asString();
                        String grantedStr = "granted role";
                        // a comma is contained in the string if there is more than one role
                        if (strRoles.indexOf(',') > 0)
                            grantedStr = "granted roles";
                        username = username + ", " + grantedStr + " " + strRoles;
                    } else {
                        username = username + " has no role associated.";
                    }
                } else {
                    username = "result was not available.";
                }
            } else {
                ctx.printLine(Util.getFailureDescription(response));
            }
        } catch (IOException e) {
            throw new CommandFormatException("Failed to get the AS release info: " + e.getLocalizedMessage());
        }
        SimpleTable st = new SimpleTable(2, ctx.getTerminalWidth());
        st.addLine(new String[] { "Username", username });
        st.addLine(new String[] { "Logged since", connInfo.getLoggedSince().toString() });
        X509Certificate[] lastChain = connInfo.getServerCertificates();
        boolean sslConn = lastChain != null;
        if (sslConn) {
            try {
                for (X509Certificate x509Current : lastChain) {
                    Map<String, String> fingerprints = FingerprintGenerator.generateFingerprints(x509Current);
                    st.addLine(new String[] { "Subject", x509Current.getSubjectX500Principal().getName() });
                    st.addLine(new String[] { "Issuer", x509Current.getIssuerDN().getName() });
                    st.addLine(new String[] { "Valid from", x509Current.getNotBefore().toString() });
                    st.addLine(new String[] { "Valid to", x509Current.getNotAfter().toString() });
                    for (String alg : fingerprints.keySet()) {
                        st.addLine(new String[] { alg, fingerprints.get(alg) });
                    }
                }
            } catch (CommandLineException cle) {
                throw new CommandFormatException("Error trying to generate server certificate fingerprint.", cle);
            }
        } else {
            st.addLine(new String[] { "Not an SSL connection.", "" });
        }
        ctx.printLine(st.toString());
    }
}
Also used : ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CommandFormatException(org.jboss.as.cli.CommandFormatException) SimpleTable(org.jboss.as.cli.util.SimpleTable) ConnectionInfo(org.jboss.as.cli.ConnectionInfo) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) X509Certificate(java.security.cert.X509Certificate) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 13 with SimpleTable

use of org.jboss.as.cli.util.SimpleTable in project wildfly-core by wildfly.

the class LsHandler method handleResponse.

@Override
protected void handleResponse(CommandContext ctx, ModelNode outcome, boolean composite) throws CommandLineException {
    final ParsedCommandLine parsedCmd = ctx.getParsedCommandLine();
    if (!composite) {
        final List<ModelNode> nodeList = outcome.get(Util.RESULT).asList();
        if (!nodeList.isEmpty()) {
            final List<String> list = new ArrayList<String>(nodeList.size());
            for (ModelNode node : nodeList) {
                list.add(node.asString());
            }
            printList(ctx, list, l.isPresent(parsedCmd));
        }
        return;
    }
    List<String> additionalProps = Collections.emptyList();
    if (l.isPresent(parsedCmd)) {
        final Set<String> argNames = parsedCmd.getPropertyNames();
        final boolean resolvePresent = resolve.isPresent(parsedCmd);
        final int recognizedArgs = 1 + (resolvePresent ? 1 : 0);
        if (argNames.size() > recognizedArgs) {
            additionalProps = new ArrayList<String>(argNames.size() - recognizedArgs);
            int i = 0;
            for (String arg : argNames) {
                if (arg.equals(l.getFullName()) || resolvePresent && arg.equals(resolve.getFullName())) {
                    continue;
                }
                final String prop;
                if (arg.length() > 1 && arg.charAt(0) == '-') {
                    if (arg.charAt(1) == '-') {
                        prop = arg.substring(2);
                    } else {
                        prop = arg.substring(1);
                    }
                } else {
                    prop = arg;
                }
                additionalProps.add(prop);
            }
        }
    }
    ModelNode resultNode = outcome.get(Util.RESULT);
    ModelNode attrDescriptions = null;
    ModelNode childDescriptions = null;
    if (resultNode.hasDefined(Util.STEP_3)) {
        final ModelNode stepOutcome = resultNode.get(Util.STEP_3);
        if (Util.isSuccess(stepOutcome)) {
            if (stepOutcome.hasDefined(Util.RESULT)) {
                final ModelNode descrResult = stepOutcome.get(Util.RESULT);
                if (descrResult.hasDefined(Util.ATTRIBUTES)) {
                    attrDescriptions = descrResult.get(Util.ATTRIBUTES);
                }
                if (descrResult.hasDefined(Util.CHILDREN)) {
                    childDescriptions = descrResult.get(Util.CHILDREN);
                }
            } else {
                throw new CommandFormatException("Result is not available for read-resource-description request: " + outcome);
            }
        } else {
            throw new CommandFormatException("Failed to get resource description: " + outcome);
        }
    }
    List<String> names = null;
    List<String> typeNames = null;
    if (resultNode.hasDefined(Util.STEP_1)) {
        ModelNode typesOutcome = resultNode.get(Util.STEP_1);
        if (Util.isSuccess(typesOutcome)) {
            if (typesOutcome.hasDefined(Util.RESULT)) {
                final ModelNode resourceResult = typesOutcome.get(Util.RESULT);
                final List<ModelNode> types = resourceResult.asList();
                if (!types.isEmpty()) {
                    typeNames = new ArrayList<String>();
                    for (ModelNode type : types) {
                        typeNames.add(type.asString());
                    }
                    if (childDescriptions == null && attrDescriptions == null) {
                        names = typeNames;
                    }
                }
            } else {
                throw new CommandFormatException("Result is not available for read-children-types request: " + outcome);
            }
        } else {
            throw new CommandFormatException("Failed to fetch type names: " + outcome);
        }
    } else {
        throw new CommandFormatException("The result for children type names is not available: " + outcome);
    }
    if (resultNode.hasDefined(Util.STEP_2)) {
        ModelNode resourceOutcome = resultNode.get(Util.STEP_2);
        if (Util.isSuccess(resourceOutcome)) {
            if (resourceOutcome.hasDefined(Util.RESULT)) {
                final ModelNode resourceResult = resourceOutcome.get(Util.RESULT);
                final List<Property> props = resourceResult.asPropertyList();
                if (!props.isEmpty()) {
                    final SimpleTable attrTable;
                    if (attrDescriptions == null) {
                        attrTable = null;
                    } else {
                        if (!additionalProps.isEmpty()) {
                            String[] headers = new String[3 + additionalProps.size()];
                            headers[0] = "ATTRIBUTE";
                            headers[1] = "VALUE";
                            headers[2] = "TYPE";
                            int i = 3;
                            for (String additional : additionalProps) {
                                headers[i++] = additional.toUpperCase(Locale.ENGLISH);
                            }
                            attrTable = new SimpleTable(headers, ctx.getTerminalWidth());
                        } else {
                            attrTable = new SimpleTable(new String[] { "ATTRIBUTE", "VALUE", "TYPE" }, ctx.getTerminalWidth());
                        }
                    }
                    SimpleTable childrenTable = childDescriptions == null ? null : new SimpleTable(new String[] { "CHILD", "MIN-OCCURS", "MAX-OCCURS" }, ctx.getTerminalWidth());
                    if (typeNames == null && attrTable == null && childrenTable == null) {
                        typeNames = new ArrayList<String>();
                        names = typeNames;
                    }
                    for (Property prop : props) {
                        final StringBuilder buf = new StringBuilder();
                        if (typeNames == null || !typeNames.contains(prop.getName())) {
                            if (attrDescriptions == null) {
                                buf.append(prop.getName());
                                buf.append('=');
                                buf.append(prop.getValue().asString());
                                // TODO the value should be formatted nicer but the current
                                // formatter uses new lines for complex value which doesn't work here
                                // final ModelNode value = prop.getValue();
                                // ModelNodeFormatter.Factory.forType(value.getType()).format(buf, 0, value);
                                typeNames.add(buf.toString());
                                buf.setLength(0);
                            } else {
                                final String[] line = new String[attrTable.columnsTotal()];
                                line[0] = prop.getName();
                                line[1] = prop.getValue().asString();
                                if (attrDescriptions.hasDefined(prop.getName())) {
                                    final ModelNode attrDescr = attrDescriptions.get(prop.getName());
                                    line[2] = getAsString(attrDescr, Util.TYPE);
                                    if (!additionalProps.isEmpty()) {
                                        int i = 3;
                                        for (String additional : additionalProps) {
                                            line[i++] = getAsString(attrDescr, additional);
                                        }
                                    }
                                } else {
                                    for (int i = 2; i < line.length; ++i) {
                                        line[i] = "n/a";
                                    }
                                }
                                attrTable.addLine(line);
                            }
                        } else if (childDescriptions != null) {
                            if (childDescriptions.hasDefined(prop.getName())) {
                                final ModelNode childDescr = childDescriptions.get(prop.getName());
                                final Integer maxOccurs = getAsInteger(childDescr, Util.MAX_OCCURS);
                                childrenTable.addLine(new String[] { prop.getName(), getAsString(childDescr, Util.MIN_OCCURS), maxOccurs == null ? "n/a" : (maxOccurs == Integer.MAX_VALUE ? "unbounded" : maxOccurs.toString()) });
                            } else {
                                childrenTable.addLine(new String[] { prop.getName(), "n/a", "n/a" });
                            }
                        }
                    }
                    StringBuilder buf = null;
                    if (attrTable != null && !attrTable.isEmpty()) {
                        buf = new StringBuilder();
                        attrTable.append(buf, true);
                    }
                    if (childrenTable != null && !childrenTable.isEmpty()) {
                        if (buf == null) {
                            buf = new StringBuilder();
                        } else {
                            buf.append("\n\n");
                        }
                        childrenTable.append(buf, true);
                    }
                    if (buf != null) {
                        ctx.printLine(buf.toString());
                    }
                }
            } else {
                throw new CommandFormatException("Result is not available for read-resource request: " + outcome);
            }
        } else {
            throw new CommandFormatException("Failed to fetch attributes: " + outcome);
        }
    } else {
        throw new CommandFormatException("The result for attributes is not available: " + outcome);
    }
    if (names != null) {
        printList(ctx, names, l.isPresent(parsedCmd));
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleTable(org.jboss.as.cli.util.SimpleTable) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 14 with SimpleTable

use of org.jboss.as.cli.util.SimpleTable 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

SimpleTable (org.jboss.as.cli.util.SimpleTable)14 ModelNode (org.jboss.dmr.ModelNode)12 CommandFormatException (org.jboss.as.cli.CommandFormatException)7 CommandLineException (org.jboss.as.cli.CommandLineException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Property (org.jboss.dmr.Property)3 ModelNodeFormatter (org.jboss.as.cli.ModelNodeFormatter)2 ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)2 Identity (org.jboss.as.patching.metadata.Identity)2 PatchElement (org.jboss.as.patching.metadata.PatchElement)2 X509Certificate (java.security.cert.X509Certificate)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 ZipException (java.util.zip.ZipException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ConnectionInfo (org.jboss.as.cli.ConnectionInfo)1 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)1 StrictSizeTable (org.jboss.as.cli.util.StrictSizeTable)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1