use of org.wso2.carbon.apimgt.keymgt.model.entity.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;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.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()]);
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateSubscriptionPolicy.
/**
* Updates Subscription level policy.
* <p>policy name and tenant id should be specified in <code>policy</code></p>
*
* @param policy updated policy object
* @throws APIManagementException
*/
public void updateSubscriptionPolicy(SubscriptionPolicy policy) throws APIManagementException {
Connection connection = null;
PreparedStatement updateStatement = null;
boolean hasCustomAttrib = false;
String updateQuery;
try {
if (policy.getCustomAttributes() != null) {
hasCustomAttrib = true;
}
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_WITH_CUSTOM_ATTRIBUTES_SQL;
}
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_BY_UUID_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_SUBSCRIPTION_POLICY_WITH_CUSTOM_ATTRIBUTES_BY_UUID_SQL;
}
} else {
String errorMsg = "Policy object doesn't contain mandatory parameters. At least UUID or Name,Tenant Id" + " should be provided. Name: " + policy.getPolicyName() + ", Tenant Id: " + policy.getTenantId() + ", UUID: " + policy.getUUID();
log.error(errorMsg);
throw new APIManagementException(errorMsg);
}
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(false);
updateStatement = connection.prepareStatement(updateQuery);
if (!StringUtils.isEmpty(policy.getDisplayName())) {
updateStatement.setString(1, policy.getDisplayName());
} else {
updateStatement.setString(1, policy.getPolicyName());
}
updateStatement.setString(2, policy.getDescription());
updateStatement.setString(3, policy.getDefaultQuotaPolicy().getType());
if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
RequestCountLimit limit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getRequestCount());
updateStatement.setString(5, null);
} else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
BandwidthLimit limit = (BandwidthLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getDataAmount());
updateStatement.setString(5, limit.getDataUnit());
} else if (PolicyConstants.EVENT_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
EventCountLimit limit = (EventCountLimit) policy.getDefaultQuotaPolicy().getLimit();
updateStatement.setLong(4, limit.getEventCount());
updateStatement.setString(5, null);
}
updateStatement.setLong(6, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
updateStatement.setString(7, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
updateStatement.setInt(8, policy.getRateLimitCount());
updateStatement.setString(9, policy.getRateLimitTimeUnit());
updateStatement.setBoolean(10, policy.isStopOnQuotaReach());
updateStatement.setInt(11, policy.getGraphQLMaxDepth());
updateStatement.setInt(12, policy.getGraphQLMaxComplexity());
updateStatement.setString(13, policy.getBillingPlan());
if (hasCustomAttrib) {
long lengthOfStream = policy.getCustomAttributes().length;
updateStatement.setBinaryStream(14, new ByteArrayInputStream(policy.getCustomAttributes()), lengthOfStream);
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(15, policy.getMonetizationPlan());
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(19, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(20, policy.getSubscriberCount());
updateStatement.setString(21, policy.getPolicyName());
updateStatement.setInt(22, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(15, policy.getMonetizationPlan());
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(19, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(20, policy.getSubscriberCount());
updateStatement.setString(21, policy.getUUID());
}
} else {
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(14, policy.getMonetizationPlan());
updateStatement.setString(15, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(19, policy.getSubscriberCount());
updateStatement.setString(20, policy.getPolicyName());
updateStatement.setInt(21, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(14, policy.getMonetizationPlan());
updateStatement.setString(15, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.FIXED_PRICE));
updateStatement.setString(16, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.BILLING_CYCLE));
updateStatement.setString(17, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.PRICE_PER_REQUEST));
updateStatement.setString(18, policy.getMonetizationPlanProperties().get(APIConstants.Monetization.CURRENCY));
updateStatement.setInt(19, policy.getSubscriberCount());
updateStatement.setString(20, policy.getUUID());
}
}
updateStatement.executeUpdate();
connection.commit();
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
// Rollback failed. Exception will be thrown later for upper exception
log.error("Failed to rollback the update Subscription Policy: " + policy.toString(), ex);
}
}
handleException("Failed to update subscription policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
} finally {
APIMgtDBUtil.closeAllConnections(updateStatement, connection, null);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testUpdateSubscriptionPolicy.
@Test
public void testUpdateSubscriptionPolicy() throws APIManagementException {
String policyName = "TestUpdateSubPolicy";
SubscriptionPolicy policy = (SubscriptionPolicy) getSubscriptionPolicy(policyName);
policy.setMonetizationPlan(APIConstants.Monetization.FIXED_RATE);
policy.setMonetizationPlanProperties(new HashMap<String, String>());
apiMgtDAO.addSubscriptionPolicy(policy);
policy = (SubscriptionPolicy) getSubscriptionPolicy(policyName);
policy.setDescription("Updated subscription description");
policy.setGraphQLMaxComplexity(200);
policy.setGraphQLMaxDepth(7);
apiMgtDAO.updateSubscriptionPolicy(policy);
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testAddAndGetSubscriptionPolicy.
@Test
public void testAddAndGetSubscriptionPolicy() throws Exception {
SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) getSubscriptionPolicy("testAddAndGetSubscriptionPolicy");
String customAttributes = "{api:abc}";
subscriptionPolicy.setTenantId(-1234);
subscriptionPolicy.setCustomAttributes(customAttributes.getBytes());
subscriptionPolicy.setMonetizationPlan(APIConstants.Monetization.FIXED_RATE);
subscriptionPolicy.setMonetizationPlanProperties(new HashMap<String, String>());
apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
SubscriptionPolicy retrievedPolicy = apiMgtDAO.getSubscriptionPolicy(subscriptionPolicy.getPolicyName(), -1234);
SubscriptionPolicy retrievedPolicyFromUUID = apiMgtDAO.getSubscriptionPolicyByUUID(retrievedPolicy.getUUID());
assertEquals(retrievedPolicy.getDescription(), retrievedPolicyFromUUID.getDescription());
assertEquals(retrievedPolicy.getDisplayName(), retrievedPolicyFromUUID.getDisplayName());
assertEquals(retrievedPolicy.getRateLimitCount(), retrievedPolicyFromUUID.getRateLimitCount());
assertEquals(retrievedPolicy.getRateLimitTimeUnit(), retrievedPolicyFromUUID.getRateLimitTimeUnit());
retrievedPolicyFromUUID.setCustomAttributes(customAttributes.getBytes());
apiMgtDAO.updateSubscriptionPolicy(retrievedPolicyFromUUID);
retrievedPolicyFromUUID.setPolicyName(null);
apiMgtDAO.updateSubscriptionPolicy(retrievedPolicyFromUUID);
SubscriptionPolicy[] subscriptionPolicies = apiMgtDAO.getSubscriptionPolicies(-1234);
apiMgtDAO.updateThrottleTierPermissions(subscriptionPolicy.getPolicyName(), "allow", "internal/everyone", -1234);
Set<TierPermissionDTO> throttleTierPermissions = apiMgtDAO.getThrottleTierPermissions(-1234);
for (TierPermissionDTO tierPermissionDTO : throttleTierPermissions) {
if (subscriptionPolicy.getPolicyName().equals(tierPermissionDTO.getTierName())) {
assertTrue(true);
break;
}
}
apiMgtDAO.updateThrottleTierPermissions(subscriptionPolicy.getPolicyName(), "deny", "internal/everyone", -1234);
assertNotNull(apiMgtDAO.getThrottleTierPermission(subscriptionPolicy.getPolicyName(), -1234));
assertTrue(subscriptionPolicies.length > 0);
apiMgtDAO.setPolicyDeploymentStatus(PolicyConstants.POLICY_LEVEL_SUB, subscriptionPolicy.getPolicyName(), -1234, true);
assertTrue(apiMgtDAO.getPolicyNames(PolicyConstants.POLICY_LEVEL_SUB, "admin").length > 0);
assertTrue(apiMgtDAO.isPolicyDeployed(PolicyConstants.POLICY_LEVEL_SUB, -1234, subscriptionPolicy.getPolicyName()));
assertTrue(apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, -1234, subscriptionPolicy.getPolicyName()));
apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_SUB, "testAddAndGetSubscriptionPolicy", -1234);
}
Aggregations