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);
}
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();
}
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;
}
Aggregations