use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class WebhooksDAO method isThrottled.
/*
* This method can be used to check whether the webhooks subscriptions is throttled out or not
*
* @param properties Subscription request properties
* */
public boolean isThrottled(Properties properties) throws APIManagementException {
try (Connection conn = APIMgtDBUtil.getConnection()) {
int throttleLimit = getAllowedConnectionsCount(conn, properties);
int currentLimit = getCurrentConnectionsCount(conn, properties);
return currentLimit > throttleLimit;
} catch (SQLException e) {
throw new APIManagementException("Error while storing subscription with callback " + properties.getProperty(APIConstants.Webhooks.CALLBACK) + " for the API " + properties.getProperty(APIConstants.Webhooks.API_UUID), e);
}
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method populateSubscriptionsList.
private void populateSubscriptionsList(List<Subscription> subscriptions, ResultSet resultSet) throws SQLException {
if (resultSet != null && subscriptions != null) {
while (resultSet.next()) {
Subscription subscription = new Subscription();
subscription.setSubscriptionUUID(resultSet.getString("SUBSCRIPTION_UUID"));
subscription.setSubscriptionId(resultSet.getInt("SUB_ID"));
subscription.setPolicyId(resultSet.getString("TIER"));
subscription.setApiId(resultSet.getInt("API_ID"));
subscription.setAppId(resultSet.getInt("APP_ID"));
subscription.setApiUUID(resultSet.getString("API_UUID"));
subscription.setApplicationUUID(resultSet.getString("APPLICATION_UUID"));
subscription.setSubscriptionState(resultSet.getString("STATUS"));
subscriptions.add(subscription);
}
}
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription 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;
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription 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;
}
use of org.wso2.carbon.apimgt.api.model.webhooks.Subscription 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;
}
Aggregations