Search in sources :

Example 1 with SubscriptionDataLoaderImpl

use of org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl in project carbon-apimgt by wso2.

the class SynapseAnalyticsDataProvider method getApi.

@Override
public API getApi() throws DataNotFoundException {
    String apiContext = (String) messageContext.getProperty(RESTConstants.REST_API_CONTEXT);
    String apiVersion = (String) messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
    String tenantDomain = MultitenantUtils.getTenantDomainFromRequestURL(apiContext);
    if (tenantDomain == null) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    SubscriptionDataStore store = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    org.wso2.carbon.apimgt.keymgt.model.entity.API apiObj = store.getApiByContextAndVersion(apiContext, apiVersion);
    API api = new API();
    if (apiObj == null) {
        try {
            apiObj = new SubscriptionDataLoaderImpl().getApi(apiContext, apiVersion);
        } catch (DataLoadingException e) {
            log.error("Error occurred when getting api.", e);
            throw new DataNotFoundException("Error occurred when getting API information", e);
        }
    }
    if (apiObj != null) {
        api.setApiId(apiObj.getUuid());
        api.setApiType(apiObj.getApiType());
        api.setApiName(apiObj.getApiName());
        api.setApiVersion(apiObj.getApiVersion());
        api.setApiCreator(apiObj.getApiProvider());
        api.setApiCreatorTenantDomain(MultitenantUtils.getTenantDomain(api.getApiCreator()));
    }
    return api;
}
Also used : SubscriptionDataLoaderImpl(org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl) DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) DataNotFoundException(org.wso2.carbon.apimgt.common.analytics.exceptions.DataNotFoundException) API(org.wso2.carbon.apimgt.common.analytics.publishers.dto.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)

Example 2 with SubscriptionDataLoaderImpl

use of org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl in project carbon-apimgt by wso2.

the class AbstractKeyValidationHandler method validate.

