Search in sources :

Example 46 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method populateSubscriptionPolicyList.

private List<SubscriptionPolicy> populateSubscriptionPolicyList(ResultSet resultSet) throws SQLException {
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    if (resultSet != null) {
        while (resultSet.next()) {
            SubscriptionPolicy subscriptionPolicyDTO = new SubscriptionPolicy();
            subscriptionPolicyDTO.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
            subscriptionPolicyDTO.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_POLICY_NAME));
            subscriptionPolicyDTO.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
            subscriptionPolicyDTO.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
            String tenantDomain = APIUtil.getTenantDomainFromTenantId(subscriptionPolicyDTO.getTenantId());
            subscriptionPolicyDTO.setTenantDomain(tenantDomain);
            subscriptionPolicyDTO.setRateLimitCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
            subscriptionPolicyDTO.setRateLimitTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
            subscriptionPolicyDTO.setStopOnQuotaReach(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
            subscriptionPolicyDTO.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
            subscriptionPolicyDTO.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
            setCommonProperties(subscriptionPolicyDTO, resultSet);
            subscriptionPolicies.add(subscriptionPolicyDTO);
        }
    }
    return subscriptionPolicies;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy) ArrayList(java.util.ArrayList)

Example 47 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdGet.

/**
 * Get a specific Subscription Policy by its uuid
 *
 * @param policyId        uuid of the policy
 * @return Matched Subscription Throttle Policy by the given name
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdGet(String policyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy subscriptionPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, subscriptionPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }
        SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(subscriptionPolicy);
        // setting policy permissions
        setPolicyPermissionsToDTO(policyDTO);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while retrieving Subscription level policy: " + policyId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 48 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdPut.

/**
 * Updates a given Subscription level policy specified by uuid
 *
 * @param policyId          u
 * @param body              DTO of policy to be updated
 * @param contentType       Content-Type header
 * @return Updated policy
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdPut(String policyId, String contentType, SubscriptionThrottlePolicyDTO body, MessageContext messageContext) throws APIManagementException {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy existingPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }
        // overridden properties
        body.setPolicyId(policyId);
        body.setPolicyName(existingPolicy.getPolicyName());
        // validate if permission info exists and halt the execution in case of an error
        validatePolicyPermissions(body);
        // update the policy
        SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
        apiProvider.updatePolicy(subscriptionPolicy);
        // update policy permissions
        updatePolicyPermissions(body);
        // retrieve the new policy and send back as the response
        SubscriptionPolicy newSubscriptionPolicy = apiProvider.getSubscriptionPolicy(username, body.getPolicyName());
        SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(newSubscriptionPolicy);
        // setting policy permissions
        setPolicyPermissionsToDTO(policyDTO);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while updating Subscription level policy: " + body.getPolicyName();
            throw new APIManagementException(errorMessage, e);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 49 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdDelete.

/**
 * Delete a Subscription level policy specified by uuid
 *
 * @param policyId          uuid of the policyu
 * @return 200 OK response if successfully deleted the policy
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdDelete(String policyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy existingPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }
        if (apiProvider.hasAttachments(username, existingPolicy.getPolicyName(), PolicyConstants.POLICY_LEVEL_SUB)) {
            String message = "Policy " + policyId + " already has subscriptions";
            log.error(message);
            throw new APIManagementException(message);
        }
        apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_SUB, existingPolicy.getPolicyName());
        return Response.ok().build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while deleting Subscription level policy : " + policyId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 50 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy in project carbon-apimgt by wso2.

the class ApiMgtDAO method getSubscriptionPolicies.

/**
 * Get all subscription level policeis belongs to specific tenant
 *
 * @param tenantID tenantID filters the polices belongs to specific tenant
 * @return subscriptionPolicy array list
 */
public SubscriptionPolicy[] getSubscriptionPolicies(int tenantID) throws APIManagementException {
    List<SubscriptionPolicy> policies = new ArrayList<SubscriptionPolicy>();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sqlQuery = SQLConstants.GET_SUBSCRIPTION_POLICIES;
    if (forceCaseInsensitiveComparisons) {
        sqlQuery = SQLConstants.GET_SUBSCRIPTION_POLICIES;
    }
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, tenantID);
        rs = ps.executeQuery();
        while (rs.next()) {
            SubscriptionPolicy subPolicy = new SubscriptionPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
            setCommonPolicyDetails(subPolicy, rs);
            subPolicy.setRateLimitCount(rs.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
            subPolicy.setRateLimitTimeUnit(rs.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
            subPolicy.setSubscriberCount(rs.getInt(ThrottlePolicyConstants.COLUMN_CONNECTION_COUNT));
            subPolicy.setStopOnQuotaReach(rs.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
            subPolicy.setBillingPlan(rs.getString(ThrottlePolicyConstants.COLUMN_BILLING_PLAN));
            subPolicy.setGraphQLMaxDepth(rs.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
            subPolicy.setGraphQLMaxComplexity(rs.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
            subPolicy.setMonetizationPlan(rs.getString(ThrottlePolicyConstants.COLUMN_MONETIZATION_PLAN));
            Map<String, String> monetizationPlanProperties = subPolicy.getMonetizationPlanProperties();
            monetizationPlanProperties.put(APIConstants.Monetization.FIXED_PRICE, rs.getString(ThrottlePolicyConstants.COLUMN_FIXED_RATE));
            monetizationPlanProperties.put(APIConstants.Monetization.BILLING_CYCLE, rs.getString(ThrottlePolicyConstants.COLUMN_BILLING_CYCLE));
            monetizationPlanProperties.put(APIConstants.Monetization.PRICE_PER_REQUEST, rs.getString(ThrottlePolicyConstants.COLUMN_PRICE_PER_REQUEST));
            monetizationPlanProperties.put(APIConstants.Monetization.CURRENCY, rs.getString(ThrottlePolicyConstants.COLUMN_CURRENCY));
            subPolicy.setMonetizationPlanProperties(monetizationPlanProperties);
            InputStream binary = rs.getBinaryStream(ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
            if (binary != null) {
                byte[] customAttrib = APIUtil.toByteArray(binary);
                subPolicy.setCustomAttributes(customAttrib);
            }
            policies.add(subPolicy);
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL", e);
    } catch (IOException e) {
        handleException("Error while converting input stream to byte array", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return policies.toArray(new SubscriptionPolicy[policies.size()]);
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) 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)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)74 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)42 Test (org.testng.annotations.Test)41 ArrayList (java.util.ArrayList)36 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 API (org.wso2.carbon.apimgt.core.models.API)30 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)30 Test (org.junit.Test)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)24 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 SQLException (java.sql.SQLException)15 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)15 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)15 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)15