Search in sources :

Example 1 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluator method getApplicableConditions.

/**
 * When called, provides a list of Applicable Condition Groups for the current request.
 *
 * @param synapseContext        Message Context of the incoming request.
 * @param authenticationContext AuthenticationContext populated by {@code APIAuthenticationHandler}
 * @param inputConditionGroups  All Condition Groups Attached with the resource/API being invoked.
 * @return List of ConditionGroups applicable for the current request.
 */
public List<ConditionGroupDTO> getApplicableConditions(org.apache.synapse.MessageContext synapseContext, AuthenticationContext authenticationContext, ConditionGroupDTO[] inputConditionGroups) {
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>(inputConditionGroups.length);
    ConditionGroupDTO defaultGroup = null;
    for (ConditionGroupDTO conditionGroup : inputConditionGroups) {
        if (APIConstants.THROTTLE_POLICY_DEFAULT.equals(conditionGroup.getConditionGroupId())) {
            defaultGroup = conditionGroup;
        } else if (isConditionGroupApplicable(synapseContext, authenticationContext, conditionGroup)) {
            matchingConditions.add(conditionGroup);
        }
    }
    // If no matching ConditionGroups are present, apply the default group.
    if (matchingConditions.isEmpty()) {
        matchingConditions.add(defaultGroup);
    }
    return matchingConditions;
}
Also used : ArrayList(java.util.ArrayList) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)

Example 2 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgContinueWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsDisabled.

@Test
public void testMsgContinueWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsDisabled() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
    authenticationContext.setApiTier(throttlingTier);
    // Set stopOnQuota
    authenticationContext.setStopOnQuotaReach(false);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    verbInfo.setConditionGroups(conditionGroupDTOs);
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
    matchingConditions.add(conditionGroupDTO);
    String subscriptionLevelThrottleKey = authenticationContext.getApplicationId() + ":" + apiContext + ":" + apiVersion;
    // Set subscription level throttled out
    throttleDataHolder.addThrottleData(subscriptionLevelThrottleKey, System.currentTimeMillis() + 10000);
    // Though subscription level is throttled out, should continue the message flow, if stop on quota reach is
    // disabled
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgThrottleOutWhenHittingSubscriptionLevelSpike.

@Test
public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, accessInformation);
    throttleHandler.setSandboxMaxCount("100");
    SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
    throttleHandler.init(synapseEnvironment);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
    authenticationContext.setApiTier(throttlingTier);
    authenticationContext.setKeyType("SANDBOX");
    authenticationContext.setSpikeArrestLimit(100);
    authenticationContext.setStopOnQuotaReach(true);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    verbInfo.setConditionGroups(conditionGroupDTOs);
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
    matchingConditions.add(conditionGroupDTO);
    throttleDataHolder.addKeyTemplate("$user", "$user");
    Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
    Assert.assertFalse(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method init.

@Before
public void init() {
    timer = Mockito.mock(Timer.class);
    timer = Mockito.mock(Timer.class);
    context = Mockito.mock(Timer.Context.class);
    throttleEvaluator = Mockito.mock(ThrottleConditionEvaluator.class);
    accessInformation = Mockito.mock(AccessInformation.class);
    Mockito.when(timer.start()).thenReturn(context);
    verbInfoDTO = new ArrayList<>();
    verbInfo = new VerbInfoDTO();
    verbInfo.setHttpVerb(httpVerb);
    verbInfo.setRequestKey(apiContext + "/" + apiVersion + resourceUri + ":" + httpVerb);
    verbInfo.setThrottling(throttlingTier);
    verbInfoDTO.add(verbInfo);
    conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditionGroupId("_default");
    conditionGroupDTOs = new ConditionGroupDTO[1];
    conditionGroupDTOs[0] = conditionGroupDTO;
    apiLevelThrottleKey = apiContext + ":" + apiVersion;
    resourceLevelThrottleKey = apiContext + "/" + apiVersion + resourceUri + ":" + httpVerb;
    org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
    PowerMockito.mockStatic(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.class);
    Mockito.when(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getExtensionListenerMap()).thenReturn(extensionListenerMap);
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) AccessInformation(org.apache.synapse.commons.throttle.core.AccessInformation) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) Timer(org.wso2.carbon.metrics.manager.Timer) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) Before(org.junit.Before)

Example 5 with ConditionGroupDTO

use of org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO in project carbon-apimgt by wso2.

the class ThrottleHandlerTest method testMsgThrottleOutWhenCustomThrottlingLimitExceeded.

@Test
public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() {
    ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
    ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, accessInformation);
    throttleHandler.setProductionMaxCount("100");
    SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
    throttleHandler.init(synapseEnvironment);
    MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
    messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
    ((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
    AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
    authenticationContext.setApiTier(throttlingTier);
    authenticationContext.setSpikeArrestLimit(0);
    messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
    verbInfo.setConditionGroups(conditionGroupDTOs);
    ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
    Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
    matchingConditions.add(conditionGroupDTO);
    throttleDataHolder.addKeyTemplate("$user", "$user");
    throttleDataHolder.addKeyTemplate("testKeyTemplate", "testKeyTemplateValue");
    throttleDataHolder.addThrottleData("testKeyTemplate", System.currentTimeMillis() + 10000);
    Assert.assertFalse(throttleHandler.handleRequest(messageContext));
    throttleDataHolder.removeKeyTemplate("testKeyTemplate");
    Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
Also used : ThrottleDataHolder(org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder) AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) ArrayList(java.util.ArrayList) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ConditionGroupDTO (org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO)37 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)31 Test (org.junit.Test)29 MessageContext (org.apache.synapse.MessageContext)21 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)21 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)19 ArrayList (java.util.ArrayList)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 ThrottleDataHolder (org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder)12 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)6 HashMap (java.util.HashMap)3 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Before (org.junit.Before)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)2 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)2 Timer (org.wso2.carbon.metrics.manager.Timer)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1