Search in sources :

Example 6 with MySQL

use of org.wso2.carbon.idp.mgt.util.IdPManagementConstants.MySQL in project carbon-apimgt by wso2.

the class DAOFactory method getPolicyDAO.

public static PolicyDAO getPolicyDAO() throws APIMgtDAOException {
    PolicyDAO policyDAO = null;
    try (Connection connection = DAOUtil.getConnection()) {
        String driverName = connection.getMetaData().getDriverName();
        if (driverName.contains(MYSQL) || driverName.contains(H2)) {
            policyDAO = new PolicyDAOImpl();
        } else if (driverName.contains(DB2)) {
        } else if (driverName.contains(MS_SQL) || driverName.contains(MICROSOFT)) {
            policyDAO = new PolicyDAOImpl();
        } else if (driverName.contains(POSTGRE)) {
            policyDAO = new PolicyDAOImpl();
        } else if (driverName.contains(ORACLE)) {
            policyDAO = new PolicyDAOImpl();
        } else {
            throw new APIMgtDAOException("Unhandled DB driver: " + driverName + " detected", ExceptionCodes.APIM_DAO_EXCEPTION);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting PolicyDAO", e);
    }
    setup();
    return policyDAO;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO)

Example 7 with MySQL

use of org.wso2.carbon.idp.mgt.util.IdPManagementConstants.MySQL in project carbon-apimgt by wso2.

the class ApiMgtDAO method getApplicationByName.

/**
 * Fetches an Application by name.
 *
 * @param applicationName Name of the Application
 * @param userId          Name of the User.
 * @param groupId         Group ID
 * @throws APIManagementException
 */
public Application getApplicationByName(String applicationName, String userId, String groupId) throws APIManagementException {
    // mysql> select APP.APPLICATION_ID, APP.NAME, APP.SUBSCRIBER_ID,APP.APPLICATION_TIER,APP.CALLBACK_URL,APP
    // .DESCRIPTION,
    // APP.APPLICATION_STATUS from AM_SUBSCRIBER as SUB,AM_APPLICATION as APP
    // where SUB.user_id='admin' AND APP.name='DefaultApplication' AND SUB.SUBSCRIBER_ID=APP.SUBSCRIBER_ID;
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    int applicationId = 0;
    Application application = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        String query = SQLConstants.GET_APPLICATION_BY_NAME_PREFIX;
        String whereClause = "  WHERE SUB.USER_ID =? AND APP.NAME=? AND SUB.SUBSCRIBER_ID=APP.SUBSCRIBER_ID";
        String whereClauseCaseInSensitive = "  WHERE LOWER(SUB.USER_ID) =LOWER(?) AND APP.NAME=? AND SUB" + "" + ".SUBSCRIBER_ID=APP.SUBSCRIBER_ID";
        String whereClauseWithGroupId = "  WHERE  (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL)" + " AND SUB.USER_ID = ?)) AND " + "APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        String whereClauseWithGroupIdCaseInSensitive = "  WHERE  (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL)" + " AND LOWER(SUB.USER_ID) = LOWER(?))) AND " + "APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        String whereClauseWithMultiGroupId = "  WHERE  ((APP.APPLICATION_ID IN (SELECT APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))  OR   SUB.USER_ID = ? " + "OR (APP.APPLICATION_ID IN (SELECT APPLICATION_ID FROM AM_APPLICATION WHERE GROUP_ID = ?))) " + "AND APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        String whereClauseWithMultiGroupIdCaseInSensitive = "  WHERE  ((APP.APPLICATION_ID IN (SELECT APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))  " + "OR   LOWER(SUB.USER_ID) = LOWER(?)  " + "OR (APP.APPLICATION_ID IN (SELECT APPLICATION_ID FROM AM_APPLICATION WHERE GROUP_ID = " + "?))) " + "AND APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        if (groupId != null && !"null".equals(groupId) && !groupId.isEmpty()) {
            if (multiGroupAppSharingEnabled) {
                Subscriber subscriber = getSubscriber(userId);
                String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
                if (forceCaseInsensitiveComparisons) {
                    query = query + whereClauseWithMultiGroupIdCaseInSensitive;
                } else {
                    query = query + whereClauseWithMultiGroupId;
                }
                String[] groupIds = groupId.split(",");
                int parameterIndex = groupIds.length;
                prepStmt = fillQueryParams(connection, query, groupIds, 1);
                prepStmt.setString(++parameterIndex, tenantDomain);
                prepStmt.setString(++parameterIndex, userId);
                prepStmt.setString(++parameterIndex, tenantDomain + '/' + groupId);
                prepStmt.setString(++parameterIndex, applicationName);
            } else {
                if (forceCaseInsensitiveComparisons) {
                    query = query + whereClauseWithGroupIdCaseInSensitive;
                } else {
                    query = query + whereClauseWithGroupId;
                }
                prepStmt = connection.prepareStatement(query);
                prepStmt.setString(1, groupId);
                prepStmt.setString(2, userId);
                prepStmt.setString(3, applicationName);
            }
        } else {
            if (forceCaseInsensitiveComparisons) {
                query = query + whereClauseCaseInSensitive;
            } else {
                query = query + whereClause;
            }
            prepStmt = connection.prepareStatement(query);
            prepStmt.setString(1, userId);
            prepStmt.setString(2, applicationName);
        }
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            String subscriberId = rs.getString("SUBSCRIBER_ID");
            String subscriberName = rs.getString("USER_ID");
            Subscriber subscriber = new Subscriber(subscriberName);
            subscriber.setId(Integer.parseInt(subscriberId));
            application = new Application(applicationName, subscriber);
            application.setOwner(rs.getString("CREATED_BY"));
            application.setDescription(rs.getString("DESCRIPTION"));
            application.setStatus(rs.getString("APPLICATION_STATUS"));
            application.setCallbackUrl(rs.getString("CALLBACK_URL"));
            applicationId = rs.getInt("APPLICATION_ID");
            application.setId(applicationId);
            application.setTier(rs.getString("APPLICATION_TIER"));
            application.setUUID(rs.getString("UUID"));
            application.setGroupId(rs.getString("GROUP_ID"));
            application.setOwner(rs.getString("CREATED_BY"));
            application.setTokenType(rs.getString("TOKEN_TYPE"));
            if (multiGroupAppSharingEnabled) {
                setGroupIdInApplication(connection, application);
            }
            if (application != null) {
                Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
                application.setApplicationAttributes(applicationAttributes);
            }
        }
    } catch (SQLException e) {
        handleException("Error while obtaining details of the Application : " + applicationName, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
    }
    return application;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 8 with MySQL

use of org.wso2.carbon.idp.mgt.util.IdPManagementConstants.MySQL in project carbon-apimgt by wso2.

the class SQLConstantManagerFactory method initializeSQLConstantManager.

/**
 * This method initialize when server start up. And select relevant DB dirver and load the const class.
 * @throws APIManagementException
 */
public static void initializeSQLConstantManager() throws APIManagementException {
    Connection connection = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        if (connection.getMetaData().getDriverName().contains("MySQL") || connection.getMetaData().getDriverName().contains("H2")) {
            sqlConstantsH2MySQL = new SQLConstantsH2MySQL();
            dbType = "h2mysql";
        } else if (connection.getMetaData().getDatabaseProductName().contains("DB2")) {
            sqlConstantsDB2 = new SQLConstantsDB2();
            dbType = "db2";
        } else if (connection.getMetaData().getDriverName().contains("MS SQL")) {
            dbType = "mssql";
            sqlConstantsMSSQL = new SQLConstantsMSSQL();
        } else if (connection.getMetaData().getDriverName().contains("Microsoft")) {
            dbType = "mssql";
            sqlConstantsMSSQL = new SQLConstantsMSSQL();
        } else if (connection.getMetaData().getDriverName().contains("PostgreSQL")) {
            dbType = "postgre";
            sqlConstantPostgreSQL = new SQLConstantPostgreSQL();
        } else if (connection.getMetaData().getDriverName().contains("Oracle")) {
            dbType = "oracle";
            sqlConstantOracle = new SQLConstantOracle();
        } else {
            log.error("Could not find DB type to load constants");
            throw new APIManagementException("Error occurred while initializing SQL Constants Manager");
        }
    } catch (SQLException e) {
        log.error("Error occurred while initializeSQLConstantManager");
        throw new APIManagementException("Error occurred while initializing SQL Constants Manager", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(null, connection, null);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 9 with MySQL

use of org.wso2.carbon.idp.mgt.util.IdPManagementConstants.MySQL in project carbon-business-process by wso2.

the class AbstractJPAVendorAdapter method determineDbType.

protected DatabaseType determineDbType() {
    Connection con = null;
    DatabaseType dbType = null;
    try {
        con = getDBConnection();
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData != null) {
            String dbProductName = metaData.getDatabaseProductName().toLowerCase();
            int dbMajorVer = metaData.getDatabaseMajorVersion();
            if (log.isDebugEnabled()) {
                log.debug("Using database " + dbProductName + " major version " + dbMajorVer);
            }
            if (dbProductName.contains("db2")) {
                dbType = DatabaseType.DB2;
            } else if (dbProductName.contains("oracle")) {
                dbType = DatabaseType.ORACLE;
            } else if (dbProductName.contains("derby")) {
                dbType = DatabaseType.DERBY;
            } else if (dbProductName.contains("h2")) {
                dbType = DatabaseType.H2;
            } else if (dbProductName.contains("hsql")) {
                dbType = DatabaseType.HSQL;
            } else if (dbProductName.contains("microsoft sql")) {
                dbType = DatabaseType.SQL_SERVER;
            } else if (dbProductName.contains("mysql")) {
                dbType = DatabaseType.MYSQL;
            } else if (dbProductName.contains("postgresql")) {
                dbType = DatabaseType.POSTGRESQL;
            } else if (dbProductName.contains("sybase")) {
                dbType = DatabaseType.SYBASE;
            }
        }
    } catch (SQLException e) {
        log.warn("Unable to determine database dialect.", e);
    } finally {
        close(con);
    }
    return dbType;
}
Also used : DatabaseType(org.wso2.carbon.bpel.b4p.coordination.dao.DatabaseType) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 10 with MySQL

use of org.wso2.carbon.idp.mgt.util.IdPManagementConstants.MySQL in project carbon-business-process by wso2.

the class AbstractJPAVendorAdapter method determineDbType.

protected DatabaseType determineDbType() {
    Connection con = null;
    DatabaseType dbType = null;
    try {
        con = getDBConnection();
        DatabaseMetaData metaData = con.getMetaData();
        if (metaData != null) {
            String dbProductName = metaData.getDatabaseProductName().toLowerCase();
            int dbMajorVer = metaData.getDatabaseMajorVersion();
            if (log.isDebugEnabled()) {
                log.debug("Using database " + dbProductName + " major version " + dbMajorVer);
            }
            if (dbProductName.contains("db2")) {
                dbType = DatabaseType.DB2;
            } else if (dbProductName.contains("oracle")) {
                dbType = DatabaseType.ORACLE;
            } else if (dbProductName.contains("derby")) {
                dbType = DatabaseType.DERBY;
            } else if (dbProductName.contains("h2")) {
                dbType = DatabaseType.H2;
            } else if (dbProductName.contains("hsql")) {
                dbType = DatabaseType.HSQL;
            } else if (dbProductName.contains("microsoft sql")) {
                dbType = DatabaseType.SQL_SERVER;
            } else if (dbProductName.contains("mysql")) {
                dbType = DatabaseType.MYSQL;
            } else if (dbProductName.contains("postgresql")) {
                dbType = DatabaseType.POSTGRESQL;
            } else if (dbProductName.contains("sybase")) {
                dbType = DatabaseType.SYBASE;
            }
        }
    } catch (SQLException e) {
        log.warn("Unable to determine database dialect.", e);
    } finally {
        close(con);
    }
    return dbType;
}
Also used : DatabaseType(org.wso2.carbon.humantask.core.dao.DatabaseType) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

SQLException (java.sql.SQLException)21 Connection (java.sql.Connection)19 PreparedStatement (java.sql.PreparedStatement)10 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 ResultSet (java.sql.ResultSet)9 ArrayList (java.util.ArrayList)6 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)4 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)4 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)4 Timestamp (java.sql.Timestamp)3 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)3 IOException (java.io.IOException)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)2 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)2 ServiceProvider (org.wso2.carbon.identity.application.common.model.ServiceProvider)2 NamedPreparedStatement (org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)2 WorkflowRequest (org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest)2 InternalWorkflowException (org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1