Search in sources :

Example 36 with SubscriptionDataStore

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

the class Utils method getSelectedAPIList.

public static TreeMap<String, org.wso2.carbon.apimgt.keymgt.model.entity.API> getSelectedAPIList(String path, String tenantDomain) {
    TreeMap<String, org.wso2.carbon.apimgt.keymgt.model.entity.API> selectedAPIMap = new TreeMap<>(new ContextLengthSorter());
    SubscriptionDataStore tenantSubscriptionStore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
    if (tenantSubscriptionStore != null) {
        Map<String, org.wso2.carbon.apimgt.keymgt.model.entity.API> contextAPIMap = tenantSubscriptionStore.getAllAPIsByContextList();
        if (contextAPIMap != null) {
            contextAPIMap.forEach((context, api) -> {
                if (ApiUtils.matchApiPath(path, context)) {
                    selectedAPIMap.put(context, api);
                }
            });
        }
    }
    return selectedAPIMap;
}
Also used : API(org.apache.synapse.api.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)

Example 37 with SubscriptionDataStore

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

the class GatewayUtilsTestCase method testGetAPI.

@Test
public void testGetAPI() {
    API api = new API();
    api.setApiName("api1");
    api.setApiVersion("1.0.0");
    api.setApiProvider("admin");
    MessageContext messageContext = Mockito.mock(MessageContext.class);
    SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
    Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
    SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
    Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn(subscriptionDataStore);
    Mockito.when(subscriptionDataStore.getApiByContextAndVersion("/abc", "1.0.0")).thenReturn(api);
    Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/abc");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
    Assert.assertEquals(GatewayUtils.getAPI(messageContext), api);
    Assert.assertEquals(GatewayUtils.getAPINameFromContextAndVersion(messageContext), "api1");
    Assert.assertEquals(GatewayUtils.getApiProviderFromContextAndVersion(messageContext), "admin");
    Mockito.verify(messageContext, Mockito.times(6)).getProperty(APIMgtGatewayConstants.API_OBJECT);
    Mockito.verify(messageContext, Mockito.times(3)).getProperty(RESTConstants.REST_API_CONTEXT);
    Mockito.verify(messageContext, Mockito.times(3)).getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
    Mockito.verify(subscriptionDataStore, Mockito.times(3)).getApiByContextAndVersion("/abc", "1.0.0");
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) MessageContext(org.apache.synapse.MessageContext) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) SubscriptionDataHolder(org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with SubscriptionDataStore

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

the class GatewayUtilsTestCase method testGetAPIasNullWhenTenantSubscriptionStoreNull.

@Test
public void testGetAPIasNullWhenTenantSubscriptionStoreNull() {
    MessageContext messageContext = Mockito.mock(MessageContext.class);
    SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
    Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
    SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
    Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn(null);
    Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/abc1");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
    Assert.assertNull(GatewayUtils.getAPI(messageContext));
    Assert.assertNull(GatewayUtils.getStatus(messageContext));
    Mockito.verify(messageContext, Mockito.times(0)).setProperty(Mockito.anyString(), Mockito.any());
    Mockito.verify(messageContext, Mockito.times(4)).getProperty(APIMgtGatewayConstants.API_OBJECT);
    Mockito.verify(messageContext, Mockito.times(2)).getProperty(RESTConstants.REST_API_CONTEXT);
    Mockito.verify(messageContext, Mockito.times(2)).getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
    Mockito.verify(subscriptionDataStore, Mockito.times(0)).getApiByContextAndVersion("/abc1", "1.0.0");
}
Also used : MessageContext(org.apache.synapse.MessageContext) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) SubscriptionDataHolder(org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 39 with SubscriptionDataStore

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

the class GatewayUtilsTestCase method testGetAPIFromProperty.

@Test
public void testGetAPIFromProperty() {
    API api = Mockito.mock(API.class);
    MessageContext messageContext = Mockito.mock(MessageContext.class);
    SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
    Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
    SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
    Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn(subscriptionDataStore);
    Mockito.when(subscriptionDataStore.getApiByContextAndVersion("/abc", "1.0.0")).thenReturn(api);
    Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/abc");
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
    Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.API_OBJECT)).thenReturn(api);
    Assert.assertEquals(GatewayUtils.getAPI(messageContext), api);
    Mockito.verify(messageContext, Mockito.times(1)).getProperty(APIMgtGatewayConstants.API_OBJECT);
    Mockito.verify(messageContext, Mockito.times(0)).getProperty(RESTConstants.REST_API_CONTEXT);
    Mockito.verify(messageContext, Mockito.times(0)).getProperty(RESTConstants.SYNAPSE_REST_API_VERSION);
    Mockito.verify(subscriptionDataStore, Mockito.times(0)).getApiByContextAndVersion("/abc", "1.0.0");
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) MessageContext(org.apache.synapse.MessageContext) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) SubscriptionDataHolder(org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 40 with SubscriptionDataStore

use of org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore 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)

Aggregations

SubscriptionDataStore (org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)43 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)14 SubscriptionDataHolder (org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder)9 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 ArrayList (java.util.ArrayList)7 Application (org.wso2.carbon.apimgt.keymgt.model.entity.Application)6 Subscription (org.wso2.carbon.apimgt.keymgt.model.entity.Subscription)5 SubscriptionDataLoaderImpl (org.wso2.carbon.apimgt.keymgt.model.impl.SubscriptionDataLoaderImpl)5 MessageContext (org.apache.synapse.MessageContext)4 DataLoadingException (org.wso2.carbon.apimgt.keymgt.model.exception.DataLoadingException)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.gateway.dto.ErrorDTO)4 ApiPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 URLMapping (org.wso2.carbon.apimgt.api.model.subscription.URLMapping)2 API (org.wso2.carbon.apimgt.common.analytics.publishers.dto.API)2 DeployAPIInGatewayEvent (org.wso2.carbon.apimgt.impl.notifier.events.DeployAPIInGatewayEvent)2 ApplicationPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.ApplicationPolicy)2 Scope (org.wso2.carbon.apimgt.keymgt.model.entity.Scope)2 SubscriptionPolicy (org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy)2