use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class SolaceBrokerDeployer method undeployWhenRetire.
/**
* Undeploy API artifact from provided environment in the external gateway when Api is retired
*
* @param api API to be undeployed from the external gateway
* @param environment Environment needed to be undeployed API from the external gateway
* @throws DeployerException if error occurs when undeploying APIs from the external gateway
*/
public boolean undeployWhenRetire(API api, Environment environment) throws DeployerException {
Application application;
APIProvider apiProvider;
// Remove subscription
try {
apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
List<SubscribedAPI> apiUsages = apiProvider.getAPIUsageByAPIId(api.getUuid(), api.getOrganization());
for (SubscribedAPI usage : apiUsages) {
application = usage.getApplication();
// Check whether the subscription is belongs to an API deployed in Solace
if (SolaceConstants.SOLACE_ENVIRONMENT.equals(api.getGatewayVendor())) {
SolaceNotifierUtils.unsubscribeAPIProductFromSolaceApplication(api, application);
}
}
} catch (APIManagementException e) {
throw new DeployerException("Error occurred when removing subscriptions of the API " + api.getUuid() + "to be retired", e);
}
// undeploy API from Solace
boolean deletedFromSolace = undeploy(api.getId().getName(), api.getId().getVersion(), api.getContext(), environment);
if (!deletedFromSolace) {
throw new DeployerException("Error while deleting API product of API " + api.getUuid() + "from Solace " + "broker");
}
return true;
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPIUsageByAPIId.
/**
* Returns usage details of a particular API
*
* @param uuid API uuid
* @param organization identifier of the organization
* @return UserApplicationAPIUsages for given provider
* @throws org.wso2.carbon.apimgt.api.APIManagementException If failed to get UserApplicationAPIUsage
*/
@Override
public List<SubscribedAPI> getAPIUsageByAPIId(String uuid, String organization) throws APIManagementException {
APIIdentifier identifier = apiMgtDAO.getAPIIdentifierFromUUID(uuid);
List<SubscribedAPI> subscribedAPIs = new ArrayList<>();
if (identifier != null) {
APIIdentifier apiIdEmailReplaced = new APIIdentifier(APIUtil.replaceEmailDomain(identifier.getProviderName()), identifier.getApiName(), identifier.getVersion());
UserApplicationAPIUsage[] allApiResult = apiMgtDAO.getAllAPIUsageByProviderAndApiId(uuid, organization);
for (UserApplicationAPIUsage usage : allApiResult) {
for (SubscribedAPI apiSubscription : usage.getApiSubscriptions()) {
APIIdentifier subsApiId = apiSubscription.getApiId();
APIIdentifier subsApiIdEmailReplaced = new APIIdentifier(APIUtil.replaceEmailDomain(subsApiId.getProviderName()), subsApiId.getApiName(), subsApiId.getVersion());
if (subsApiIdEmailReplaced.equals(apiIdEmailReplaced)) {
subscribedAPIs.add(apiSubscription);
}
}
}
}
return subscribedAPIs;
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class APIProviderImpl method updateSubscription.
/**
* This method is used to update the subscription
*
* @param subscribedAPI subscribedAPI object that represents the new subscription detals
* @throws APIManagementException if failed to update subscription
*/
public void updateSubscription(SubscribedAPI subscribedAPI) throws APIManagementException {
apiMgtDAO.updateSubscription(subscribedAPI);
subscribedAPI = apiMgtDAO.getSubscriptionByUUID(subscribedAPI.getUUID());
Identifier identifier = subscribedAPI.getApiId() != null ? subscribedAPI.getApiId() : subscribedAPI.getProductId();
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_UPDATE.name(), tenantId, tenantDomain, subscribedAPI.getSubscriptionId(), subscribedAPI.getUUID(), identifier.getId(), identifier.getUUID(), subscribedAPI.getApplication().getId(), subscribedAPI.getApplication().getUUID(), subscribedAPI.getTier().getName(), subscribedAPI.getSubStatus());
APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsPost.
/**
* Creates a new subscriptions with the details specified in the body parameter
*
* @param body new subscription details
* @return newly added subscription as a SubscriptionDTO if successful
*/
@Override
public Response subscriptionsPost(SubscriptionDTO body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer;
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String userOrganization = RestApiUtil.getValidatedSubjectOrganization(messageContext);
apiConsumer = RestApiCommonUtil.getConsumer(username, userOrganization);
String applicationId = body.getApplicationId();
// this will throw a APIMgtResourceNotFoundException
if (body.getApiId() != null) {
if (!RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(body.getApiId(), organization)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, body.getApiId(), log);
}
} else {
RestApiUtil.handleBadRequest("Request must contain either apiIdentifier or apiProductIdentifier and the relevant type", log);
return null;
}
Application application = apiConsumer.getApplicationByUUID(applicationId);
if (application == null) {
// required application not found
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
return null;
}
// If application creation workflow status is pending or rejected, throw a Bad request exception
if (application.getStatus().equals(WorkflowStatus.REJECTED.toString()) || application.getStatus().equals(WorkflowStatus.CREATED.toString())) {
RestApiUtil.handleBadRequest("Workflow status is not Approved", log);
return null;
}
if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
// application access failure occurred
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
}
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(body.getApiId(), organization);
apiTypeWrapper.setTier(body.getThrottlingPolicy());
SubscriptionResponse subscriptionResponse = apiConsumer.addSubscription(apiTypeWrapper, username, application);
SubscribedAPI addedSubscribedAPI = apiConsumer.getSubscriptionByUUID(subscriptionResponse.getSubscriptionUUID());
SubscriptionDTO addedSubscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(addedSubscribedAPI, apiTypeWrapper, organization);
WorkflowResponse workflowResponse = subscriptionResponse.getWorkflowResponse();
if (workflowResponse instanceof HttpWorkflowResponse) {
String payload = workflowResponse.getJSONPayload();
addedSubscriptionDTO.setRedirectionParams(payload);
}
return Response.created(new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTIONS + "/" + addedSubscribedAPI.getUUID())).entity(addedSubscriptionDTO).build();
} catch (APIMgtAuthorizationFailedException e) {
// this occurs when the api:application:tier mapping is not allowed. The reason for the message is taken from
// the message of the exception e
RestApiUtil.handleAuthorizationFailure(e.getMessage(), e, log);
} catch (SubscriptionAlreadyExistingException e) {
RestApiUtil.handleResourceAlreadyExistsError("Specified subscription already exists for API " + body.getApiId() + ", for application " + body.getApplicationId(), e, log);
} catch (URISyntaxException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
// this happens when the specified API identifier does not exist
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, body.getApiId(), e, log);
} else {
// unhandled exception
RestApiUtil.handleInternalServerError("Error while adding the subscription API:" + body.getApiId() + ", application:" + body.getApplicationId() + ", tier:" + body.getThrottlingPolicy(), e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method getAdditionalInfoOfAPISubscriptions.
/**
* Get additional Info details of subscriptions attached with given API
*
* @param apiId apiId
* @param offset starting index of the subscription list
* @param limit max num of subscriptions returned
* @param ifNoneMatch If-None-Match header value
* @param messageContext message context
* @return Response with additional Info of the GraphQL API
*/
@Override
public Response getAdditionalInfoOfAPISubscriptions(String apiId, String groupId, String xWSO2Tenant, Integer offset, Integer limit, String ifNoneMatch, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
Subscriber subscriber = new Subscriber(username);
Set<SubscribedAPI> subscriptions;
List<SubscribedAPI> subscribedAPIList = new ArrayList<>();
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
AdditionalSubscriptionInfoListDTO additionalSubscriptionInfoListDTO;
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
if (apiTypeWrapper.isAPIProduct()) {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApiProduct().getId(), groupId, organization);
} else {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApi().getId(), groupId, organization);
}
// Sort subscriptions by application name
subscribedAPIList.addAll(subscriptions);
subscribedAPIList.sort(Comparator.comparing(o -> o.getApplication().getName()));
additionalSubscriptionInfoListDTO = AdditionalSubscriptionInfoMappingUtil.fromAdditionalSubscriptionInfoListToDTO(subscribedAPIList, limit, offset, organization);
AdditionalSubscriptionInfoMappingUtil.setPaginationParams(additionalSubscriptionInfoListDTO, apiId, "", limit, offset, subscribedAPIList.size());
return Response.ok().entity(additionalSubscriptionInfoListDTO).build();
} catch (APIManagementException e) {
// to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving additional information details of the API : " + apiId, e, log);
} else {
String msg = "Error while retrieving additional information details of the API " + apiId;
RestApiUtil.handleInternalServerError(msg, e, log);
}
}
return null;
}
Aggregations