use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class APIConsumerImpl method removeSubscription.
@Override
public void removeSubscription(Identifier identifier, String userId, int applicationId, String organization) throws APIManagementException {
APIIdentifier apiIdentifier = null;
APIProductIdentifier apiProdIdentifier = null;
if (identifier instanceof APIIdentifier) {
apiIdentifier = (APIIdentifier) identifier;
}
if (identifier instanceof APIProductIdentifier) {
apiProdIdentifier = (APIProductIdentifier) identifier;
}
String applicationName = apiMgtDAO.getApplicationNameFromId(applicationId);
try {
SubscriptionWorkflowDTO workflowDTO;
WorkflowExecutor createSubscriptionWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
WorkflowExecutor removeSubscriptionWFExecutor = getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
String workflowExtRef = apiMgtDAO.getExternalWorkflowReferenceForSubscription(identifier, applicationId, organization);
// in a normal flow workflowExtRef is null when workflows are not enabled
if (workflowExtRef == null) {
workflowDTO = new SubscriptionWorkflowDTO();
} else {
workflowDTO = (SubscriptionWorkflowDTO) apiMgtDAO.retrieveWorkflow(workflowExtRef);
// set tiername to the workflowDTO only when workflows are enabled
SubscribedAPI subscription = apiMgtDAO.getSubscriptionById(Integer.parseInt(workflowDTO.getWorkflowReference()));
workflowDTO.setTierName(subscription.getTier().getName());
}
workflowDTO.setApiProvider(identifier.getProviderName());
API api = null;
APIProduct product = null;
String context = null;
ApiTypeWrapper wrapper;
if (apiIdentifier != null) {
// The API is retrieved without visibility permission check, since the subscribers should be allowed
// to delete already existing subscriptions made for restricted APIs
wrapper = getAPIorAPIProductByUUIDWithoutPermissionCheck(apiIdentifier.getUUID(), organization);
api = wrapper.getApi();
context = api.getContext();
} else if (apiProdIdentifier != null) {
// The API Product is retrieved without visibility permission check, since the subscribers should be
// allowe to delete already existing subscriptions made for restricted API Products
wrapper = getAPIorAPIProductByUUIDWithoutPermissionCheck(apiProdIdentifier.getUUID(), organization);
product = wrapper.getApiProduct();
context = product.getContext();
}
workflowDTO.setApiContext(context);
workflowDTO.setApiName(identifier.getName());
workflowDTO.setApiVersion(identifier.getVersion());
workflowDTO.setApplicationName(applicationName);
workflowDTO.setTenantDomain(tenantDomain);
workflowDTO.setTenantId(tenantId);
workflowDTO.setExternalWorkflowReference(workflowExtRef);
workflowDTO.setSubscriber(userId);
workflowDTO.setCallbackUrl(removeSubscriptionWFExecutor.getCallbackURL());
workflowDTO.setApplicationId(applicationId);
workflowDTO.setMetadata(WorkflowConstants.PayloadConstants.API_ID, String.valueOf(identifier.getId()));
String status = null;
if (apiIdentifier != null) {
status = apiMgtDAO.getSubscriptionStatus(apiIdentifier.getUUID(), applicationId);
} else if (apiProdIdentifier != null) {
status = apiMgtDAO.getSubscriptionStatus(apiProdIdentifier.getUUID(), applicationId);
}
if (APIConstants.SubscriptionStatus.ON_HOLD.equals(status)) {
try {
createSubscriptionWFExecutor.cleanUpPendingTask(workflowExtRef);
} catch (WorkflowException ex) {
// failed cleanup processes are ignored to prevent failing the deletion process
log.warn("Failed to clean pending subscription approval task");
}
}
// update attributes of the new remove workflow to be created
workflowDTO.setStatus(WorkflowStatus.CREATED);
workflowDTO.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
workflowDTO.setCreatedTime(System.currentTimeMillis());
workflowDTO.setExternalWorkflowReference(removeSubscriptionWFExecutor.generateUUID());
Tier tier = null;
if (api != null) {
Set<Tier> policies = api.getAvailableTiers();
Iterator<Tier> iterator = policies.iterator();
boolean isPolicyAllowed = false;
while (iterator.hasNext()) {
Tier policy = iterator.next();
if (policy.getName() != null && (policy.getName()).equals(workflowDTO.getTierName())) {
tier = policy;
}
}
} else if (product != null) {
Set<Tier> policies = product.getAvailableTiers();
Iterator<Tier> iterator = policies.iterator();
boolean isPolicyAllowed = false;
while (iterator.hasNext()) {
Tier policy = iterator.next();
if (policy.getName() != null && (policy.getName()).equals(workflowDTO.getTierName())) {
tier = policy;
}
}
}
if (api != null) {
// check whether monetization is enabled for API and tier plan is commercial
if (api.getMonetizationStatus() && APIConstants.COMMERCIAL_TIER_PLAN.equals(tier.getTierPlan())) {
removeSubscriptionWFExecutor.deleteMonetizedSubscription(workflowDTO, api);
} else {
removeSubscriptionWFExecutor.execute(workflowDTO);
}
} else if (product != null) {
// check whether monetization is enabled for API product and tier plan is commercial
if (product.getMonetizationStatus() && APIConstants.COMMERCIAL_TIER_PLAN.equals(tier.getTierPlan())) {
removeSubscriptionWFExecutor.deleteMonetizedSubscription(workflowDTO, product);
} else {
removeSubscriptionWFExecutor.execute(workflowDTO);
}
}
JSONObject subsLogObject = new JSONObject();
subsLogObject.put(APIConstants.AuditLogConstants.API_NAME, identifier.getName());
subsLogObject.put(APIConstants.AuditLogConstants.PROVIDER, identifier.getProviderName());
subsLogObject.put(APIConstants.AuditLogConstants.APPLICATION_ID, applicationId);
subsLogObject.put(APIConstants.AuditLogConstants.APPLICATION_NAME, applicationName);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.SUBSCRIPTION, subsLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
} catch (WorkflowException e) {
String errorMsg = "Could not execute Workflow, " + WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION + " for resource " + identifier.toString();
handleException(errorMsg, e);
}
if (log.isDebugEnabled()) {
String logMessage = "Subscription removed from app " + applicationName + " by " + userId + " For Id: " + identifier.toString();
log.debug(logMessage);
}
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class APIConsumerImpl method getPaginatedSubscribedAPIsByApplication.
@Override
public Set<SubscribedAPI> getPaginatedSubscribedAPIsByApplication(Application application, Integer offset, Integer limit, String organization) throws APIManagementException {
Set<SubscribedAPI> subscribedAPIs = null;
try {
subscribedAPIs = apiMgtDAO.getPaginatedSubscribedAPIsByApplication(application, offset, limit, organization);
if (subscribedAPIs != null && !subscribedAPIs.isEmpty()) {
Map<String, Tier> tiers = APIUtil.getTiers(tenantId);
for (SubscribedAPI subscribedApi : subscribedAPIs) {
Tier tier = tiers.get(subscribedApi.getTier().getName());
subscribedApi.getTier().setDisplayName(tier != null ? tier.getDisplayName() : subscribedApi.getTier().getName());
}
}
} catch (APIManagementException e) {
handleException("Failed to get subscribed APIs of application " + application.getUUID(), e);
}
return subscribedAPIs;
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class APIConsumerImpl method getSubscribedIdentifiers.
@Override
public Set<SubscribedAPI> getSubscribedIdentifiers(Subscriber subscriber, Identifier identifier, String groupingId, String organization) throws APIManagementException {
Set<SubscribedAPI> subscribedAPISet = new HashSet<>();
Set<SubscribedAPI> subscribedAPIs = getSubscribedAPIs(organization, subscriber, groupingId);
for (SubscribedAPI api : subscribedAPIs) {
if (identifier instanceof APIIdentifier && identifier.equals(api.getApiId())) {
Set<APIKey> keys = getApplicationKeys(api.getApplication().getId());
for (APIKey key : keys) {
api.addKey(key);
}
subscribedAPISet.add(api);
} else if (identifier instanceof APIProductIdentifier && identifier.equals(api.getProductId())) {
Set<APIKey> keys = getApplicationKeys(api.getApplication().getId());
for (APIKey key : keys) {
api.addKey(key);
}
subscribedAPISet.add(api);
}
}
return subscribedAPISet;
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class APIConsumerImpl method getLightWeightSubscribedAPIs.
private Set<SubscribedAPI> getLightWeightSubscribedAPIs(String organization, Subscriber subscriber, String groupingId) throws APIManagementException {
Set<SubscribedAPI> originalSubscribedAPIs;
Set<SubscribedAPI> subscribedAPIs = new HashSet<SubscribedAPI>();
try {
originalSubscribedAPIs = apiMgtDAO.getSubscribedAPIs(organization, subscriber, groupingId);
if (originalSubscribedAPIs != null && !originalSubscribedAPIs.isEmpty()) {
Map<String, Tier> tiers = APIUtil.getTiers(tenantId);
for (SubscribedAPI subscribedApi : originalSubscribedAPIs) {
Application application = subscribedApi.getApplication();
if (application != null) {
int applicationId = application.getId();
}
Tier tier = tiers.get(subscribedApi.getTier().getName());
subscribedApi.getTier().setDisplayName(tier != null ? tier.getDisplayName() : subscribedApi.getTier().getName());
subscribedAPIs.add(subscribedApi);
}
}
} catch (APIManagementException e) {
handleException("Failed to get APIs of " + subscriber.getName(), e);
}
return subscribedAPIs;
}
Aggregations