use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method getSubscriptionPolicyByNameForTenant.
/*
* @param policyName : name of the subscription level throttling policy
* @return {@link SubscriptionPolicy}
* */
public SubscriptionPolicy getSubscriptionPolicyByNameForTenant(String policyName, String tenantDomain) {
if (StringUtils.isNotEmpty(policyName) && StringUtils.isNotEmpty(tenantDomain)) {
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_SUBSCRIPTION_POLICY_SQL)) {
int tenantId = 0;
try {
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
} catch (UserStoreException e) {
log.error("Error in loading ApplicationPolicy for tenantDomain : " + tenantDomain, e);
}
ps.setString(1, policyName);
ps.setInt(2, tenantId);
try (ResultSet resultSet = ps.executeQuery()) {
if (resultSet.next()) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy();
subscriptionPolicy.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
subscriptionPolicy.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_POLICY_NAME));
subscriptionPolicy.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
subscriptionPolicy.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
subscriptionPolicy.setTenantDomain(APIUtil.getTenantDomainFromTenantId(tenantId));
subscriptionPolicy.setRateLimitCount(resultSet.getInt(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_COUNT));
subscriptionPolicy.setRateLimitTimeUnit(resultSet.getString(ThrottlePolicyConstants.COLUMN_RATE_LIMIT_TIME_UNIT));
subscriptionPolicy.setStopOnQuotaReach(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH));
subscriptionPolicy.setGraphQLMaxDepth(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_DEPTH));
subscriptionPolicy.setGraphQLMaxComplexity(resultSet.getInt(ThrottlePolicyConstants.COLUMN_MAX_COMPLEXITY));
setCommonProperties(subscriptionPolicy, resultSet);
return subscriptionPolicy;
}
}
} catch (SQLException e) {
log.error("Error in retrieving Subscription policy by id : " + policyName + " for " + tenantDomain, e);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method getAllApplicationPolicies.
/*
* @param tenantDomain : tenant domain name
* @return {@link List<ApplicationPolicy>}
* */
public List<ApplicationPolicy> getAllApplicationPolicies(String tenantDomain) {
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_TENANT_APPLICATION_POLICIES_SQL)) {
int tenantId = 0;
try {
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
} catch (UserStoreException e) {
log.error("Error in loading ApplicationPolicies for tenantDomain : " + tenantDomain, e);
}
ps.setInt(1, tenantId);
try (ResultSet resultSet = ps.executeQuery()) {
return populateApplicationPolicyList(resultSet);
}
} catch (SQLException e) {
log.error("Error in loading application policies for tenantId : " + tenantDomain, e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method populateApplicationPolicyList.
private List<ApplicationPolicy> populateApplicationPolicyList(ResultSet resultSet) throws SQLException {
List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
if (resultSet != null) {
while (resultSet.next()) {
ApplicationPolicy applicationPolicyDTO = new ApplicationPolicy();
applicationPolicyDTO.setId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_POLICY_ID));
applicationPolicyDTO.setName(resultSet.getString(ThrottlePolicyConstants.COLUMN_NAME));
applicationPolicyDTO.setQuotaType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
applicationPolicyDTO.setTenantId(resultSet.getInt(ThrottlePolicyConstants.COLUMN_TENANT_ID));
String tenantDomain = APIUtil.getTenantDomainFromTenantId(applicationPolicyDTO.getTenantId());
applicationPolicyDTO.setTenantDomain(tenantDomain);
setCommonProperties(applicationPolicyDTO, resultSet);
applicationPolicies.add(applicationPolicyDTO);
}
}
return applicationPolicies;
}
use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesApplicationPolicyIdDelete.
/**
* Delete an Application level policy specified by uuid
*
* @param policyId uuid of the policy
* @return 200 OK response if successfully deleted the policy
*/
@Override
public Response throttlingPoliciesApplicationPolicyIdDelete(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
ApplicationPolicy existingPolicy = apiProvider.getApplicationPolicyByUUID(policyId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APP_POLICY, policyId, log);
}
if (apiProvider.hasAttachments(username, existingPolicy.getPolicyName(), PolicyConstants.POLICY_LEVEL_APP)) {
String message = "Policy " + policyId + " already attached to an application";
log.error(message);
throw new APIManagementException(message);
}
apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_APP, existingPolicy.getPolicyName());
return Response.ok().build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APP_POLICY, policyId, e, log);
} else {
String errorMessage = "Error while deleting Application level policy : " + policyId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesApplicationPolicyIdPut.
/**
* Updates a given Application level policy specified by uuid
*
* @param policyId uuid of the policy
* @param body DTO of policy to be updated
* @param contentType Content-Type header
* @return Updated policy
*/
@Override
public Response throttlingPoliciesApplicationPolicyIdPut(String policyId, String contentType, ApplicationThrottlePolicyDTO body, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// will give PolicyNotFoundException if there's no policy exists with UUID
ApplicationPolicy existingPolicy = apiProvider.getApplicationPolicyByUUID(policyId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APP_POLICY, policyId, log);
}
// overridden properties
body.setPolicyId(policyId);
body.setPolicyName(existingPolicy.getPolicyName());
// update the policy
ApplicationPolicy appPolicy = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(body);
apiProvider.updatePolicy(appPolicy);
// retrieve the new policy and send back as the response
ApplicationPolicy newAppPolicy = apiProvider.getApplicationPolicyByUUID(policyId);
ApplicationThrottlePolicyDTO policyDTO = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(newAppPolicy);
return Response.ok().entity(policyDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APP_POLICY, policyId, e, log);
} else {
String errorMessage = "Error while updating Application level policy: " + body.getPolicyName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
Aggregations