private APIKeyValidationInfoDTO validate(APIKeyValidationInfoDTO infoDTO, String apiTenantDomain, int tenantId, SubscriptionDataStore datastore, API api, ApplicationKeyMapping key, Application app, Subscription sub, String keyManager) {
    String subscriptionStatus = sub.getSubscriptionState();
    String type = key.getKeyType();
    if (APIConstants.SubscriptionStatus.BLOCKED.equals(subscriptionStatus)) {
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
        infoDTO.setAuthorized(false);
        return infoDTO;
    } else if (APIConstants.SubscriptionStatus.ON_HOLD.equals(subscriptionStatus) || APIConstants.SubscriptionStatus.REJECTED.equals(subscriptionStatus)) {
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.SUBSCRIPTION_INACTIVE);
        infoDTO.setAuthorized(false);
        return infoDTO;
    } else if (APIConstants.SubscriptionStatus.PROD_ONLY_BLOCKED.equals(subscriptionStatus) && !APIConstants.API_KEY_TYPE_SANDBOX.equals(type)) {
        infoDTO.setValidationStatus(APIConstants.KeyValidationStatus.API_BLOCKED);
        infoDTO.setType(type);
        infoDTO.setAuthorized(false);
        return infoDTO;
    }
    infoDTO.setTier(sub.getPolicyId());
    infoDTO.setSubscriber(app.getSubName());
    infoDTO.setApplicationId(app.getId().toString());
    infoDTO.setApiName(api.getApiName());
    infoDTO.setApiVersion(api.getApiVersion());
    infoDTO.setApiPublisher(api.getApiProvider());
    infoDTO.setApplicationName(app.getName());
    infoDTO.setApplicationTier(app.getPolicy());
    infoDTO.setApplicationUUID(app.getUUID());
    infoDTO.setAppAttributes(app.getAttributes());
    infoDTO.setType(type);
    // Advanced Level Throttling Related Properties
    String apiTier = api.getApiTier();
    String subscriberTenant = MultitenantUtils.getTenantDomain(app.getSubName());
    ApplicationPolicy appPolicy = datastore.getApplicationPolicyByName(app.getPolicy(), APIUtil.getTenantIdFromTenantDomain(app.getOrganization()));
    if (appPolicy == null) {
        try {
            appPolicy = new SubscriptionDataLoaderImpl().getApplicationPolicy(app.getPolicy(), app.getOrganization());
            datastore.addOrUpdateApplicationPolicy(appPolicy);
        } catch (DataLoadingException e) {
            log.error("Error while loading ApplicationPolicy");
        }
    }
    SubscriptionPolicy subPolicy = datastore.getSubscriptionPolicyByName(sub.getPolicyId(), tenantId);
    if (subPolicy == null) {
        try {
            subPolicy = new SubscriptionDataLoaderImpl().getSubscriptionPolicy(sub.getPolicyId(), apiTenantDomain);
            datastore.addOrUpdateSubscriptionPolicy(subPolicy);
        } catch (DataLoadingException e) {
            log.error("Error while loading SubscriptionPolicy");
        }
    }
    ApiPolicy apiPolicy = datastore.getApiPolicyByName(api.getApiTier(), tenantId);
    boolean isContentAware = false;
    if (appPolicy.isContentAware() || subPolicy.isContentAware() || (apiPolicy != null && apiPolicy.isContentAware())) {
        isContentAware = true;
    }
    infoDTO.setContentAware(isContentAware);
    // TODO this must implement as a part of throttling implementation.
    int spikeArrest = 0;
    String apiLevelThrottlingKey = "api_level_throttling_key";
    if (subPolicy.getRateLimitCount() > 0) {
        spikeArrest = subPolicy.getRateLimitCount();
    }
    String spikeArrestUnit = null;
    if (subPolicy.getRateLimitTimeUnit() != null) {
        spikeArrestUnit = subPolicy.getRateLimitTimeUnit();
    }
    boolean stopOnQuotaReach = subPolicy.isStopOnQuotaReach();
    int graphQLMaxDepth = 0;
    if (subPolicy.getGraphQLMaxDepth() > 0) {
        graphQLMaxDepth = subPolicy.getGraphQLMaxDepth();
    }
    int graphQLMaxComplexity = 0;
    if (subPolicy.getGraphQLMaxComplexity() > 0) {
        graphQLMaxComplexity = subPolicy.getGraphQLMaxComplexity();
    }
    List<String> list = new ArrayList<String>();
    list.add(apiLevelThrottlingKey);
    infoDTO.setSpikeArrestLimit(spikeArrest);
    infoDTO.setSpikeArrestUnit(spikeArrestUnit);
    infoDTO.setStopOnQuotaReach(stopOnQuotaReach);
    infoDTO.setSubscriberTenantDomain(subscriberTenant);
    infoDTO.setGraphQLMaxDepth(graphQLMaxDepth);
    infoDTO.setGraphQLMaxComplexity(graphQLMaxComplexity);
    if (apiTier != null && apiTier.trim().length() > 0) {
        infoDTO.setApiTier(apiTier);
    }
    // We also need to set throttling data list associated with given API. This need to have
    // policy id and
    // condition id list for all throttling tiers associated with this API.
    infoDTO.setThrottlingDataList(list);
    infoDTO.setAuthorized(true);
    return infoDTO;
}
Also used : SubscriptionDataLoaderImpl(org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl) DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) SubscriptionPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) ArrayList(java.util.ArrayList)

Example 3 with SubscriptionDataLoaderImpl

use of org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl in project carbon-apimgt by wso2.

the class SubscriptionDataStoreImpl method addOrUpdateAPIWithUrlTemplates.

@Override
public void addOrUpdateAPIWithUrlTemplates(API api) {
    try {
        API newAPI = new SubscriptionDataLoaderImpl().getApi(api.getContext(), api.getApiVersion());
        if (newAPI != null) {
            apiMap.put(api.getCacheKey(), newAPI);
            String key = newAPI.getApiName().concat(":").concat(newAPI.getApiVersion());
            apiNameVersionMap.put(key, newAPI);
            apiByUUIDMap.put(newAPI.getUuid(), newAPI);
        }
    } catch (DataLoadingException e) {
        log.error("Exception while loading api for " + api.getContext() + " " + api.getApiVersion(), e);
    }
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) API(org.wso2.carbon.apimgt.keymgt.model.entity.API)

