Search in sources :

Example 31 with ApplicationPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getSubscriptionPolicyById.

/**
 * Retrieves {@link SubscriptionPolicy} with policy uuid <code>uuid</code>
 * <p>This will retrieve complete details about the ApplicationPolicy with all pipelins and conditions.</p>
 *
 * @param uuid uuid of the policy to retrieve from the database
 * @return {@link SubscriptionPolicy}
 */
private SubscriptionPolicy getSubscriptionPolicyById(String uuid) throws SQLException, APIMgtDAOException {
    final String query = "SELECT NAME, UUID, 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 UUID = ?";
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement statement = conn.prepareStatement(query)) {
        statement.setString(1, uuid);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            if (rs.next()) {
                return createSubscriptionPolicyFromResultSet(uuid, rs);
            } else {
                // not found
                String msg = "Subscription Policy not found for id: " + uuid;
                log.warn(msg);
                throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
            }
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 32 with ApplicationPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method updateApplicationPolicy.

/**
 * updates an existing Application Policy
 *
 * @param applicationPolicy {@link Policy} instance
 * @param connection DB Connection instance
 * @throws SQLException if an error occurs while updating the API policy
 * @throws APIMgtDAOException if the uuid of the policy is not available
 */
private void updateApplicationPolicy(Policy applicationPolicy, Connection connection) throws SQLException {
    final String query = "UPDATE AM_APPLICATION_POLICY SET NAME = ?, DISPLAY_NAME = ?, DESCRIPTION = ?, " + "QUOTA_TYPE = ?, UNIT_TIME = ?, QUOTA = ?, QUOTA_UNIT = ?, TIME_UNIT = ?, LAST_UPDATED_TIME = ? " + "WHERE UUID = ?";
    Limit limit = applicationPolicy.getDefaultQuotaPolicy().getLimit();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, applicationPolicy.getPolicyName());
        statement.setString(2, applicationPolicy.getDisplayName());
        statement.setString(3, applicationPolicy.getDescription());
        statement.setString(4, applicationPolicy.getDefaultQuotaPolicy().getType());
        statement.setInt(5, applicationPolicy.getDefaultQuotaPolicy().getLimit().getUnitTime());
        setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
        statement.setString(8, applicationPolicy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
        statement.setTimestamp(9, Timestamp.valueOf(LocalDateTime.now()));
        statement.setString(10, applicationPolicy.getUuid());
        statement.execute();
    }
}
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)

Example 33 with ApplicationPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method updateApplicationPolicy.

@Override
public void updateApplicationPolicy(ApplicationPolicy policy) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            updateApplicationPolicy(policy, connection);
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "Error in updating Application policy with 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 34 with ApplicationPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getSimplifiedPolicyByLevelAndName.

@Override
public Policy getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel policyLevel, String policyName) throws APIMgtDAOException, APIMgtResourceNotFoundException {
    Policy policy = null;
    final String apiPolicyQuery = "SELECT UUID,NAME FROM AM_API_POLICY WHERE NAME = ?";
    final String applicationPolicyQuery = "SELECT UUID,NAME FROM AM_APPLICATION_POLICY WHERE NAME = ?";
    final String subscriptionPolicyQuery = "SELECT UUID,NAME FROM AM_SUBSCRIPTION_POLICY WHERE NAME = ?";
    try (Connection connection = DAOUtil.getConnection()) {
        if (policyLevel.equals(APIMgtAdminService.PolicyLevel.api)) {
            try (PreparedStatement statement = connection.prepareStatement(apiPolicyQuery)) {
                statement.setString(1, policyName);
                try (ResultSet resultSet = statement.executeQuery()) {
                    if (resultSet.next()) {
                        policy = new APIPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    }
                }
            }
        } else if (policyLevel.equals(APIMgtAdminService.PolicyLevel.application)) {
            try (PreparedStatement statement = connection.prepareStatement(applicationPolicyQuery)) {
                statement.setString(1, policyName);
                try (ResultSet resultSet = statement.executeQuery()) {
                    if (resultSet.next()) {
                        policy = new ApplicationPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    }
                }
            }
        } else {
            try (PreparedStatement statement = connection.prepareStatement(subscriptionPolicyQuery)) {
                statement.setString(1, policyName);
                try (ResultSet resultSet = statement.executeQuery()) {
                    if (resultSet.next()) {
                        policy = new SubscriptionPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    }
                }
            }
        }
    } catch (SQLException e) {
        String msg = "Error while retrieving policies";
        log.error(msg, e);
        throw new APIMgtDAOException(msg, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
    if (policy == null) {
        throw new APIMgtResourceNotFoundException("Policy " + policyLevel + "Couldn't found " + policyName, ExceptionCodes.POLICY_NOT_FOUND);
    }
    return policy;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SQLException(java.sql.SQLException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)

Example 35 with ApplicationPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getApplicationPolicies.

@Override
public List<ApplicationPolicy> getApplicationPolicies() throws APIMgtDAOException {
    try {
        List<ApplicationPolicy> policyList = new ArrayList<>();
        String sqlQuery = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, IS_DEPLOYED from AM_APPLICATION_POLICY";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    ApplicationPolicy applicationPolicy = new ApplicationPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    setCommonPolicyDetails(applicationPolicy, resultSet);
                    policyList.add(applicationPolicy);
                }
            }
        }
        return policyList;
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving Application policies";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)56 Test (org.testng.annotations.Test)30 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)24 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)18 Application (org.wso2.carbon.apimgt.core.models.Application)17 BeforeTest (org.testng.annotations.BeforeTest)13 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)13 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)13 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)10 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)10 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)9 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)9 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)8 ResultSet (java.sql.ResultSet)6