Search in sources :

Example 71 with APIMgtDAOException

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

the class PolicyDAOImpl method getCustomPolicies.

@Override
public List<CustomPolicy> getCustomPolicies() throws APIMgtDAOException {
    List<CustomPolicy> customPolicyList = new ArrayList<>();
    String getQuery = "SELECT NAME, DESCRIPTION, UUID, KEY_TEMPLATE, IS_DEPLOYED, SIDDHI_QUERY FROM " + "AM_CUSTOM_POLICY";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(getQuery)) {
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String siddhiQuery = null;
                CustomPolicy customPolicy = new CustomPolicy(resultSet.getString("NAME"));
                customPolicy.setDescription(resultSet.getString("DESCRIPTION"));
                customPolicy.setUuid(resultSet.getString("UUID"));
                customPolicy.setKeyTemplate(resultSet.getString("KEY_TEMPLATE"));
                customPolicy.setDeployed(resultSet.getBoolean("IS_DEPLOYED"));
                InputStream siddhiQueryBlob = resultSet.getBinaryStream("SIDDHI_QUERY");
                if (siddhiQueryBlob != null) {
                    try {
                        siddhiQuery = IOUtils.toString(siddhiQueryBlob);
                    } catch (IOException e) {
                        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "reading siddhi query stream", e);
                    }
                }
                customPolicy.setSiddhiQuery(siddhiQuery);
                customPolicyList.add(customPolicy);
            }
        }
        return customPolicyList;
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting custom policies", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 72 with APIMgtDAOException

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

the class PolicyDAOImpl method getApiPolicy.

@Override
public APIPolicy getApiPolicy(String policyName) throws APIMgtDAOException {
    try {
        String sqlQuery = "SELECT UUID, NAME, DEFAULT_QUOTA_TYPE, DEFAULT_TIME_UNIT, DEFAULT_UNIT_TIME, " + "DEFAULT_QUOTA, DEFAULT_QUOTA_UNIT, DESCRIPTION, DISPLAY_NAME, IS_DEPLOYED, APPLICABLE_LEVEL " + "from AM_API_POLICY WHERE NAME = ?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
            preparedStatement.setString(1, policyName);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    APIPolicy policy = new APIPolicy(policyName);
                    setCommonPolicyDetails(policy, resultSet);
                    policy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
                    policy.setPipelines(getPipelines(policy.getUuid(), connection));
                    return policy;
                } else {
                    // not found
                    String msg = "API 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 API policy with 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) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 73 with APIMgtDAOException

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

the class PolicyDAOImpl method deletePolicy.

@Override
public void deletePolicy(APIMgtAdminService.PolicyLevel policyLevel, String policyName) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            if (APIMgtAdminService.PolicyLevel.application == policyLevel) {
                deleteApplicationPolicy(policyName, connection);
            } else if (APIMgtAdminService.PolicyLevel.subscription == policyLevel) {
                deleteSubscriptionPolicy(policyName, connection);
            } else if (APIMgtAdminService.PolicyLevel.api == policyLevel) {
                deleteApiPolicy(policyName, connection);
            }
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "Error in deleting throttling policy for level: " + policyLevel + ", policy name: " + policyName;
            log.error(errorMessage, e);
            throw new APIMgtDAOException(errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMsg = "Error in obtaining DB connection";
        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)

Example 74 with APIMgtDAOException

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

the class PolicyDAOImpl method addApiPolicy.

@Override
public void addApiPolicy(APIPolicy policy) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            addApiPolicy(policy, connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "Error in adding API policy, policy name: " + policy.getPolicyName();
            log.error(errorMessage, e);
            throw new APIMgtDAOException(errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMsg = "Error in obtaining DB connection";
        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)

Example 75 with APIMgtDAOException

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

the class PolicyDAOImpl method addApiPolicy.

private static void addApiPolicy(APIPolicy policy, Connection connection) throws SQLException, APIMgtDAOException {
    final String query = "INSERT INTO AM_API_POLICY (UUID, NAME, DISPLAY_NAME, DESCRIPTION, " + "DEFAULT_QUOTA_TYPE, DEFAULT_QUOTA, DEFAULT_QUOTA_UNIT, DEFAULT_UNIT_TIME," + " DEFAULT_TIME_UNIT, APPLICABLE_LEVEL, IS_DEPLOYED) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    Limit limit = policy.getDefaultQuotaPolicy().getLimit();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, policy.getUuid());
        statement.setString(2, policy.getPolicyName());
        statement.setString(3, policy.getDisplayName());
        statement.setString(4, policy.getDescription());
        statement.setString(5, policy.getDefaultQuotaPolicy().getType());
        setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
        statement.setLong(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
        statement.setString(9, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
        statement.setString(10, API_TIER_LEVEL);
        statement.setBoolean(11, policy.isDeployed());
        statement.execute();
        if (policy.getPipelines() != null) {
            addAPIPipeline(connection, policy.getPipelines(), policy.getUuid());
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

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