Search in sources :

Example 1 with KnoxShellTable

use of org.apache.knox.gateway.shell.table.KnoxShellTable in project knox by apache.

the class DataSourceCommand method buildTable.

private KnoxShellTable buildTable() {
    KnoxShellTable datasource = new KnoxShellTable();
    datasource.title("Knox DataSources");
    datasource.header("Name").header("Connect String").header("Driver").header("Authn Type");
    @SuppressWarnings("unchecked") Map<String, KnoxDataSource> dataSources = (Map<String, KnoxDataSource>) getVariables().get(KNOXDATASOURCES);
    if (dataSources != null && !dataSources.isEmpty()) {
        for (KnoxDataSource dsValue : dataSources.values()) {
            datasource.row().value(dsValue.getName()).value(dsValue.getConnectStr()).value(dsValue.getDriver()).value(dsValue.getAuthnType());
        }
    }
    return datasource;
}
Also used : KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) KnoxDataSource(org.apache.knox.gateway.shell.KnoxDataSource) Map(java.util.Map)

Example 2 with KnoxShellTable

use of org.apache.knox.gateway.shell.table.KnoxShellTable 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 3 with KnoxShellTable

use of org.apache.knox.gateway.shell.table.KnoxShellTable in project knox by apache.

the class WebHDFSCommand method listStatus.

private Object listStatus(Map<String, String> mounts, String path) {
    Object response = null;
    try {
        String directory;
        String mountPoint = determineMountPoint(path);
        if (mountPoint != null) {
            KnoxSession session = getSessionForMountPoint(mounts, mountPoint);
            if (session != null) {
                directory = determineTargetPath(path, mountPoint);
                String json = Hdfs.ls(session).dir(directory).now().getString();
                Map<String, HashMap<String, ArrayList<HashMap<String, String>>>> map = JsonUtils.getFileStatusesAsMap(json);
                if (map != null) {
                    ArrayList<HashMap<String, String>> list = map.get("FileStatuses").get("FileStatus");
                    KnoxShellTable table = buildTableFromListStatus(directory, list);
                    response = table;
                }
            } else {
                response = "No session established for mountPoint: " + mountPoint + " Use :fs mount {topology-url} {mountpoint-name}";
            }
        } else {
            response = "No mountpoint found. Use ':fs mount {topologyURL} {mountpoint}'.";
        }
    } catch (KnoxShellException | IOException e) {
        response = "Exception ocurred: " + e.getMessage();
        e.printStackTrace();
    }
    return response;
}
Also used : KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) HashMap(java.util.HashMap) KnoxSession(org.apache.knox.gateway.shell.KnoxSession) IOException(java.io.IOException) KnoxShellException(org.apache.knox.gateway.shell.KnoxShellException)

Example 4 with KnoxShellTable

use of org.apache.knox.gateway.shell.table.KnoxShellTable in project knox by apache.

the class CSVCommand method execute.

@SuppressWarnings("unchecked")
@Override
public Object execute(List<String> args) {
    KnoxShellTable table = null;
    String bindVariableName = null;
    if (!args.isEmpty()) {
        bindVariableName = getBindingVariableNameForResultingTable(args);
    }
    if (args.get(0).contentEquals("withHeaders")) {
        withHeaders = true;
        url = args.get(1);
    } else {
        url = args.get(0);
    }
    try {
        if (withHeaders) {
            if (url.startsWith("$")) {
                // a knoxshell variable is a csv file as a string
                String csvString = (String) getVariables().get(url.substring(1));
                table = KnoxShellTable.builder().csv().withHeaders().string(csvString);
            } else {
                table = KnoxShellTable.builder().csv().withHeaders().url(url);
            }
        } else {
            if (url.startsWith("$")) {
                // a knoxshell variable is a csv file as a string
                String csvString = (String) getVariables().get(url.substring(1));
                table = KnoxShellTable.builder().csv().string(csvString);
            } else {
                table = KnoxShellTable.builder().csv().url(url);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (table != null && bindVariableName != null) {
        getVariables().put(bindVariableName, table);
    }
    return table;
}
Also used : KnoxShellTable(org.apache.knox.gateway.shell.table.KnoxShellTable) IOException(java.io.IOException)

Example 5 with KnoxShellTable

use of org.apache.knox.gateway.shell.table.KnoxShellTable 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

KnoxShellTable (org.apache.knox.gateway.shell.table.KnoxShellTable)8 SQLException (java.sql.SQLException)3 KnoxDataSource (org.apache.knox.gateway.shell.KnoxDataSource)3 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 CredentialCollectionException (org.apache.knox.gateway.shell.CredentialCollectionException)2 CredentialCollector (org.apache.knox.gateway.shell.CredentialCollector)2 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 Map (java.util.Map)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 KnoxShellException (org.apache.knox.gateway.shell.KnoxShellException)1