Example 4 with SubscriptionDataLoaderImpl

use of org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl in project carbon-apimgt by wso2.

the class SubscriptionDataStoreImpl method getApplicationById.

@Override
public Application getApplicationById(int appId) {
    String synchronizeKey = "SubscriptionDataStoreImpl-Application-" + appId;
    Application application = applicationMap.get(appId);
    if (application == null) {
        synchronized (synchronizeKey.intern()) {
            application = applicationMap.get(appId);
            if (application != null) {
                return application;
            }
        }
        try {
            application = new SubscriptionDataLoaderImpl().getApplicationById(appId);
        } catch (DataLoadingException e) {
            log.error("Error while Retrieving Application Metadata From Internal API.", e);
        }
        if (application != null && application.getId() != null && application.getId() != 0) {
            // load to the memory
            log.debug("Loading Application to the in-memory datastore. applicationId = " + application.getId());
            addOrUpdateApplication(application);
        } else {
            log.debug("Application not found. applicationId = " + appId);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Retrieving Application information with Application Id : " + appId);
        if (application != null) {
            log.debug("Retrieved Application :" + application.toString());
        } else {
            log.debug("Retrieved Application information with Application Id : " + appId + " is empty");
        }
    }
    return application;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) Application(org.wso2.carbon.apimgt.keymgt.model.entity.Application)

Example 5 with SubscriptionDataLoaderImpl

use of org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl in project carbon-apimgt by wso2.

the class SubscriptionDataStoreImpl method getApiByContextAndVersion.

@Override
public API getApiByContextAndVersion(String context, String version) {
    String key = context + DELEM_PERIOD + version;
    String synchronizeKey = "SubscriptionDataStoreImpl-API-" + key;
    API api = apiMap.get(key);
    if (api == null) {
        synchronized (synchronizeKey.intern()) {
            api = apiMap.get(key);
            if (api != null) {
                return api;
            }
            try {
                api = new SubscriptionDataLoaderImpl().getApi(context, version);
            } catch (DataLoadingException e) {
                log.error("Error while Retrieving Data From Internal Rest API", e);
            }
            if (api != null && api.getApiId() != 0) {
                // load to the memory
                log.debug("Loading API to the in-memory datastore.");
                addOrUpdateAPI(api);
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Retrieving API information with Context " + context + " and Version : " + version);
        if (api != null) {
            log.debug("Retrieved API information with Context  : " + context + " and Version : " + version + " is" + " " + api.toString());
        } else {
            log.debug("Retrieved API information with Context  : " + context + " and Version : " + version + " is" + " empty");
        }
    }
    return api;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) API(org.wso2.carbon.apimgt.keymgt.model.entity.API)

Aggregations

DataLoadingException (org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException)10 ApiPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy)5 SubscriptionDataLoaderImpl (org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl)5 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)4 ArrayList (java.util.ArrayList)3 SubscriptionDataStore (org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 API (org.wso2.carbon.apimgt.common.analytics.publishers.dto.API)2 ApplicationPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy)2 SubscriptionPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy)2 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)1 ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)1 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)1 URLMapping (org.wso2.carbon.apimgt.api.model.subscription.URLMapping)1 DataNotFoundException (org.wso2.carbon.apimgt.common.analytics.exceptions.DataNotFoundException)1 APIPolicyConditionGroup (org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyConditionGroup)1 Application (org.wso2.carbon.apimgt.keymgt.model.entity.Application)1 ApplicationKeyMapping (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping)1 ApplicationKeyMappingCacheKey (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMappingCacheKey)1 Condition (org.wso2.carbon.apimgt.keymgt.model.entity.Condition)1