Search in sources :

Example 66 with Subscription

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

the class PolicyDAOImpl method getSubscriptionPolicies.

@Override
public List<SubscriptionPolicy> getSubscriptionPolicies() throws APIMgtDAOException {
    try {
        List<SubscriptionPolicy> policyList = new ArrayList<>();
        String sqlQuery = "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";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    policyList.add(createSubscriptionPolicyFromResultSet(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), resultSet));
                }
            }
        }
        return policyList;
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving Subscription policies";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 67 with Subscription

use of org.wso2.carbon.apimgt.core.models.Subscription 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 68 with Subscription

use of org.wso2.carbon.apimgt.core.models.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 69 with Subscription

use of org.wso2.carbon.apimgt.core.models.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 70 with Subscription

use of org.wso2.carbon.apimgt.core.models.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)

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 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22 Response (javax.ws.rs.core.Response)21 Test (org.junit.Test)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