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