Search in sources :

Example 31 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class AdditionalSubscriptionInfoMappingUtil method fromAdditionalSubscriptionInfoToDTO.

/**
 * Converts a AdditionalSubscriptionInfo object into AdditionalSubscriptionInfoDTO
 *
 * @param subscription SubscribedAPI object
 * @param organization Identifier of the organization
 * @return SubscriptionDTO corresponds to SubscribedAPI object
 */
public static AdditionalSubscriptionInfoDTO fromAdditionalSubscriptionInfoToDTO(SubscribedAPI subscription, String organization) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    AdditionalSubscriptionInfoDTO additionalSubscriptionInfoDTO = new AdditionalSubscriptionInfoDTO();
    additionalSubscriptionInfoDTO.setSubscriptionId(subscription.getUUID());
    APIIdentifier apiId = subscription.getApiId();
    API api = null;
    if (apiId != null) {
        try {
            api = apiConsumer.getLightweightAPIByUUID(apiId.getUUID(), organization);
        } catch (APIManagementException e) {
            String msg = "User :" + username + " does not have access to the API " + apiId;
            RestApiUtil.handleInternalServerError(msg, e, log);
        }
    }
    additionalSubscriptionInfoDTO.setApiId(api.getUuid());
    // Set Application information
    Application application = subscription.getApplication();
    application = apiConsumer.getApplicationByUUID(application.getUUID());
    additionalSubscriptionInfoDTO.setApplicationId(subscription.getApplication().getUUID());
    additionalSubscriptionInfoDTO.setApplicationName(application.getName());
    additionalSubscriptionInfoDTO.setIsSolaceAPI(SolaceNotifierUtils.checkWhetherAPIDeployedToSolaceUsingRevision(api));
    if (additionalSubscriptionInfoDTO.isIsSolaceAPI()) {
        // Set Solace organization details if API is a Solace API
        additionalSubscriptionInfoDTO.setSolaceOrganization(SolaceNotifierUtils.getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment(api));
        Map<String, Environment> gatewayEnvironmentMap = APIUtil.getReadOnlyGatewayEnvironments();
        Environment solaceEnvironment = null;
        for (Map.Entry<String, Environment> entry : gatewayEnvironmentMap.entrySet()) {
            if (SolaceConstants.SOLACE_ENVIRONMENT.equals(entry.getValue().getProvider())) {
                solaceEnvironment = entry.getValue();
            }
        }
        if (solaceEnvironment != null) {
            List<SolaceDeployedEnvironmentDTO> solaceDeployedEnvironmentsDTOS = SolaceStoreUtils.getSolaceDeployedEnvsInfo(solaceEnvironment, additionalSubscriptionInfoDTO.getSolaceOrganization(), application.getUUID());
            List<AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO> solaceEnvironments = new ArrayList<>();
            for (SolaceDeployedEnvironmentDTO solaceDeployedEnvironmentEntry : solaceDeployedEnvironmentsDTOS) {
                // Set Solace environment details
                AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO solaceDeployedEnvironmentsDTO = new AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO();
                solaceDeployedEnvironmentsDTO.setEnvironmentName(solaceDeployedEnvironmentEntry.getEnvironmentName());
                solaceDeployedEnvironmentsDTO.setEnvironmentDisplayName(solaceDeployedEnvironmentEntry.getEnvironmentDisplayName());
                solaceDeployedEnvironmentsDTO.setOrganizationName(solaceDeployedEnvironmentEntry.getOrganizationName());
                // Set Solace URLs
                List<AdditionalSubscriptionInfoSolaceURLsDTO> endpointUrls = new ArrayList<>();
                List<SolaceURLsDTO> solaceURLsDTOS = solaceDeployedEnvironmentEntry.getSolaceURLs();
                for (SolaceURLsDTO entry : solaceURLsDTOS) {
                    AdditionalSubscriptionInfoSolaceURLsDTO solaceURLsDTO = new AdditionalSubscriptionInfoSolaceURLsDTO();
                    solaceURLsDTO.setProtocol(entry.getProtocol());
                    solaceURLsDTO.setEndpointURL(entry.getEndpointURL());
                    endpointUrls.add(solaceURLsDTO);
                }
                solaceDeployedEnvironmentsDTO.setSolaceURLs(endpointUrls);
                // Set Solace Topic Objects
                solaceDeployedEnvironmentsDTO.setSolaceTopicsObject(mapSolaceTopicObjects(solaceDeployedEnvironmentEntry.getSolaceTopicsObject()));
                solaceEnvironments.add(solaceDeployedEnvironmentsDTO);
            }
            additionalSubscriptionInfoDTO.setSolaceDeployedEnvironments(solaceEnvironments);
        }
    }
    return additionalSubscriptionInfoDTO;
}
Also used : AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceDeployedEnvironmentsDTO) ArrayList(java.util.ArrayList) AdditionalSubscriptionInfoSolaceURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceURLsDTO) SolaceURLsDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO) AdditionalSubscriptionInfoSolaceURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoSolaceURLsDTO) AdditionalSubscriptionInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdditionalSubscriptionInfoDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) API(org.wso2.carbon.apimgt.api.model.API) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Application(org.wso2.carbon.apimgt.api.model.Application) Map(java.util.Map) SolaceDeployedEnvironmentDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceDeployedEnvironmentDTO)

