Search in sources :

Example 16 with MySQL

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

the class MysqlSQLStatements method prepareAttributeSearchStatementForStore.

/**
 * @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
 * Map, int, int)
 */
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
    StringBuilder roleListBuilder = new StringBuilder();
    roleListBuilder.append("?");
    for (int i = 0; i < roles.size() - 1; i++) {
        roleListBuilder.append(",?");
    }
    StringBuilder searchQuery = new StringBuilder();
    Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        searchQuery.append("LOWER(");
        if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
            searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
        } else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
            searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
        } else {
            searchQuery.append(entry.getKey());
        }
        searchQuery.append(") LIKE ?");
        if (entries.hasNext()) {
            searchQuery.append(" AND ");
        }
    }
    // retrieve the attribute applicable for the search
    String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
    // get the corresponding implementation based on the attribute to be searched
    String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
    query = "Select * from ( " + query + " ) A" + getStoreAPIsByLabelJoinQuery(labels);
    try {
        int queryIndex = 1;
        PreparedStatement statement = connection.prepareStatement(query);
        // include the attribute in the query (for APIs with public visibility)
        for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
            statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
            queryIndex++;
        }
        // include user roles in the query
        for (String role : roles) {
            statement.setString(queryIndex, role);
            queryIndex++;
        }
        // include the attribute in the query (for APIs with restricted visibility)
        for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
            statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
            queryIndex++;
        }
        for (String label : labels) {
            statement.setString(queryIndex, label);
            queryIndex++;
        }
        // setting 0 as the default offset based on store-api.yaml and MySQL specifications
        statement.setInt(queryIndex, (offset < 0) ? 0 : offset);
        statement.setInt(++queryIndex, limit);
        return statement;
    } catch (SQLException e) {
        String errorMsg = "Error occurred while searching APIs for attributes in the database.";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 17 with MySQL

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

the class DAOFactory method getWorkflowDAO.

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

Example 18 with MySQL

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

the class DAOFactory method getApplicationDAO.

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

Example 19 with MySQL

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

the class DAOFactory method getLabelDAO.

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

Example 20 with MySQL

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

the class ApplicationDAOImpl method getApplicationBasicInfo.

@Override
public ApplicationBasicInfo[] getApplicationBasicInfo(String filter, int offset, int limit) throws IdentityApplicationManagementException {
    validateAttributesForPagination(offset, limit);
    if ("*".equals(filter)) {
        return getApplicationBasicInfo(offset, limit);
    }
    validateAttributesForPagination(offset, limit);
    int tenantID = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    Connection connection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement getAppNamesStmt = null;
    ResultSet appNameResultSet = null;
    String sqlQuery;
    ArrayList<ApplicationBasicInfo> appInfo = new ArrayList<>();
    try {
        String filterResolvedForSQL = resolveSQLFilter(filter);
        String databaseProductName = connection.getMetaData().getDatabaseProductName();
        if (databaseProductName.contains("MySQL") || databaseProductName.contains("MariaDB") || databaseProductName.contains("H2")) {
            sqlQuery = LOAD_APP_NAMES_BY_TENANT_AND_APP_NAME_MYSQL;
            getAppNamesStmt = connection.prepareStatement(sqlQuery);
            populateApplicationSearchQuery(getAppNamesStmt, tenantID, filterResolvedForSQL, offset, limit);
        } else if (databaseProductName.contains("Oracle")) {
            sqlQuery = LOAD_APP_NAMES_BY_TENANT_AND_APP_NAME_ORACLE;
            getAppNamesStmt = connection.prepareStatement(sqlQuery);
            populateApplicationSearchQuery(getAppNamesStmt, tenantID, filterResolvedForSQL, offset + limit, offset);
        } else if (databaseProductName.contains("Microsoft")) {
            sqlQuery = LOAD_APP_NAMES_BY_TENANT_AND_APP_NAME_MSSQL;
            getAppNamesStmt = connection.prepareStatement(sqlQuery);
            populateApplicationSearchQuery(getAppNamesStmt, tenantID, filterResolvedForSQL, offset, limit);
        } else if (databaseProductName.contains("PostgreSQL")) {
            sqlQuery = LOAD_APP_NAMES_BY_TENANT_AND_APP_NAME_POSTGRESQL;
            getAppNamesStmt = connection.prepareStatement(sqlQuery);
            populateApplicationSearchQuery(getAppNamesStmt, tenantID, filterResolvedForSQL, limit, offset);
        } else if (databaseProductName.contains("DB2")) {
            sqlQuery = LOAD_APP_NAMES_BY_TENANT_AND_APP_NAME_DB2SQL;
            getAppNamesStmt = connection.prepareStatement(sqlQuery);
            populateApplicationSearchQuery(getAppNamesStmt, tenantID, filterResolvedForSQL, offset + 1, offset + limit);
        } else if (databaseProductName.contains("INFORMIX")) {
            sqlQuery = LOAD_APP_NAMES_BY_TENANT_AND_APP_NAME_INFORMIX;
            getAppNamesStmt = connection.prepareStatement(sqlQuery);
            getAppNamesStmt.setInt(1, offset);
            getAppNamesStmt.setInt(2, limit);
            getAppNamesStmt.setInt(3, tenantID);
            getAppNamesStmt.setString(4, filterResolvedForSQL);
            getAppNamesStmt.setString(5, LOCAL_SP);
        } else {
            log.error("Error while loading applications from DB: Database driver could not be identified or " + "not supported.");
            throw new IdentityApplicationManagementException("Error while loading applications from DB:" + "Database driver could not be identified or not supported.");
        }
        appNameResultSet = getAppNamesStmt.executeQuery();
        while (appNameResultSet.next()) {
            appInfo.add(buildApplicationBasicInfo(appNameResultSet));
        }
    } catch (SQLException e) {
        throw new IdentityApplicationManagementException("Error while loading applications from DB: " + e.getMessage(), e);
    } finally {
        IdentityApplicationManagementUtil.closeStatement(getAppNamesStmt);
        IdentityApplicationManagementUtil.closeResultSet(appNameResultSet);
        IdentityApplicationManagementUtil.closeConnection(connection);
    }
    return appInfo.toArray(new ApplicationBasicInfo[0]);
}
Also used : SQLException(java.sql.SQLException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)

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