Search in sources :

Example 1 with ExportedSubscribedAPI

use of org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI in project carbon-apimgt by wso2.

the class ImportUtils method importSubscriptions.

/**
 * Import and add subscriptions of a particular application for the available APIs and API products
 *
 * @param subscribedAPIs Subscribed APIs
 * @param userId         Username of the subscriber
 * @param application    Application
 * @param update         Whether to update the application or not
 * @param apiConsumer    API Consumer
 * @param organization   Organization
 * @return a list of APIIdentifiers of the skipped subscriptions
 * @throws APIManagementException if an error occurs while importing and adding subscriptions
 * @throws UserStoreException     if an error occurs while checking whether the tenant domain exists
 */
public static List<APIIdentifier> importSubscriptions(Set<ExportedSubscribedAPI> subscribedAPIs, String userId, Application application, Boolean update, APIConsumer apiConsumer, String organization) throws APIManagementException, UserStoreException {
    List<APIIdentifier> skippedAPIList = new ArrayList<>();
    for (ExportedSubscribedAPI subscribedAPI : subscribedAPIs) {
        APIIdentifier apiIdentifier = subscribedAPI.getApiId();
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()));
        if (!StringUtils.isEmpty(tenantDomain) && APIUtil.isTenantAvailable(tenantDomain)) {
            String name = apiIdentifier.getApiName();
            String version = apiIdentifier.getVersion();
            // Creating a solr compatible search query, here we will execute a search query without wildcard *s
            StringBuilder searchQuery = new StringBuilder();
            String[] searchCriteria = { name, "version:" + version };
            for (int i = 0; i < searchCriteria.length; i++) {
                if (i == 0) {
                    searchQuery = new StringBuilder(APIUtil.getSingleSearchCriteria(searchCriteria[i]).replace("*", ""));
                } else {
                    searchQuery.append(APIConstants.SEARCH_AND_TAG).append(APIUtil.getSingleSearchCriteria(searchCriteria[i]).replace("*", ""));
                }
            }
            Map matchedAPIs;
            matchedAPIs = apiConsumer.searchPaginatedAPIs(searchQuery.toString(), tenantDomain, 0, Integer.MAX_VALUE, false);
            Set<Object> apiSet = (Set<Object>) matchedAPIs.get("apis");
            if (apiSet != null && !apiSet.isEmpty()) {
                Object type = apiSet.iterator().next();
                ApiTypeWrapper apiTypeWrapper = null;
                String apiOrApiProductUuid;
                // Check whether the object is ApiProduct
                if (isApiProduct(type)) {
                    APIProduct apiProduct = (APIProduct) apiSet.iterator().next();
                    apiOrApiProductUuid = APIUtil.getUUIDFromIdentifier(apiProduct.getId(), organization);
                } else {
                    API api = (API) apiSet.iterator().next();
                    apiOrApiProductUuid = APIUtil.getUUIDFromIdentifier(api.getId(), organization);
                }
                apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiOrApiProductUuid, organization);
                // Tier of the imported subscription
                String targetTier = subscribedAPI.getThrottlingPolicy();
                // Checking whether the target tier is available
                if (isTierAvailable(targetTier, apiTypeWrapper) && apiTypeWrapper.getStatus() != null && APIConstants.PUBLISHED.equals(apiTypeWrapper.getStatus())) {
                    apiTypeWrapper.setTier(targetTier);
                    // It will throw an error if subscriber already exists
                    if (update == null || !update) {
                        apiConsumer.addSubscription(apiTypeWrapper, userId, application);
                    } else if (!apiConsumer.isSubscribedToApp(subscribedAPI.getApiId(), userId, application.getId())) {
                        // on update skip subscriptions that already exists
                        apiConsumer.addSubscription(apiTypeWrapper, userId, application);
                    }
                } else {
                    log.error("Failed to import Subscription as API/API Product " + name + "-" + version + " as one or more tiers may be unavailable or the API/API Product may not have been published ");
                    skippedAPIList.add(subscribedAPI.getApiId());
                }
            } else {
                log.error("Failed to import Subscription as API " + name + "-" + version + " is not available");
                skippedAPIList.add(subscribedAPI.getApiId());
            }
        } else {
            log.error("Failed to import Subscription as Tenant domain: " + tenantDomain + " is not available");
            skippedAPIList.add(subscribedAPI.getApiId());
        }
    }
    return skippedAPIList;
}
Also used : ExportedSubscribedAPI(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI) Set(java.util.Set) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) ArrayList(java.util.ArrayList) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) JSONObject(org.json.simple.JSONObject) API(org.wso2.carbon.apimgt.api.model.API) ExportedSubscribedAPI(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI) Map(java.util.Map)

Example 2 with ExportedSubscribedAPI

use of org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI in project carbon-apimgt by wso2.

the class ExportUtils method createApplicationDTOToExport.

/**
 * Create an aggregated Application DTO to be exported.
 *
 * @param application Application{@link Application} to be exported
 * @param apiConsumer API Consumer
 * @param withKeys    Export the Application with keys or not
 * @return Exported application
 * @throws APIManagementException If an error occurs while retrieving subscribed APIs
 */
private static ExportedApplication createApplicationDTOToExport(Application application, APIConsumer apiConsumer, Boolean withKeys) throws APIManagementException {
    ApplicationDTO applicationDto = ApplicationMappingUtil.fromApplicationtoDTO(application);
    // Set keys if withKeys is true
    if (withKeys == null || !withKeys) {
        application.clearOAuthApps();
    } else {
        List<ApplicationKeyDTO> applicationKeyDTOs = new ArrayList<>();
        for (APIKey apiKey : application.getKeys()) {
            // Encode the consumer secret and set it
            apiKey.setConsumerSecret(new String(Base64.encodeBase64(apiKey.getConsumerSecret().getBytes(Charset.defaultCharset()))));
            ApplicationKeyDTO applicationKeyDTO = ApplicationKeyMappingUtil.fromApplicationKeyToDTO(apiKey);
            applicationKeyDTOs.add(applicationKeyDTO);
        }
        applicationDto.setKeys(applicationKeyDTOs);
    }
    // Get the subscribed API details and add it to a set
    Set<SubscribedAPI> subscribedAPIs = apiConsumer.getSubscribedAPIs(application.getSubscriber(), application.getName(), application.getGroupId());
    Set<ExportedSubscribedAPI> exportedSubscribedAPIs = new HashSet<>();
    for (SubscribedAPI subscribedAPI : subscribedAPIs) {
        ExportedSubscribedAPI exportedSubscribedAPI = new ExportedSubscribedAPI(subscribedAPI.getApiId(), subscribedAPI.getSubscriber(), subscribedAPI.getTier().getName());
        exportedSubscribedAPIs.add(exportedSubscribedAPI);
    }
    // Set the subscription count by counting the number of subscribed APIs
    applicationDto.setSubscriptionCount(exportedSubscribedAPIs.size());
    // Set the application
    ExportedApplication exportedApplication = new ExportedApplication(applicationDto);
    // Set the subscribed APIs
    exportedApplication.setSubscribedAPIs(exportedSubscribedAPIs);
    return exportedApplication;
}
Also used : ExportedSubscribedAPI(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI) ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO) APIKey(org.wso2.carbon.apimgt.api.model.APIKey) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ExportedSubscribedAPI(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)2 ExportedSubscribedAPI (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedSubscribedAPI)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 JSONObject (org.json.simple.JSONObject)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)1 APIKey (org.wso2.carbon.apimgt.api.model.APIKey)1 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)1 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationDTO)1 ApplicationKeyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)1 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)1