Search in sources :

Example 46 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class SubscriptionDataStoreImpl method initializeLoadingTasks.

public void initializeLoadingTasks() {
    Runnable apiTask = new PopulateTask<>(apiMap, () -> {
        try {
            log.debug("Calling loadAllApis. ");
            List<API> apiList = new SubscriptionDataLoaderImpl().loadAllApis(tenantDomain);
            apiByUUIDMap.clear();
            for (API api : apiList) {
                apiByUUIDMap.put(api.getUuid(), api);
                String key = api.getApiName().concat(":").concat(api.getApiVersion());
                apiNameVersionMap.put(key, api);
            }
            apisInitialized = true;
            return apiList;
        } catch (APIManagementException e) {
            log.error("Exception while loading APIs " + e);
        }
        return null;
    });
    executorService.schedule(apiTask, 0, TimeUnit.SECONDS);
    Runnable subscriptionLoadingTask = new PopulateTask<>(subscriptionMap, () -> {
        try {
            log.debug("Calling loadAllSubscriptions.");
            return new SubscriptionDataLoaderImpl().loadAllSubscriptions(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Subscriptions " + e);
        }
        return null;
    });
    executorService.schedule(subscriptionLoadingTask, 0, TimeUnit.SECONDS);
    Runnable applicationLoadingTask = new PopulateTask<>(applicationMap, () -> {
        try {
            log.debug("Calling loadAllApplications.");
            return new SubscriptionDataLoaderImpl().loadAllApplications(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Applications " + e);
        }
        return null;
    });
    executorService.schedule(applicationLoadingTask, 0, TimeUnit.SECONDS);
    Runnable keyMappingsTask = new PopulateTask<>(applicationKeyMappingMap, () -> {
        try {
            log.debug("Calling loadAllKeyMappings.");
            return new SubscriptionDataLoaderImpl().loadAllKeyMappings(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading ApplicationKeyMapping " + e);
        }
        return null;
    });
    executorService.schedule(keyMappingsTask, 0, TimeUnit.SECONDS);
    Runnable apiPolicyLoadingTask = new PopulateTask<>(apiPolicyMap, () -> {
        try {
            log.debug("Calling loadAllSubscriptionPolicies.");
            List<ApiPolicy> apiPolicyList = new SubscriptionDataLoaderImpl().loadAllAPIPolicies(tenantDomain);
            apiPoliciesInitialized = true;
            return apiPolicyList;
        } catch (APIManagementException e) {
            log.error("Exception while loading api Policies " + e);
        }
        return null;
    });
    executorService.schedule(apiPolicyLoadingTask, 0, TimeUnit.SECONDS);
    Runnable subPolicyLoadingTask = new PopulateTask<>(subscriptionPolicyMap, () -> {
        try {
            log.debug("Calling loadAllSubscriptionPolicies.");
            return new SubscriptionDataLoaderImpl().loadAllSubscriptionPolicies(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Subscription Policies " + e);
        }
        return null;
    });
    executorService.schedule(subPolicyLoadingTask, 0, TimeUnit.SECONDS);
    Runnable appPolicyLoadingTask = new PopulateTask<>(appPolicyMap, () -> {
        try {
            log.debug("Calling loadAllAppPolicies.");
            return new SubscriptionDataLoaderImpl().loadAllAppPolicies(tenantDomain);
        } catch (APIManagementException e) {
            log.error("Exception while loading Application Policies " + e);
        }
        return null;
    });
    executorService.schedule(appPolicyLoadingTask, 0, TimeUnit.SECONDS);
    Runnable scopesLoadingTask = new PopulateTask<>(scopesMap, () -> {
        try {
            log.debug("Calling loadAllScopes.");
            List<Scope> scopeList = new SubscriptionDataLoaderImpl().loadAllScopes(tenantDomain);
            scopesInitialized = true;
            return scopeList;
        } catch (APIManagementException e) {
            log.error("Exception while loading Scopes " + e);
        }
        return null;
    });
    executorService.schedule(scopesLoadingTask, 0, TimeUnit.SECONDS);
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.keymgt.model.entity.Scope) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) API(org.wso2.carbon.apimgt.keymgt.model.entity.API)

Example 47 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class RecommendationsApiServiceImpl method recommendationsGet.

public Response recommendationsGet(MessageContext messageContext) throws APIManagementException {
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    RecommendationEnvironment recommendationEnvironment = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration().getApiRecommendationEnvironment();
    List<JSONObject> recommendedApis = new ArrayList<>();
    JSONObject responseObj = new JSONObject();
    String apiId = null;
    try {
        String userName = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
        String requestedTenantDomain = apiConsumer.getRequestedTenant();
        if (apiConsumer.isRecommendationEnabled(requestedTenantDomain) && !APIConstants.WSO2_ANONYMOUS_USER.equals(userName)) {
            int maxRecommendations = recommendationEnvironment.getMaxRecommendations();
            String recommendations = apiConsumer.getApiRecommendations(userName, requestedTenantDomain);
            if (recommendations != null) {
                JSONObject jsonResponse = new JSONObject(recommendations);
                JSONArray apiList = jsonResponse.getJSONArray("userRecommendations");
                for (int i = 0; i < apiList.length(); i++) {
                    try {
                        JSONObject apiObj = apiList.getJSONObject(i);
                        apiId = apiObj.getString("id");
                        ApiTypeWrapper apiWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
                        API api = apiWrapper.getApi();
                        APIIdentifier apiIdentifier = api.getId();
                        boolean isApiSubscribed = apiConsumer.isSubscribed(apiIdentifier, userName);
                        if (!isApiSubscribed && recommendedApis.size() < maxRecommendations) {
                            JSONObject apiDetails = new JSONObject();
                            apiDetails.put("id", apiId);
                            apiDetails.put("name", apiWrapper.getName());
                            apiDetails.put("avgRating", api.getRating());
                            recommendedApis.add(apiDetails);
                        }
                    } catch (APIManagementException e) {
                        log.debug("Requested API " + apiId + " is not accessible by the consumer");
                    }
                }
            }
        }
    } catch (Exception e) {
        log.error("Error occurred when retrieving recommendations through the rest api: ", e);
    }
    int count = recommendedApis.size();
    responseObj.put("count", count);
    responseObj.put("list", recommendedApis);
    String responseStringObj = String.valueOf(responseObj);
    return Response.ok().entity(responseStringObj).build();
}
Also used : ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) RecommendationEnvironment(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment)

Example 48 with APIList

use of org.wso2.carbon.apimgt.keymgt.model.entity.APIList in project carbon-apimgt by wso2.

the class ServicesApiServiceImpl method getServiceUsage.

@Override
public Response getServiceUsage(String serviceId, MessageContext messageContext) {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    try {
        List<API> apiList = serviceCatalog.getServiceUsage(serviceId, tenantId);
        if (apiList != null) {
            APIListDTO apiListDTO = new APIListDTO();
            List<APIInfoDTO> apiInfoDTOList = new ArrayList<>();
            for (API api : apiList) {
                apiInfoDTOList.add(ServiceEntryMappingUtil.fromAPIToAPIInfoDTO(api));
            }
            apiListDTO.setList(apiInfoDTOList);
            apiListDTO.setCount(apiList.size());
            return Response.ok().entity(apiListDTO).build();
        } else {
            RestApiUtil.handleResourceNotFoundError("Service", serviceId, log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving API usage of service";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) APIListDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.APIListDTO) API(org.wso2.carbon.apimgt.api.model.API) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.APIInfoDTO)

Aggregations

ArrayList (java.util.ArrayList)33 API (org.wso2.carbon.apimgt.core.models.API)21 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)15 API (org.wso2.carbon.apimgt.api.model.API)14 Test (org.testng.annotations.Test)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)12 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)12 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 JSONObject (org.json.simple.JSONObject)9 APIComparator (org.wso2.carbon.apimgt.core.util.APIComparator)8 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)7 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)6 TreeSet (java.util.TreeSet)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)5 IOException (java.io.IOException)4 ResultSet (java.sql.ResultSet)4 List (java.util.List)4