Search in sources :

Example 1 with CredentialCollector

use of org.apache.knox.gateway.shell.CredentialCollector in project knox by apache.

the class SelectCommand method execute.

@SuppressWarnings({ "unchecked", "PMD.CloseResource" })
@Override
public Object execute(List<String> args) {
    boolean ok = false;
    String sql = "";
    String bindVariableName = null;
    KnoxShellTable table = null;
    if (!args.isEmpty()) {
        bindVariableName = getBindingVariableNameForResultingTable(args);
    }
    String dsName = (String) getVariables().get(KNOXDATASOURCE);
    @SuppressWarnings("unchecked") Map<String, KnoxDataSource> dataSources = getDataSources();
    KnoxDataSource ds = null;
    if (dsName == null || dsName.isEmpty()) {
        if (dataSources == null || dataSources.isEmpty()) {
            return "please configure a datasource with ':datasources add {name} {connectStr} {driver} {authntype: none|basic}'.";
        } else if (dataSources.size() == 1) {
            dsName = (String) dataSources.keySet().toArray()[0];
        } else {
            return "mulitple datasources configured. please disambiguate with ':datasources select {name}'.";
        }
    }
    sqlHistory = getSQLHistory(dsName);
    historyIndex = (sqlHistory != null && !sqlHistory.isEmpty()) ? sqlHistory.size() - 1 : -1;
    ds = dataSources.get(dsName);
    if (ds != null) {
        JLabel jl = new JLabel("Query: ");
        sqlField = new JTextArea(5, 40);
        sqlField.addKeyListener(this);
        sqlField.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(sqlField);
        Box box = Box.createHorizontalBox();
        box.add(jl);
        box.add(scrollPane);
        // JDK-5018574 : Unable to set focus to another component in JOptionPane
        SwingUtils.workAroundFocusIssue(sqlField);
        int x = JOptionPane.showConfirmDialog(null, box, "SQL Query Input", JOptionPane.OK_CANCEL_OPTION);
        if (x == JOptionPane.OK_OPTION) {
            ok = true;
            sql = sqlField.getText();
            addToSQLHistory(dsName, sql);
            historyIndex = -1;
        }
        // KnoxShellTable.builder().jdbc().connect("jdbc:derby:codejava/webdb1").driver("org.apache.derby.jdbc.EmbeddedDriver").username("lmccay").pwd("xxxx").sql("SELECT * FROM book");
        try {
            if (ok) {
                System.out.println(sql);
                try {
                    Connection conn = getConnectionFromSession(ds);
                    if (conn == null || conn.isClosed()) {
                        String username = null;
                        char[] pass = null;
                        if (ds.getAuthnType().equalsIgnoreCase("basic")) {
                            CredentialCollector dlg = login();
                            username = dlg.name();
                            pass = dlg.chars();
                        }
                        conn = getConnection(ds, username, new String(pass));
                    }
                    try (Statement statement = conn.createStatement()) {
                        if (statement.execute(sql)) {
                            try (ResultSet resultSet = statement.getResultSet()) {
                                table = KnoxShellTable.builder().jdbc().resultSet(resultSet);
                            }
                        }
                    }
                } catch (SQLException e) {
                    System.out.println("SQL Exception encountered... " + e.getMessage());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        return "please select a datasource via ':datasources select {name}'.";
    }
    if (table != null && bindVariableName != null) {
        getVariables().put(bindVariableName, table);
    }
    return table;
}
Also used : JScrollPane(javax.swing.JScrollPane) JTextArea(javax.swing.JTextArea) KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JLabel(javax.swing.JLabel) Box(javax.swing.Box) SQLException(java.sql.SQLException) CredentialCollector(org.apache.knox.gateway.shell.CredentialCollector) ResultSet(java.sql.ResultSet) KnoxDataSource(org.apache.knox.gateway.shell.KnoxDataSource)

Example 2 with CredentialCollector

use of org.apache.knox.gateway.shell.CredentialCollector in project knox by apache.

the class WebHDFSCommand method establishSession.

private KnoxSession establishSession(String mountPoint, String url) {
    CredentialCollector dlg;
    try {
        dlg = login();
    } catch (CredentialCollectionException e) {
        e.printStackTrace();
        return null;
    }
    String username = dlg.name();
    String password = new String(dlg.chars());
    KnoxSession session = null;
    try {
        session = KnoxSession.login(url, username, password);
        sessions.put(mountPoint, session);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return session;
}
Also used : CredentialCollector(org.apache.knox.gateway.shell.CredentialCollector) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) URISyntaxException(java.net.URISyntaxException)

Example 3 with CredentialCollector

use of org.apache.knox.gateway.shell.CredentialCollector in project knox by apache.

the class DataSourceCommand method execute.

@SuppressWarnings({ "unchecked", "PMD.CloseResource" })
@Override
public Object execute(List<String> args) {
    Map<String, KnoxDataSource> dataSources = getDataSources();
    if (args.isEmpty()) {
        args.add("list");
    }
    if (args.get(0).equalsIgnoreCase("add")) {
        KnoxDataSource ds = new KnoxDataSource(args.get(1), args.get(2), args.get(3), args.get(4));
        dataSources.put(ds.getName(), ds);
        getVariables().put(KNOXDATASOURCES, dataSources);
        persistDataSources();
    } else if (args.get(0).equalsIgnoreCase("remove")) {
        if (dataSources == null || dataSources.isEmpty()) {
            return "No datasources to remove.";
        }
        // if the removed datasource is currently selected, unselect it
        dataSources.remove(args.get(1));
        if (getVariables().get(KNOXDATASOURCE) != null) {
            if (args.get(1) != null) {
                if (((String) getVariables().get(KNOXDATASOURCE)).equals(args.get(1))) {
                    System.out.println("unselecting datasource.");
                    getVariables().put(KNOXDATASOURCE, "");
                }
            } else {
                System.out.println("Missing datasource name to remove.");
            }
        }
        getVariables().put(KNOXDATASOURCES, dataSources);
        persistDataSources();
    } else if (args.get(0).equalsIgnoreCase("list")) {
    // valid command no additional work needed though
    } else if (args.get(0).equalsIgnoreCase("select")) {
        if (dataSources == null || dataSources.isEmpty()) {
            return "No datasources to select from.";
        }
        KnoxDataSource dsValue = dataSources.get(args.get(1));
        Connection conn = getConnectionFromSession(dsValue);
        try {
            if (conn == null || conn.isClosed()) {
                String username = null;
                char[] pass = null;
                if (dsValue.getAuthnType().equalsIgnoreCase("basic")) {
                    CredentialCollector dlg;
                    try {
                        dlg = login();
                    } catch (CredentialCollectionException e) {
                        e.printStackTrace();
                        return "Error: Credential collection failure.";
                    }
                    username = dlg.name();
                    pass = dlg.chars();
                }
                try {
                    getConnection(dsValue, username, new String(pass));
                } catch (Exception e) {
                    e.printStackTrace();
                    return "Error: Connection creation failure.";
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (dataSources.containsKey(args.get(1))) {
            getVariables().put(KNOXDATASOURCE, args.get(1));
        }
        KnoxShellTable datasource = new KnoxShellTable();
        datasource.title("Knox DataSource Selected");
        datasource.header("Name").header("Connect String").header("Driver").header("Authn Type");
        datasource.row().value(dsValue.getName()).value(dsValue.getConnectStr()).value(dsValue.getDriver()).value(dsValue.getAuthnType());
        return datasource;
    } else {
        return "ERROR: unknown datasources command.";
    }
    return buildTable();
}
Also used : KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) SQLException(java.sql.SQLException) CredentialCollector(org.apache.knox.gateway.shell.CredentialCollector) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException) Connection(java.sql.Connection) KnoxDataSource(org.apache.knox.gateway.shell.KnoxDataSource) SQLException(java.sql.SQLException) CredentialCollectionException(org.apache.knox.gateway.shell.CredentialCollectionException)

Aggregations

CredentialCollector (org.apache.knox.gateway.shell.CredentialCollector)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 CredentialCollectionException (org.apache.knox.gateway.shell.CredentialCollectionException)2 KnoxDataSource (org.apache.knox.gateway.shell.KnoxDataSource)2 KnoxShellTable (org.apache.knox.gateway.shell.table.KnoxShellTable)2 URISyntaxException (java.net.URISyntaxException)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Box (javax.swing.Box)1 JLabel (javax.swing.JLabel)1 JScrollPane (javax.swing.JScrollPane)1 JTextArea (javax.swing.JTextArea)1 KnoxSession (org.apache.knox.gateway.shell.KnoxSession)1