Search in sources :

Example 51 with ThrottleProperties

use of org.wso2.carbon.apimgt.impl.dto.ThrottleProperties in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetPoliciesExcludingUnAuthenticatedTier.

@Test
public void testGetPoliciesExcludingUnAuthenticatedTier() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException, RegistryException, XMLStreamException {
    SubscriptionPolicy[] policies3 = { new SubscriptionPolicy("policy4"), new SubscriptionPolicy("policy5"), new SubscriptionPolicy("policy6"), new SubscriptionPolicy(APIConstants.UNAUTHENTICATED_TIER), new SubscriptionPolicy(APIConstants.UNLIMITED_TIER) };
    PowerMockito.mockStatic(APIUtil.class);
    BDDMockito.when(APIUtil.getTenantId(Mockito.anyString())).thenReturn(-1234);
    PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
        Object[] args = invocation.getArguments();
        return (String) args[0];
    });
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiMgtDAO);
    Mockito.when(apiMgtDAO.getSubscriptionPolicies(Mockito.anyInt())).thenReturn(policies3);
    ServiceReferenceHolder sh = mockRegistryAndUserRealm(-1234);
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
    PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    PowerMockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
    Mockito.when(throttleProperties.isEnableUnlimitedTier()).thenReturn(false);
    Assert.assertEquals(3, abstractAPIManager.getPolicies(API_PROVIDER, PolicyConstants.POLICY_LEVEL_SUB).length);
}
Also used : RegistryAuthorizationManager(org.wso2.carbon.registry.core.jdbc.realm.RegistryAuthorizationManager) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PaginationContext(org.wso2.carbon.registry.core.pagination.PaginationContext) RealmService(org.wso2.carbon.user.core.service.RealmService) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) Map(java.util.Map) XMLStreamException(javax.xml.stream.XMLStreamException) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) DocumentationType(org.wso2.carbon.apimgt.api.model.DocumentationType) ContentType(org.apache.http.entity.ContentType) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) Matchers.any(org.mockito.Matchers.any) IOUtils(org.apache.commons.io.IOUtils) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) MultitenantUtils(org.wso2.carbon.utils.multitenancy.MultitenantUtils) QName(javax.xml.namespace.QName) TestUtils.mockRegistryAndUserRealm(org.wso2.carbon.apimgt.impl.TestUtils.mockRegistryAndUserRealm) CarbonConstants(org.wso2.carbon.CarbonConstants) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) BlockConditionNotFoundException(org.wso2.carbon.apimgt.api.BlockConditionNotFoundException) Mock(org.mockito.Mock) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) CARBON_HOME(org.wso2.carbon.utils.ServerConstants.CARBON_HOME) RunWith(org.junit.runner.RunWith) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) Association(org.wso2.carbon.registry.core.Association) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) RegistryConstants(org.wso2.carbon.registry.core.RegistryConstants) ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) PowerMockito(org.powermock.api.mockito.PowerMockito) PublisherAPISearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherAPISearchResult) Before(org.junit.Before) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl) Wsdl(org.wso2.carbon.apimgt.api.model.Wsdl) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Test(org.junit.Test) PolicyNotFoundException(org.wso2.carbon.apimgt.api.PolicyNotFoundException) IOException(java.io.IOException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) ScopesDAO(org.wso2.carbon.apimgt.impl.dao.ScopesDAO) Field(java.lang.reflect.Field) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) BDDMockito(org.mockito.BDDMockito) TreeMap(java.util.TreeMap) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) KeyManagerHolder(org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Assert(org.junit.Assert) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) RegistryContext(org.wso2.carbon.registry.core.config.RegistryContext) SortedSet(java.util.SortedSet) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) RegistryUtils(org.wso2.carbon.registry.core.utils.RegistryUtils) PolicyConstants(org.wso2.carbon.apimgt.api.model.policy.PolicyConstants) APIPersistence(org.wso2.carbon.apimgt.persistence.APIPersistence) PublisherAPIInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIInfo) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) GovernanceUtils(org.wso2.carbon.governance.api.util.GovernanceUtils) Application(org.wso2.carbon.apimgt.api.model.Application) AXIOMUtil(org.apache.axiom.om.util.AXIOMUtil) APIUtil(org.wso2.carbon.apimgt.impl.utils.APIUtil) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) UUID(java.util.UUID) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) List(java.util.List) Modifier(java.lang.reflect.Modifier) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) SortedMap(java.util.SortedMap) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) HashMap(java.util.HashMap) HashSet(java.util.HashSet) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) API(org.wso2.carbon.apimgt.api.model.API) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) DEFAULT_DIALECT_URI(org.wso2.carbon.apimgt.impl.token.ClaimsRetriever.DEFAULT_DIALECT_URI) Tier(org.wso2.carbon.apimgt.api.model.Tier) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) Mockito(org.mockito.Mockito) UserStoreException(org.wso2.carbon.user.core.UserStoreException) Registry(org.wso2.carbon.registry.core.Registry) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) OASParserUtil(org.wso2.carbon.apimgt.impl.definitions.OASParserUtil) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) InputStream(java.io.InputStream) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 52 with ThrottleProperties

