Search in sources :

Example 1 with BasicDB

use of com.tremolosecurity.provisioning.core.providers.BasicDB in project OpenUnison by TremoloSecurity.

the class LoadFromDatabaseTarget method validate.

@Override
public String validate(String value, HttpFilterRequest request) throws Exception {
    BasicDB db = (BasicDB) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
    Connection con = db.getDS().getConnection();
    try {
        PreparedStatement stmt = con.prepareStatement(this.exactSQL);
        stmt.setString(1, value);
        ResultSet rs = stmt.executeQuery();
        String error = null;
        if (!rs.next()) {
            error = this.errorMessage;
        }
        rs.close();
        stmt.close();
        return error;
    } catch (SQLException sqlException) {
        logger.error("Unable to validate against '" + this.exactSQL + "'");
        throw sqlException;
    } finally {
        if (con != null) {
            con.close();
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) BasicDB(com.tremolosecurity.provisioning.core.providers.BasicDB)

Example 2 with BasicDB

use of com.tremolosecurity.provisioning.core.providers.BasicDB in project OpenUnison by TremoloSecurity.

the class LoadFromDatabaseTarget method getSourceList.

@Override
public List<NVP> getSourceList(HttpFilterRequest request) throws Exception {
    if (request.getParameter("search") == null) {
        BasicDB db = (BasicDB) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
        Connection con = db.getDS().getConnection();
        try {
            ArrayList<NVP> toReturn = new ArrayList<NVP>();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(noParamSQL);
            while (rs.next()) {
                toReturn.add(new NVP(rs.getString(nameField), rs.getString(valueField)));
                if (this.maxEntries > 0 && toReturn.size() > this.maxEntries) {
                    rs.close();
                    stmt.close();
                    break;
                }
            }
            return toReturn;
        } finally {
            if (con != null) {
                con.close();
            }
        }
    } else {
        BasicDB db = (BasicDB) GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getTarget(this.targetName).getProvider();
        Connection con = db.getDS().getConnection();
        try {
            ArrayList<NVP> toReturn = new ArrayList<NVP>();
            PreparedStatement stmt = con.prepareStatement(this.paramSQL);
            stmt.setString(1, "%" + request.getParameter("search").getValues().get(0) + "%");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                toReturn.add(new NVP(rs.getString(nameField), rs.getString(valueField)));
                if (this.maxEntries > 0 && toReturn.size() > this.maxEntries) {
                    rs.close();
                    stmt.close();
                    break;
                }
            }
            return toReturn;
        } finally {
            if (con != null) {
                con.close();
            }
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) NVP(com.tremolosecurity.util.NVP) PreparedStatement(java.sql.PreparedStatement) BasicDB(com.tremolosecurity.provisioning.core.providers.BasicDB)

Example 3 with BasicDB

use of com.tremolosecurity.provisioning.core.providers.BasicDB in project OpenUnison by TremoloSecurity.

the class DBTargetDynamicWF method generateWorkflows.

@Override
public List<Map<String, String>> generateWorkflows(WorkflowType wf, ConfigManager cfg, HashMap<String, Attribute> params) throws ProvisioningException {
    String target = params.get("target").getValues().get(0);
    String SQL = params.get("sql").getValues().get(0);
    Connection con = null;
    ResultSet rs = null;
    Statement s = null;
    try {
        con = ((BasicDB) cfg.getProvisioningEngine().getTarget(target).getProvider()).getDS().getConnection();
        s = con.createStatement();
        rs = s.executeQuery(SQL);
        List<Map<String, String>> wfParams = new ArrayList<Map<String, String>>();
        while (rs.next()) {
            HashMap<String, String> row = new HashMap<String, String>();
            for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                if (rs.getObject(rs.getMetaData().getColumnLabel(i + 1)) != null) {
                    row.put(rs.getMetaData().getColumnLabel(i + 1), rs.getObject(rs.getMetaData().getColumnLabel(i + 1)).toString());
                }
            }
            wfParams.add(row);
        }
        return wfParams;
    } catch (SQLException e) {
        throw new ProvisioningException("Could not get workflow data", e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
        }
        if (s != null) {
            try {
                s.close();
            } catch (SQLException e) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) BasicDB(com.tremolosecurity.provisioning.core.providers.BasicDB) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

BasicDB (com.tremolosecurity.provisioning.core.providers.BasicDB)3 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)1 NVP (com.tremolosecurity.util.NVP)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1