use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ApplicationThrottleControllerTest method testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceRetrievalFailed.
@Test(expected = SynapseException.class)
public void testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceRetrievalFailed() throws UserStoreException, RegistryException {
Mockito.when(throttleDataHolder.getThrottleContext(applicationId)).thenReturn(null);
PowerMockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
PowerMockito.when(registryService.getGovernanceSystemRegistry(tenantID)).thenReturn(registry);
PowerMockito.when(registry.resourceExists(RESOURCE_PATH)).thenReturn(true);
PowerMockito.doThrow(new RegistryException("Error while fetching resource ")).when(registry).get(RESOURCE_PATH);
ApplicationThrottleController.getApplicationThrottleContext(messageContext, throttleDataHolder, applicationId, THROTTLE_POLICY_KEY);
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ApplicationThrottleControllerTest method testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceContentRetrievalFailed.
@Test(expected = SynapseException.class)
public void testCreatingThrottleContextThrowsSynapseExceptionWhenThrottlingPolicyResourceContentRetrievalFailed() throws UserStoreException, RegistryException {
Mockito.when(throttleDataHolder.getThrottleContext(applicationId)).thenReturn(null);
PowerMockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantID);
PowerMockito.when(registryService.getGovernanceSystemRegistry(tenantID)).thenReturn(registry);
PowerMockito.when(registry.resourceExists(RESOURCE_PATH)).thenReturn(true);
PowerMockito.when(registry.get(RESOURCE_PATH)).thenReturn(throttlingPolicyResource);
PowerMockito.doThrow(new RegistryException("Error while retrieving resource content")).when(throttlingPolicyResource).getContent();
ApplicationThrottleController.getApplicationThrottleContext(messageContext, throttleDataHolder, applicationId, THROTTLE_POLICY_KEY);
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class SseUtils method isRequestBlocked.
public static boolean isRequestBlocked(AuthenticationContext authContext, String apiContext, String apiVersion, String authorizedUser, String clientIp, String apiTenantDomain) {
ThrottleDataHolder throttleDataHolder = ServiceReferenceHolder.getInstance().getThrottleDataHolder();
if (throttleDataHolder.isBlockingConditionsPresent()) {
String appLevelBlockingKey = authContext.getSubscriber() + ":" + authContext.getApplicationName();
String subscriptionLevelBlockingKey = apiContext + ":" + apiVersion + ":" + authContext.getSubscriber() + "-" + authContext.getApplicationName() + ":" + authContext.getKeyType();
return throttleDataHolder.isRequestBlocked(apiContext, appLevelBlockingKey, authorizedUser, clientIp, apiTenantDomain, subscriptionLevelBlockingKey);
}
return false;
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ApplicationThrottleController method createThrottleContext.
private static ThrottleContext createThrottleContext(MessageContext synCtx, ThrottleDataHolder dataHolder, String applicationId, String policyKeyApplication) {
// Object entryValue = synCtx.getEntry(APPLICATION_THROTTLE_POLICY_KEY);
PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
RealmService realmService = (RealmService) carbonContext.getOSGiService(RealmService.class, null);
AuthenticationContext authContext = APISecurityUtils.getAuthenticationContext(synCtx);
// extract the subscriber username from the auth Context
String subscriber = authContext.getSubscriber();
// get the tenant Domain from the subscriber
String tenantDomain = MultitenantUtils.getTenantDomain(subscriber);
int tenantId;
// get the tenant domain id from the tenant domain name
try {
tenantId = realmService.getTenantManager().getTenantId(tenantDomain);
} catch (UserStoreException e) {
handleException("Unable to Find the tenant ID using tenant: " + tenantDomain, e);
return null;
}
Object entryValue = lookup(policyKeyApplication, tenantId);
if (entryValue == null || !(entryValue instanceof OMElement)) {
handleException("Unable to load throttling policy using key: " + policyKeyApplication);
}
try {
Throttle throttle = ThrottleFactory.createMediatorThrottle(PolicyEngine.getPolicy((OMElement) entryValue));
ThrottleContext context = throttle.getThrottleContext(ThrottleConstants.ROLE_BASED_THROTTLE_KEY);
dataHolder.addThrottleContext(applicationId, context);
return context;
} catch (ThrottleException e) {
handleException("Error processing the throttling policy", e);
}
return null;
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class KeyTemplateRetrieverTest method run.
@Test
public void run() throws Exception {
Map map = new HashMap();
map.put("$userId", "$userId");
map.put("$apiContext", "$apiContext");
map.put("$apiVersion", "$apiVersion");
String content = "[\"$userId\",\"$apiContext\",\"$apiVersion\"]";
PowerMockito.mockStatic(APIUtil.class);
HttpClient httpClient = Mockito.mock(HttpClient.class);
HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(content.getBytes()));
Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
Mockito.when(httpClient.execute(Mockito.any(HttpGet.class))).thenReturn(httpResponse);
StatusLine status = Mockito.mock(StatusLine.class);
Mockito.when(status.getStatusCode()).thenReturn(200);
Mockito.when(httpResponse.getStatusLine()).thenReturn(status);
BDDMockito.given(APIUtil.getHttpClient(Mockito.anyInt(), Mockito.anyString())).willReturn(httpClient);
EventHubConfigurationDto eventHubConfigurationDto = new EventHubConfigurationDto();
eventHubConfigurationDto.setUsername("admin");
eventHubConfigurationDto.setPassword("admin".toCharArray());
eventHubConfigurationDto.setEnabled(true);
eventHubConfigurationDto.setServiceUrl("http://localhost:18084/internal/data/v1");
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
KeyTemplateRetriever keyTemplateRetriever = new KeyTemplateRetrieverWrapper(eventHubConfigurationDto, throttleDataHolder);
keyTemplateRetriever.run();
Map<String, String> keyTemplateMap = throttleDataHolder.getKeyTemplateMap();
Assert.assertNotNull(keyTemplateMap);
Assert.assertEquals(map, keyTemplateMap);
}
Aggregations