Search in sources :

Example 6 with SubscriptionDataLoaderImpl

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

the class WebSocketAnalyticsDataProvider method getApi.

@Override
public API getApi() {
    String apiContext = (String) WebSocketUtils.getPropertyFromChannel(RESTConstants.REST_API_CONTEXT, ctx);
    String apiVersion = (String) WebSocketUtils.getPropertyFromChannel(RESTConstants.SYNAPSE_REST_API_VERSION, ctx);
    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 RuntimeException("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) API(org.wso2.carbon.apimgt.common.analytics.publishers.dto.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)

Example 7 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, Application app, Subscription sub) {
    String subscriptionStatus = sub.getSubscriptionState();
    String type = app.getTokenType();
    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 subscriberUserId = sub.getSubscriptionId();
    String subscriberTenant = MultitenantUtils.getTenantDomain(app.getSubName());
    ApplicationPolicy appPolicy = datastore.getApplicationPolicyByName(app.getPolicy(), tenantId);
    if (appPolicy == null) {
        try {
            appPolicy = new SubscriptionDataLoaderImpl().getApplicationPolicy(app.getPolicy(), apiTenantDomain);
            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 8 with SubscriptionDataLoaderImpl

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

the class APIKeyValidationService method getTemplates.

private ArrayList<URITemplate> getTemplates(String context, String version) throws APIManagementException {
    String tenantDomain = MultitenantUtils.getTenantDomainFromRequestURL(context);
    if (tenantDomain == null) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    ArrayList<URITemplate> templates = new ArrayList<URITemplate>();
    SubscriptionDataStore store = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    if (store == null) {
        return templates;
    }
    API api = store.getApiByContextAndVersion(context, version);
    if (api == null) {
        log.debug("SubscriptionDataStore didn't contains API metadata reading from rest api context: " + context + " And version " + version);
        api = new SubscriptionDataLoaderImpl().getApi(context, version);
        if (api != null) {
            store.addOrUpdateAPI(api);
            if (log.isDebugEnabled()) {
                log.debug("Update SubscriptionDataStore api for " + api.getCacheKey());
            }
        }
    }
    if (api == null || api.getApiId() == 0) {
        return templates;
    }
    List<URLMapping> mapping = api.getResources();
    if (mapping == null || mapping.isEmpty()) {
        return templates;
    }
    int apiTenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(api.getApiProvider()));
    if (log.isDebugEnabled()) {
        log.debug("Tenant domain: " + tenantDomain + " tenantId: " + apiTenantId);
    }
    ApiPolicy apiPolicy;
    URITemplate template;
    for (URLMapping urlMapping : mapping) {
        template = new URITemplate();
        template.setHTTPVerb(urlMapping.getHttpMethod());
        template.setAuthType(urlMapping.getAuthScheme());
        template.setUriTemplate(urlMapping.getUrlPattern());
        template.setThrottlingTier(urlMapping.getThrottlingPolicy());
        if (store.isApiPoliciesInitialized()) {
            log.debug("SubscriptionDataStore Initialized. Reading API Policies from SubscriptionDataStore");
            apiPolicy = store.getApiPolicyByName(urlMapping.getThrottlingPolicy(), apiTenantId);
            if (apiPolicy == null) {
                // could be null for situations where invoke before map is updated
                log.debug("API Policies not found in the SubscriptionDataStore. Retrieving from the Rest API");
                apiPolicy = new SubscriptionDataLoaderImpl().getAPIPolicy(urlMapping.getThrottlingPolicy(), tenantDomain);
                if (apiPolicy != null) {
                    if (apiPolicy.getName() != null) {
                        store.addOrUpdateApiPolicy(apiPolicy);
                        if (log.isDebugEnabled()) {
                            log.debug("Update SubscriptionDataStore API Policy for " + apiPolicy.getCacheKey());
                        }
                    } else {
                        throw new APIManagementException("Exception while loading api policy for " + urlMapping.getThrottlingPolicy() + " for domain " + tenantDomain);
                    }
                }
            }
        } else {
            log.debug("SubscriptionDataStore not Initialized. Reading API Policies from Rest API");
            apiPolicy = new SubscriptionDataLoaderImpl().getAPIPolicy(urlMapping.getThrottlingPolicy(), tenantDomain);
            if (apiPolicy != null) {
                if (apiPolicy.getName() != null) {
                    store.addOrUpdateApiPolicy(apiPolicy);
                    if (log.isDebugEnabled()) {
                        log.debug("Update SubscriptionDataStore API Policy for " + apiPolicy.getCacheKey());
                    }
                } else {
                    throw new APIManagementException("Exception while loading api policy for " + urlMapping.getThrottlingPolicy() + " for domain " + tenantDomain);
                }
            }
        }
        List<String> tiers = new ArrayList<String>();
        tiers.add(urlMapping.getThrottlingPolicy() + ">" + apiPolicy.isContentAware());
        template.setThrottlingTiers(tiers);
        template.setApplicableLevel(apiPolicy.getApplicableLevel());
        List<APIPolicyConditionGroup> conditions = apiPolicy.getConditionGroups();
        List<ConditionGroupDTO> conditionGroupsList = new ArrayList<ConditionGroupDTO>();
        for (APIPolicyConditionGroup cond : conditions) {
            Set<Condition> condSet = cond.getCondition();
            if (condSet.isEmpty()) {
                continue;
            }
            List<ConditionDTO> conditionDtoList = new ArrayList<ConditionDTO>();
            for (Condition condition : condSet) {
                ConditionDTO item = new ConditionDTO();
                item.setConditionName(condition.getName());
                item.setConditionType(condition.getConditionType());
                item.setConditionValue(condition.getValue());
                item.isInverted(condition.isInverted());
                conditionDtoList.add(item);
            }
            ConditionGroupDTO group = new ConditionGroupDTO();
            group.setConditionGroupId("_condition_" + cond.getConditionGroupId());
            group.setConditions(conditionDtoList.toArray(new ConditionDTO[] {}));
            conditionGroupsList.add(group);
        }
        ConditionGroupDTO defaultGroup = new ConditionGroupDTO();
        defaultGroup.setConditionGroupId(APIConstants.THROTTLE_POLICY_DEFAULT);
        conditionGroupsList.add(defaultGroup);
        template.getThrottlingConditions().add(APIConstants.THROTTLE_POLICY_DEFAULT);
        template.setConditionGroups(conditionGroupsList.toArray(new ConditionGroupDTO[] {}));
        templates.add(template);
    }
    return templates;
}
Also used : Condition(org.wso2.carbon.apimgt.keymgt.model.entity.Condition) APIPolicyConditionGroup(org.wso2.carbon.apimgt.keymgt.model.entity.APIPolicyConditionGroup) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) ApiPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) SubscriptionDataLoaderImpl(org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl) URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) API(org.wso2.carbon.apimgt.keymgt.model.entity.API)