use of org.wso2.carbon.apimgt.impl.dto.ThrottleProperties in project carbon-apimgt by wso2.

the class ThrottlePolicyDeployerComponent method activate.

@Activate
protected void activate(ComponentContext context) {
    log.debug("Activating component...");
    APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIMConfiguration();
    if (configuration == null) {
        log.warn("API Manager Configuration not properly set.");
        return;
    }
    ThrottleProperties throttleProperties = configuration.getThrottleProperties();
    if (throttleProperties.isEnablePolicyDeployment()) {
        ThrottlePolicyStartupListener throttlePolicyStartupListener = new ThrottlePolicyStartupListener();
        registration = context.getBundleContext().registerService(ServerStartupObserver.class, throttlePolicyStartupListener, null);
        registration = context.getBundleContext().registerService(ServerShutdownHandler.class, throttlePolicyStartupListener, null);
        registration = context.getBundleContext().registerService(JMSListenerShutDownService.class, throttlePolicyStartupListener, null);
    }
}
Also used : ServerStartupObserver(org.wso2.carbon.core.ServerStartupObserver) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) JMSListenerShutDownService(org.wso2.carbon.apimgt.impl.jms.listener.JMSListenerShutDownService) ThrottlePolicyStartupListener(org.wso2.carbon.apimgt.throttle.policy.deployer.utils.ThrottlePolicyStartupListener) ServerShutdownHandler(org.wso2.carbon.core.ServerShutdownHandler) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) Activate(org.osgi.service.component.annotations.Activate)

Example 53 with ThrottleProperties

use of org.wso2.carbon.apimgt.impl.dto.ThrottleProperties in project carbon-apimgt by wso2.

the class AbstractAPIManager method getPolicies.

public Policy[] getPolicies(String username, String level) throws APIManagementException {
    Policy[] policies = null;
    int tenantID = APIUtil.getTenantId(username);
    if (PolicyConstants.POLICY_LEVEL_API.equals(level)) {
        policies = apiMgtDAO.getAPIPolicies(tenantID);
    } else if (PolicyConstants.POLICY_LEVEL_APP.equals(level)) {
        policies = apiMgtDAO.getApplicationPolicies(tenantID);
    } else if (PolicyConstants.POLICY_LEVEL_SUB.equals(level)) {
        policies = apiMgtDAO.getSubscriptionPolicies(tenantID);
    } else if (PolicyConstants.POLICY_LEVEL_GLOBAL.equals(level)) {
        policies = apiMgtDAO.getGlobalPolicies(tenantID);
    }
    // Get the API Manager configurations and check whether the unlimited tier is disabled. If disabled, remove
    // the tier from the array.
    APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    ThrottleProperties throttleProperties = apiManagerConfiguration.getThrottleProperties();
    List<Policy> policiesWithoutUnlimitedTier = new ArrayList<Policy>();
    if (policies != null) {
        for (Policy policy : policies) {
            if (APIConstants.UNLIMITED_TIER.equals(policy.getPolicyName())) {
                if (throttleProperties.isEnableUnlimitedTier()) {
                    policiesWithoutUnlimitedTier.add(policy);
                }
            } else if (!APIConstants.UNAUTHENTICATED_TIER.equals(policy.getPolicyName())) {
                policiesWithoutUnlimitedTier.add(policy);
            }
        }
    }
    policies = policiesWithoutUnlimitedTier.toArray(new Policy[0]);
    return policies;
}
Also used : Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) ArrayList(java.util.ArrayList) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)

