Search in sources :

Example 66 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getAllPolicies.

@Override
public Set<PolicyValidationData> getAllPolicies() throws APIMgtDAOException {
    Set<PolicyValidationData> policyValidationDataList = new LinkedHashSet();
    final String apiPolicyQuery = "SELECT UUID,NAME FROM AM_API_POLICY";
    final String applicationPolicyQuery = "SELECT UUID,NAME FROM AM_APPLICATION_POLICY";
    final String subscriptionPolicyQuery = "SELECT UUID,NAME,STOP_ON_QUOTA_REACH FROM AM_SUBSCRIPTION_POLICY";
    try (Connection connection = DAOUtil.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            try (ResultSet resultSet = statement.executeQuery(apiPolicyQuery)) {
                while (resultSet.next()) {
                    policyValidationDataList.add(new PolicyValidationData(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), false));
                }
            }
            try (ResultSet resultSet = statement.executeQuery(applicationPolicyQuery)) {
                while (resultSet.next()) {
                    policyValidationDataList.add(new PolicyValidationData(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), false));
                }
            }
            try (ResultSet resultSet = statement.executeQuery(subscriptionPolicyQuery)) {
                while (resultSet.next()) {
                    policyValidationDataList.add(new PolicyValidationData(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH)));
                }
            }
        }
    } catch (SQLException e) {
        String msg = "Error while retrieving policies";
        log.error(msg, e);
        throw new APIMgtDAOException(msg, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
    return policyValidationDataList;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 67 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class TagDAOImpl method getTags.

@Override
public List<Tag> getTags() throws APIMgtDAOException {
    final String query = "SELECT NAME, COUNT FROM AM_TAGS";
    List<Tag> tags = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try (ResultSet rs = statement.executeQuery()) {
            while (rs.next()) {
                Tag tag = new Tag.Builder().name(rs.getString("NAME")).count(rs.getInt("COUNT")).build();
                tags.add(tag);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting tags", e);
    }
    return tags;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Tag(org.wso2.carbon.apimgt.core.models.Tag)

Example 68 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class ThreatProtectionDAOImpl method updatePolicy.

/**
 * Update a threat protection policy
 * @param policy Policy to be updated
 * @param connection SQL Connection
 * @throws APIMgtDAOException If failed to update policy
 */
private void updatePolicy(ThreatProtectionPolicy policy, Connection connection) throws APIMgtDAOException {
    final String sqlQuery = "UPDATE " + THREAT_PROTECTION_TABLE + " SET NAME = ?, " + "TYPE = ?, " + "POLICY = ? " + "WHERE UUID = ?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, policy.getName());
        preparedStatement.setString(2, policy.getType());
        preparedStatement.setBytes(3, policy.getPolicy().getBytes("UTF-8"));
        preparedStatement.setString(4, policy.getUuid());
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        String errorMsg = "Error updating Threat Protection policy";
        throw new APIMgtDAOException(errorMsg, e);
    } catch (UnsupportedEncodingException e) {
        String errorMsg = "Charset error in threat protection policy";
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Example 69 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getSubscriptionPolicy.

@Override
public SubscriptionPolicy getSubscriptionPolicy(String policyName) throws APIMgtDAOException {
    try {
        final String query = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED, RATE_LIMIT_COUNT, RATE_LIMIT_TIME_UNIT, " + "STOP_ON_QUOTA_REACH, BILLING_PLAN FROM AM_SUBSCRIPTION_POLICY WHERE NAME = ?";
        try (Connection conn = DAOUtil.getConnection();
            PreparedStatement statement = conn.prepareStatement(query)) {
            statement.setString(1, policyName);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                if (rs.next()) {
                    return createSubscriptionPolicyFromResultSet(policyName, rs);
                } else {
                    // not found
                    String msg = "Subscription Policy not found for name: " + policyName;
                    log.warn(msg);
                    throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
                }
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving Subscription policy for name: " + policyName;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 70 with APIMgtDAOException

use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getBlockConditions.

@Override
public List<BlockConditions> getBlockConditions() throws APIMgtDAOException {
    List<BlockConditions> blockConditionsList = new ArrayList<BlockConditions>();
    String query = "SELECT CONDITION_ID, TYPE, VALUE, ENABLED, AM_BLOCK_CONDITIONS.UUID, STARTING_IP, ENDING_IP " + "FROM AM_BLOCK_CONDITIONS LEFT JOIN AM_IP_RANGE_CONDITION ON " + "AM_BLOCK_CONDITIONS.UUID = AM_IP_RANGE_CONDITION.UUID";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement selectPreparedStatement = connection.prepareStatement(query)) {
        connection.setAutoCommit(true);
        try (ResultSet resultSet = selectPreparedStatement.executeQuery()) {
            while (resultSet.next()) {
                BlockConditions blockConditions = new BlockConditions();
                blockConditions.setEnabled(resultSet.getBoolean("ENABLED"));
                blockConditions.setConditionType(resultSet.getString("TYPE"));
                blockConditions.setConditionValue(resultSet.getString("VALUE"));
                blockConditions.setConditionId(resultSet.getInt("CONDITION_ID"));
                blockConditions.setUuid(resultSet.getString("UUID"));
                blockConditions.setStartingIP(resultSet.getString("STARTING_IP"));
                blockConditions.setEndingIP(resultSet.getString("ENDING_IP"));
                blockConditionsList.add(blockConditions);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting block conditions", e);
    }
    return blockConditionsList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)333 SQLException (java.sql.SQLException)190 Connection (java.sql.Connection)144 PreparedStatement (java.sql.PreparedStatement)127 Test (org.testng.annotations.Test)84 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)72 ResultSet (java.sql.ResultSet)70 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)57 API (org.wso2.carbon.apimgt.core.models.API)57 ArrayList (java.util.ArrayList)49 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)35 Application (org.wso2.carbon.apimgt.core.models.Application)24 HashMap (java.util.HashMap)23 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)22 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)21 BeforeTest (org.testng.annotations.BeforeTest)21 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)20 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)20 IOException (java.io.IOException)19 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)17