use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method setCommonProperties.
private void setCommonProperties(APIPolicyConditionGroup apiPolicyConditionGroup, ResultSet resultSet) throws SQLException {
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
if (quotaPolicy.getType() != null) {
if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
RequestCountLimit reqLimit = new RequestCountLimit();
reqLimit.setUnitTime(resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME));
reqLimit.setTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT));
reqLimit.setRequestCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA));
quotaPolicy.setLimit(reqLimit);
} else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
BandwidthLimit bandLimit = new BandwidthLimit();
bandLimit.setUnitTime(resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME));
bandLimit.setTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT));
bandLimit.setDataAmount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA));
bandLimit.setDataUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_UNIT));
quotaPolicy.setLimit(bandLimit);
}
apiPolicyConditionGroup.setQuotaPolicy(quotaPolicy);
}
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateApplicationPolicy.
/**
* Updates Application 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 updateApplicationPolicy(ApplicationPolicy policy) throws APIManagementException {
Connection connection = null;
PreparedStatement updateStatement = null;
boolean hasCustomAttrib = false;
String updateQuery;
if (policy.getTenantId() == -1 || StringUtils.isEmpty(policy.getPolicyName())) {
String errorMsg = "Policy object doesn't contain mandatory parameters. Name: " + policy.getPolicyName() + ", Tenant Id: " + policy.getTenantId();
log.error(errorMsg);
throw new APIManagementException(errorMsg);
}
try {
if (policy.getCustomAttributes() != null) {
hasCustomAttrib = true;
}
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(false);
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateQuery = SQLConstants.UPDATE_APPLICATION_POLICY_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_APPLICATION_POLICY_WITH_CUSTOM_ATTRIBUTES_SQL;
}
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateQuery = SQLConstants.UPDATE_APPLICATION_POLICY_BY_UUID_SQL;
if (hasCustomAttrib) {
updateQuery = SQLConstants.UPDATE_APPLICATION_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);
}
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());
}
updateStatement.setLong(6, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
updateStatement.setString(7, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
if (hasCustomAttrib) {
updateStatement.setBlob(8, new ByteArrayInputStream(policy.getCustomAttributes()));
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(9, policy.getPolicyName());
updateStatement.setInt(10, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(9, policy.getUUID());
}
} else {
if (!StringUtils.isBlank(policy.getPolicyName()) && policy.getTenantId() != -1) {
updateStatement.setString(8, policy.getPolicyName());
updateStatement.setInt(9, policy.getTenantId());
} else if (!StringUtils.isBlank(policy.getUUID())) {
updateStatement.setString(8, 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 Application Policy: " + policy.toString(), ex);
}
}
handleException("Failed to update application policy: " + policy.getPolicyName() + '-' + policy.getTenantId(), e);
} finally {
APIMgtDBUtil.closeAllConnections(updateStatement, connection, null);
}
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit 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.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class APIProviderImplTest method getPolicyAPILevelPerUser.
private APIPolicy getPolicyAPILevelPerUser() {
APIPolicy policy = new APIPolicy("custom1");
policy.setUserLevel(PolicyConstants.PER_USER);
policy.setDescription("Description");
// policy.setPolicyLevel("api");
policy.setTenantDomain("carbon.super");
RequestCountLimit defaultLimit = new RequestCountLimit();
defaultLimit.setTimeUnit("min");
defaultLimit.setUnitTime(5);
defaultLimit.setRequestCount(400);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
defaultQuotaPolicy.setLimit(defaultLimit);
defaultQuotaPolicy.setType("RequestCount");
policy.setDefaultQuotaPolicy(defaultQuotaPolicy);
List<Pipeline> pipelines;
Pipeline p;
QuotaPolicy quotaPolicy;
List<Condition> condition;
RequestCountLimit countlimit;
Condition cond;
pipelines = new ArrayList<Pipeline>();
// /////////pipeline item start//////
p = new Pipeline();
quotaPolicy = new QuotaPolicy();
quotaPolicy.setType("RequestCount");
countlimit = new RequestCountLimit();
countlimit.setTimeUnit("min");
countlimit.setUnitTime(5);
countlimit.setRequestCount(100);
quotaPolicy.setLimit(countlimit);
condition = new ArrayList<Condition>();
HTTPVerbCondition verbCond = new HTTPVerbCondition();
verbCond.setHttpVerb("POST");
condition.add(verbCond);
p.setQuotaPolicy(quotaPolicy);
p.setConditions(condition);
pipelines.add(p);
// /////////pipeline item end//////
policy.setPipelines(pipelines);
return policy;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit in project carbon-apimgt by wso2.
the class ApiMgtDAO method getPipelines.
/**
* Retrieves list of pipelines for the policy with policy Id: <code>policyId</code>
*
* @param policyId policy id of the pipelines
* @return list of pipelines
* @throws APIManagementException
*/
private ArrayList<Pipeline> getPipelines(int policyId) throws APIManagementException {
Connection connection = null;
PreparedStatement pipelinesStatement = null;
ResultSet resultSet = null;
ArrayList<Pipeline> pipelines = new ArrayList<Pipeline>();
try {
connection = APIMgtDBUtil.getConnection();
pipelinesStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.GET_PIPELINES_SQL);
int unitTime = 0;
int quota = 0;
int pipelineId = -1;
String timeUnit = null;
String quotaUnit = null;
String description;
pipelinesStatement.setInt(1, policyId);
resultSet = pipelinesStatement.executeQuery();
while (resultSet.next()) {
Pipeline pipeline = new Pipeline();
ArrayList<Condition> conditions = null;
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
timeUnit = resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT);
quotaUnit = resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_UNIT);
unitTime = resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME);
quota = resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA);
pipelineId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONDITION_ID);
description = resultSet.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION);
if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setUnitTime(unitTime);
requestCountLimit.setTimeUnit(timeUnit);
requestCountLimit.setRequestCount(quota);
quotaPolicy.setLimit(requestCountLimit);
} else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
BandwidthLimit bandwidthLimit = new BandwidthLimit();
bandwidthLimit.setUnitTime(unitTime);
bandwidthLimit.setTimeUnit(timeUnit);
bandwidthLimit.setDataUnit(quotaUnit);
bandwidthLimit.setDataAmount(quota);
quotaPolicy.setLimit(bandwidthLimit);
}
conditions = getConditions(pipelineId);
pipeline.setConditions(conditions);
pipeline.setQuotaPolicy(quotaPolicy);
pipeline.setId(pipelineId);
pipeline.setDescription(description);
pipelines.add(pipeline);
}
} catch (SQLException e) {
handleException("Failed to get pipelines for policyId: " + policyId, e);
} finally {
APIMgtDBUtil.closeAllConnections(pipelinesStatement, connection, resultSet);
}
return pipelines;
}
Aggregations