Search in sources :

Example 71 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addSubscriptionPolicy.

/**
 * Adds an Subscription policy
 *
 * @param policy {@link SubscriptionPolicy} instance
 * @param connection DB Connection instance
 * @throws SQLException if any error occurs while setting default throttle policy related information
 */
private static void addSubscriptionPolicy(SubscriptionPolicy policy, Connection connection) throws SQLException {
    String query;
    query = "INSERT INTO AM_SUBSCRIPTION_POLICY (UUID, NAME, DISPLAY_NAME, DESCRIPTION, QUOTA_TYPE, QUOTA, " + "QUOTA_UNIT, UNIT_TIME, RATE_LIMIT_COUNT, RATE_LIMIT_TIME_UNIT, CUSTOM_ATTRIBUTES, " + "STOP_ON_QUOTA_REACH, BILLING_PLAN, TIME_UNIT, 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.setInt(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
        policy.populateDataInPreparedStatement(statement);
        statement.setString(14, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
        statement.setBoolean(15, policy.isDeployed());
        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 72 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class PolicyDAOImpl method updateSubscriptionPolicy.

/**
 * updates an existing Subscription Policy
 *
 * @param subscriptionPolicy {@link Policy} instance
 * @param connection DB Connection instance
 * @throws APIMgtDAOException if an error occurs while updating the Subscription policy
 */
private void updateSubscriptionPolicy(Policy subscriptionPolicy, Connection connection) throws SQLException {
    final String query = "UPDATE AM_SUBSCRIPTION_POLICY SET NAME = ?, DISPLAY_NAME = ?, DESCRIPTION = ?, QUOTA_TYPE = ?, " + "UNIT_TIME = ?, QUOTA = ?, QUOTA_UNIT = ?, TIME_UNIT = ?, RATE_LIMIT_COUNT = ?, " + "RATE_LIMIT_TIME_UNIT = ?, CUSTOM_ATTRIBUTES = ?, STOP_ON_QUOTA_REACH = ?, " + "BILLING_PLAN = ?, IS_DEPLOYED = ?, LAST_UPDATED_TIME = ? WHERE UUID = ?";
    Limit limit = subscriptionPolicy.getDefaultQuotaPolicy().getLimit();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, subscriptionPolicy.getPolicyName());
        statement.setString(2, subscriptionPolicy.getDisplayName());
        statement.setString(3, subscriptionPolicy.getDescription());
        statement.setString(4, subscriptionPolicy.getDefaultQuotaPolicy().getType());
        statement.setInt(5, subscriptionPolicy.getDefaultQuotaPolicy().getLimit().getUnitTime());
        setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
        statement.setString(8, subscriptionPolicy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
        subscriptionPolicy.populateDataInPreparedStatement(statement);
        statement.setBoolean(14, subscriptionPolicy.isDeployed());
        statement.setTimestamp(15, Timestamp.valueOf(LocalDateTime.now()));
        statement.setString(16, subscriptionPolicy.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 73 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class PolicyDAOImpl method createSubscriptionPolicyFromResultSet.

/**
 * Creates a Subscription Policy from the results set
 *
 * @param identifier policy id
 * @param rs {@link ResultSet} instance
 * @return {@link SubscriptionPolicy} instance
 * @throws SQLException if any error occurs while creating the Subscription Policy from the result set
 * @throws APIMgtDAOException if any error occurs while retrieving custom attributes for this Subscription Policy
 */
private SubscriptionPolicy createSubscriptionPolicyFromResultSet(String identifier, ResultSet rs) throws SQLException, APIMgtDAOException {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(rs.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
    setCommonPolicyDetails(subscriptionPolicy, rs);
    setSubscriptionPolicyDetals(subscriptionPolicy, rs);
    InputStream binary = rs.getBinaryStream(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
    if (binary != null) {
        byte[] customAttrib;
        try {
            customAttrib = IOUtils.toByteArray(binary);
            if (customAttrib.length > 0) {
                subscriptionPolicy.setCustomAttributes(customAttrib);
            }
        } catch (IOException e) {
            String errorMsg = "An Error occurred while retrieving custom attributes for subscription policy with " + "identifier: " + identifier;
            log.error(errorMsg, e);
            throw new APIMgtDAOException(errorMsg, e);
        }
    }
    return subscriptionPolicy;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 74 with Subscription

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addSubscriptionPolicy.

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

use of org.wso2.eventing.Subscription in project carbon-apimgt by wso2.

the class PolicyDAOImpl method updateSubscriptionPolicy.

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

Aggregations

Test (org.testng.annotations.Test)58 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)58 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)37 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)35 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)34 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)34 Application (org.wso2.carbon.apimgt.core.models.Application)30 API (org.wso2.carbon.apimgt.core.models.API)28 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)22 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22 Response (javax.ws.rs.core.Response)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 SQLException (java.sql.SQLException)20 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)18 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)17 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)14