Example 54 with ThrottleProperties

use of org.wso2.carbon.apimgt.impl.dto.ThrottleProperties in project wso2-synapse by wso2.

the class ThrottleUtil method loadThrottlePropertiesFromConfigurations.

/**
 * This method used to set throttle properties loaded from throttle.properties file in configuration folder
 *
 * @return properties of throttle properties
 */
public static ThrottleProperties loadThrottlePropertiesFromConfigurations() {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    Properties properties = new Properties();
    String throttlePropertiesFileLocation;
    if (System.getProperty(CONF_LOCATION) != null) {
        throttlePropertiesFileLocation = System.getProperty(CONF_LOCATION) + File.separator + "throttle.properties";
        try {
            properties.load(new FileInputStream(throttlePropertiesFileLocation));
            SecretResolver secretResolver = SecretResolverFactory.create(properties);
            Set<String> keys = properties.stringPropertyNames();
            for (String key : keys) {
                if (ThrottleConstants.THROTTLE_CONTEXT_CLEANUP_TASK_FREQUENCY.equals(key)) {
                    String throttleFrequency = properties.getProperty(key);
                    if (!StringUtils.isEmpty(throttleFrequency)) {
                        throttleProperties.setThrottleFrequency(throttleFrequency);
                    }
                }
                if (ThrottleConstants.THROTTLE_CONTEXT_DISTRIBUTED_CLEANUP_TASK_FREQUENCY.equals(key)) {
                    String throttleContextDistributedCleanupTaskFrequency = properties.getProperty(key);
                    if (throttleContextDistributedCleanupTaskFrequency != null && !throttleContextDistributedCleanupTaskFrequency.equals("")) {
                        throttleProperties.setThrottleContextDistributedCleanupTaskFrequency(throttleContextDistributedCleanupTaskFrequency);
                    }
                }
                if (ThrottleConstants.THROTTLE_CONTEXT_DISTRIBUTED_EXPIRED_INSTANCE_TIME.equals(key)) {
                    String throttleContextDistributedExpiredInstanceTime = properties.getProperty(key);
                    if (throttleContextDistributedExpiredInstanceTime != null && !throttleContextDistributedExpiredInstanceTime.equals("")) {
                        throttleProperties.setThrottleContextDistributedExpiredInstanceTime(throttleContextDistributedExpiredInstanceTime);
                    }
                }
                if (ThrottleConstants.THROTTLE_DISTRIBUTED_CLEANUP_POOL_SIZE.equals(key)) {
                    String throttleDistributedCleanupPoolSize = properties.getProperty(key);
                    if (throttleDistributedCleanupPoolSize != null && throttleDistributedCleanupPoolSize != "") {
                        throttleProperties.setThrottleDistributedCleanupPoolSize(throttleDistributedCleanupPoolSize);
                    }
                }
                if (ThrottleConstants.THROTTLE_DISTRIBUTED_CLEANUP_AMOUNT.equals(key)) {
                    String throttleDistributedCleanupAmount = properties.getProperty(key);
                    if (throttleDistributedCleanupAmount != null && throttleDistributedCleanupAmount != "") {
                        throttleProperties.setThrottleDistributedCleanupAmount(throttleDistributedCleanupAmount);
                    }
                }
                if (ThrottleConstants.THROTTLE_DISTRIBUTED_CLEANUP_TASK_ENABLE.equals(key)) {
                    String throttleDistributedCleanupTaskEnable = properties.getProperty(key);
                    if (throttleDistributedCleanupTaskEnable != null && throttleDistributedCleanupTaskEnable != "") {
                        throttleProperties.setThrottleDistributedCleanupTaskEnable(throttleDistributedCleanupTaskEnable);
                    }
                }
                if (ThrottleConstants.MAX_NON_ASSOCIATED_COUNTER_CLEANUP_AMOUNT.equals(key)) {
                    String maxNonAssociatedCounterCleanupAmount = properties.getProperty(key);
                    if (maxNonAssociatedCounterCleanupAmount != null && maxNonAssociatedCounterCleanupAmount != "") {
                        throttleProperties.setMaxNonAssociatedCounterCleanupAmount(maxNonAssociatedCounterCleanupAmount);
                    }
                }
                if (ThrottleConstants.THROTTLING_POOL_SIZE.equals(key)) {
                    String throttlingPoolSize = properties.getProperty(key);
                    if (throttlingPoolSize != null && throttlingPoolSize != "") {
                        throttleProperties.setThrottlingPoolSize(throttlingPoolSize);
                    }
                }
                if (ThrottleConstants.THROTTLING_REPLICATION_FREQUENCY.equals(key)) {
                    String throttlingReplicationFrequency = properties.getProperty(key);
                    if (throttlingReplicationFrequency != null && throttlingReplicationFrequency != "") {
                        throttleProperties.setThrottlingReplicationFrequency(throttlingReplicationFrequency);
                    }
                }
                if (ThrottleConstants.THROTTLING_KEYS_TO_REPLICATE.equals(key)) {
                    String throttlingKeysToReplicate = properties.getProperty(key);
                    if (throttlingKeysToReplicate != null && throttlingKeysToReplicate != "") {
                        throttleProperties.setThrottlingKeysToReplicates(throttlingKeysToReplicate);
                    }
                }
                if (ThrottleConstants.WINDOW_REPLICATOR_POOL_SIZE.equals(key)) {
                    String windowReplicatorPoolSize = properties.getProperty(key);
                    if (windowReplicatorPoolSize != null && windowReplicatorPoolSize != "") {
                        throttleProperties.setWindowReplicatorPoolSize(windowReplicatorPoolSize);
                    }
                }
                if (ThrottleConstants.WINDOW_REPLICATOR_FREQUENCY.equals(key)) {
                    String windowReplicatorFrequency = properties.getProperty(key);
                    if (windowReplicatorFrequency != null && windowReplicatorFrequency != "") {
                        throttleProperties.setWindowReplicatorFrequency(windowReplicatorFrequency);
                    }
                }
                if (ThrottleConstants.DISTRIBUTED_COUNTER_TYPE.equals(key)) {
                    String distributedCounterType = properties.getProperty(key);
                    if (distributedCounterType != null && !distributedCounterType.equals("")) {
                        throttleProperties.setDistributedCounterType(distributedCounterType);
                    }
                }
                if (key.contains(ThrottleConstants.DISTRIBUTED_COUNTER_CONFIGURATIONS)) {
                    String distributedConfiguration = properties.getProperty(key);
                    String configuration = key.split(ThrottleConstants.DISTRIBUTED_COUNTER_CONFIGURATIONS)[1];
                    if (StringUtils.isNotEmpty(distributedConfiguration)) {
                        throttleProperties.getDistributedCounterConfigurations().put(configuration, getResolvedValue(secretResolver, distributedConfiguration));
                    }
                }
            }
        } catch (IOException e) {
            log.debug("Setting the Default Throttle Properties");
        }
    } else {
        log.debug("Setting the Default Throttle Properties");
    }
    return throttleProperties;
}
Also used : SecretResolver(org.wso2.securevault.SecretResolver) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Aggregations

ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)53 Test (org.junit.Test)45 MessageContext (org.apache.synapse.MessageContext)28 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)28 HashMap (java.util.HashMap)22 List (java.util.List)21 TreeMap (java.util.TreeMap)19 ArrayList (java.util.ArrayList)18 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)17 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)16 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)14 RealmService (org.wso2.carbon.user.core.service.RealmService)13 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)12 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)12 Map (java.util.Map)11 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)11 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)11 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)10