Example 32 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI 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 33 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class SolaceKeyGenNotifier method syncSolaceApplicationClientId.

/**
 * Syncing consumer key of the dev portal applications with applications on the Solace broker
 *
 * @param event ApplicationEvent to sync Solace applications with dev portal applications
 * @throws NotifierException if error occurs when patching applications on the Solace broker
 */
private void syncSolaceApplicationClientId(ApplicationRegistrationEvent event) throws NotifierException {
    // get list of subscribed APIs in the application
    try {
        Application application = apiMgtDAO.getApplicationByUUID(event.getApplicationUUID());
        Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
        Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(application.getSubscriber(), application.getName(), application.getGroupId());
        boolean isContainsSolaceApis = false;
        String organizationNameOfSolaceDeployment = null;
        labelOne: // Check whether the application needs to be updated has a Solace API subscription
        for (SubscribedAPI api : subscriptions) {
            List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getIdentifier().getUUID());
            for (APIRevisionDeployment deployment : deployments) {
                if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
                    if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
                        isContainsSolaceApis = true;
                        organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
                        break labelOne;
                    }
                }
            }
        }
        // Patching consumerKey to Solace application using Admin Apis
        if (isContainsSolaceApis) {
            if (application.getKeys() != null) {
                String consumerSecret = null;
                APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(CarbonContext.getThreadLocalCarbonContext().getUsername());
                Set<APIKey> consumerKeys = apiConsumer.getApplicationKeysOfApplication(application.getId());
                for (APIKey key : consumerKeys) {
                    if (key.getConsumerKey().equals(event.getConsumerKey())) {
                        consumerSecret = key.getConsumerSecret();
                    }
                }
                SolaceNotifierUtils.patchSolaceApplicationClientId(organizationNameOfSolaceDeployment, application, event.getConsumerKey(), consumerSecret);
            } else {
                throw new NotifierException("Application keys are not found in the application : " + application.getName());
            }
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException) APIKey(org.wso2.carbon.apimgt.api.model.APIKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) List(java.util.List) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 34 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class SubscriptionMappingUtil method fromSubscriptionListToDTO.

/**
 * Converts a List object of SubscribedAPIs into a DTO
 *
 * @param subscriptions a list of SubscribedAPI objects
 * @param query         query to filter subscriptions
 * @return SubscriptionListDTO object containing SubscriptionDTOs
 */
public static SubscriptionListDTO fromSubscriptionListToDTO(List<SubscribedAPI> subscriptions, String query) throws APIManagementException {
    SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
    List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
    if (subscriptionDTOs == null) {
        subscriptionDTOs = new ArrayList<>();
        subscriptionListDTO.setList(subscriptionDTOs);
    }
    query = query.toLowerCase().trim();
    for (SubscribedAPI sub : subscriptions) {
        SubscriptionDTO subscription = fromSubscriptionToDTO(sub);
        if (subscription.getApplicationInfo().getName().toLowerCase().contains(query) || subscription.getApplicationInfo().getSubscriber().toLowerCase().contains(query) || subscription.getThrottlingPolicy().toLowerCase().contains(query)) {
            subscriptionDTOs.add(subscription);
        }
    }
    subscriptionListDTO.setCount(subscriptionDTOs.size());
    return subscriptionListDTO;
}
Also used : SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionDTO) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)

Example 35 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class SubscriptionMappingUtil method fromSubscriptionToDTO.

/**
 * Converts a SubscribedAPI object into SubscriptionDTO
 *
 * @param subscription SubscribedAPI object
 * @return SubscriptionDTO corresponds to SubscribedAPI object
 */
public static SubscriptionDTO fromSubscriptionToDTO(SubscribedAPI subscription) throws APIManagementException {
    SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
    subscriptionDTO.setSubscriptionId(subscription.getUUID());
    subscriptionDTO.setApplicationInfo(fromApplicationToApplicationInfoDTO(subscription.getApplication()));
    subscriptionDTO.setSubscriptionStatus(SubscriptionDTO.SubscriptionStatusEnum.valueOf(subscription.getSubStatus()));
    subscriptionDTO.setThrottlingPolicy(subscription.getTier().getName());
    return subscriptionDTO;
}
Also used : SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionDTO)

Aggregations

SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)54 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 Application (org.wso2.carbon.apimgt.api.model.Application)28 Tier (org.wso2.carbon.apimgt.api.model.Tier)23 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)16 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)14 ArrayList (java.util.ArrayList)12 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)12 HashSet (java.util.HashSet)11 TreeMap (java.util.TreeMap)11 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)11 Connection (java.sql.Connection)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 ResultSet (java.sql.ResultSet)9 JSONObject (net.minidev.json.JSONObject)9 API (org.wso2.carbon.apimgt.api.model.API)9