Example 9 with SubscriptionDataLoaderImpl

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

the class SubscriptionDataStoreImpl method getSubscriptionById.

@Override
public Subscription getSubscriptionById(int appId, int apiId) {
    String subscriptionCacheKey = SubscriptionDataStoreUtil.getSubscriptionCacheKey(appId, apiId);
    String synchronizeKey = "SubscriptionDataStoreImpl-Subscription-" + subscriptionCacheKey;
    Subscription subscription = subscriptionMap.get(subscriptionCacheKey);
    if (subscription == null) {
        synchronized (synchronizeKey.intern()) {
            subscription = subscriptionMap.get(subscriptionCacheKey);
            if (subscription != null) {
                return subscription;
            }
            try {
                subscription = new SubscriptionDataLoaderImpl().getSubscriptionById(Integer.toString(apiId), Integer.toString(appId));
            } catch (DataLoadingException e) {
                log.error("Error while Retrieving Subscription Data From Internal API", e);
            }
            if (subscription != null && !StringUtils.isEmpty(subscription.getSubscriptionId())) {
                // load to the memory
                log.debug("Loading Subscription to the in-memory datastore.");
                subscriptionMap.put(subscription.getCacheKey(), subscription);
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Retrieving API Subscription with Application " + appId + " and APIId : " + apiId);
        if (subscription != null) {
            log.debug("Retrieved API Subscription with Application " + appId + " and APIId : " + apiId + " is " + subscription.toString());
        } else {
            log.debug("Retrieved API Subscription with Application " + appId + " and APIId : " + apiId + " is " + "empty.");
        }
    }
    return subscription;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) Subscription(org.wso2.carbon.apimgt.keymgt.model.entity.Subscription)

Example 10 with SubscriptionDataLoaderImpl

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

the class SubscriptionDataStoreImpl method getKeyMappingByKeyAndKeyManager.

@Override
public ApplicationKeyMapping getKeyMappingByKeyAndKeyManager(String key, String keyManager) {
    ApplicationKeyMappingCacheKey applicationKeyMappingCacheKey = new ApplicationKeyMappingCacheKey(key, keyManager);
    String synchronizeKey = "SubscriptionDataStoreImpl-KeyMapping-" + applicationKeyMappingCacheKey;
    ApplicationKeyMapping applicationKeyMapping = applicationKeyMappingMap.get(applicationKeyMappingCacheKey);
    if (applicationKeyMapping == null) {
        synchronized (synchronizeKey.intern()) {
            applicationKeyMapping = applicationKeyMappingMap.get(applicationKeyMappingCacheKey);
            if (applicationKeyMapping != null) {
                return applicationKeyMapping;
            }
            try {
                applicationKeyMapping = new SubscriptionDataLoaderImpl().getKeyMapping(key, keyManager, tenantDomain);
            } catch (DataLoadingException e) {
                log.error("Error while Loading KeyMapping Information from Internal API.", e);
            }
            if (applicationKeyMapping != null && !StringUtils.isEmpty(applicationKeyMapping.getConsumerKey())) {
                // load to the memory
                log.debug("Loading Keymapping to the in-memory datastore.");
                addOrUpdateApplicationKeyMapping(applicationKeyMapping);
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Retrieving Application information with Consumer Key : " + key + " and keymanager : " + keyManager);
        if (applicationKeyMapping != null) {
            log.debug("Retrieved Application information with Consumer Key : " + key + " and keymanager : " + keyManager + " is " + applicationKeyMapping.toString());
        } else {
            log.debug("Retrieving Application information with Consumer Key : " + key + " and keymanager : " + keyManager + " is empty");
        }
    }
    return applicationKeyMapping;
}
Also used : DataLoadingException(org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException) ApplicationKeyMappingCacheKey(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMappingCacheKey) ApplicationKeyMapping(org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationKeyMapping)

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