Search in sources :

Example 76 with APIManagerConfiguration

use of org.wso2.carbon.apimgt.impl.APIManagerConfiguration 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 77 with APIManagerConfiguration

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

the class APISecurityUtilsTestCase method testSetAuthenticationContext.

public void testSetAuthenticationContext() {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    APIManagerConfiguration apiMgtConfig = Mockito.mock(APIManagerConfiguration.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfiguration()).thenReturn(apiMgtConfig);
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    AuthenticationContext authenticationContext = Mockito.mock(AuthenticationContext.class);
    Mockito.when(authenticationContext.getKeyType()).thenReturn("keyType");
    APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
    // test when caller token is not null
    Mockito.when(authenticationContext.getCallerToken()).thenReturn("callertoken");
    Mockito.when(messageContext.getProperty(APIConstants.API_KEY_TYPE)).thenReturn("keyType");
    // Axis2MessageContext axis2MessageContext = Mockito.mock(Axis2MessageContext.class);
    org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
    Map transportHeaders = new HashMap();
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
    APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
    Assert.assertEquals(APISecurityUtils.getAuthenticationContext(messageContext).getCallerToken(), "callertoken");
    Assert.assertEquals("keyType", messageContext.getProperty(APIConstants.API_KEY_TYPE));
    // test for IllegalStateException
    String API_AUTH_CONTEXT = "__API_AUTH_CONTEXT";
    Mockito.when(authenticationContext.getCallerToken()).thenReturn("newCallerToken");
    Mockito.when(messageContext.getProperty(API_AUTH_CONTEXT)).thenReturn("abc");
    APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
    Assert.assertEquals(APISecurityUtils.getAuthenticationContext(messageContext).getCallerToken(), "newCallerToken");
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) HashMap(java.util.HashMap) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) HashMap(java.util.HashMap) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 78 with APIManagerConfiguration

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

the class OAuthAuthenticatorTest method initOAuthParams.

@Test
public void initOAuthParams() throws Exception {
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    OAuthAuthenticator oauthAuthenticator = new OauthAuthenticatorWrapper(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.REMOVE_OAUTH_HEADERS_FROM_MESSAGE)).thenReturn("true");
    Mockito.when(apiManagerConfiguration.getJwtConfigurationDto()).thenReturn(new ExtendedJWTConfigurationDto());
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) ExtendedJWTConfigurationDto(org.wso2.carbon.apimgt.impl.dto.ExtendedJWTConfigurationDto) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 79 with APIManagerConfiguration

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

the class CommonConfigDeployer method createdConfigurationContext.

public void createdConfigurationContext(ConfigurationContext configurationContext) {
    final String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
    try {
        // TODO adding only the policies to data wouldn't be sufficient. Need to figure out approach after tenant story has finalized
        // Add default set of policies to database
        ThrottleProperties.PolicyDeployer policyDeployer = configuration.getThrottleProperties().getPolicyDeployer();
        // Also this will avoid tenant login overhead as well
        if (policyDeployer.isEnabled()) {
            Thread t1 = new Thread(new Runnable() {

                public void run() {
                    try {
                        APIUtil.addDefaultTenantAdvancedThrottlePolicies(tenantDomain, tenantId);
                    } catch (APIManagementException e) {
                        log.error("Error while deploying throttle policies", e);
                    }
                }
            });
            t1.start();
        }
    } catch (Exception e) {
        log.error("Failed to load default policies to tenant" + tenantDomain, e);
    }
    try {
        // Check whether GatewayType is "Synapse" before attempting to load Custom-Sequences into registry
        String gatewayType = configuration.getFirstProperty(APIConstants.API_GATEWAY_TYPE);
        if (APIConstants.API_GATEWAY_TYPE_SYNAPSE.equalsIgnoreCase(gatewayType)) {
            APIUtil.writeDefinedSequencesToTenantRegistry(tenantId);
        }
    }// Need to continue the execution even if we encounter an error.
     catch (Exception e) {
        log.error("Failed to write defined sequences to tenant " + tenantDomain + "'s registry", e);
    }
    try {
        APIUtil.loadTenantExternalStoreConfig(tenantDomain);
    } catch (Exception e) {
        log.error("Failed to load external-stores.xml to tenant " + tenantDomain + "'s registry", e);
    }
    try {
        APIUtil.loadTenantGAConfig(tenantDomain);
    } catch (Exception e) {
        log.error("Failed to load ga-config.xml to tenant " + tenantDomain + "'s registry", e);
    }
    try {
        // load workflow-extension configuration to the registry
        APIUtil.loadTenantWorkFlowExtensions(tenantDomain);
    } catch (Exception e) {
        log.error("Failed to load workflow-extension.xml to tenant " + tenantDomain + "'s registry", e);
    }
    try {
        // load self signup configurations to the registry
        APIUtil.loadTenantSelfSignUpConfigurations(tenantDomain);
    } catch (Exception e) {
        log.error("Failed to load sign-up-config.xml to tenant " + tenantDomain + "'s registry", e);
    }
    try {
        APIUtil.loadAndSyncTenantConf(tenantDomain);
    } catch (APIManagementException e) {
        log.error("Failed to load " + APIConstants.API_TENANT_CONF + " for tenant " + tenantDomain, e);
    } catch (Exception e) {
        // The generic Exception is handled explicitly so execution does not stop during config deployment
        log.error("Exception when loading " + APIConstants.API_TENANT_CONF + " for tenant " + tenantDomain, e);
    }
    try {
        // Load common operation policies to tenant
        APIUtil.loadCommonOperationPolicies(tenantDomain);
    } catch (Exception e) {
        // The generic Exception is handled explicitly so execution does not stop during config deployment
        log.error("Exception when loading " + APIConstants.OPERATION_POLICIES + " for tenant " + tenantDomain, e);
    }
    try {
        APIUtil.createDefaultRoles(tenantId);
    } catch (APIManagementException e) {
        log.error("Failed create default roles for tenant " + tenantDomain, e);
    } catch (Exception e) {
        // The generic Exception is handled explicitly so execution does not stop during config deployment
        log.error("Exception when creating default roles for tenant " + tenantDomain, e);
    }
    try {
        CommonUtil.addDefaultLifecyclesIfNotAvailable(ServiceReferenceHolder.getInstance().getRegistryService().getConfigSystemRegistry(tenantId), CommonUtil.getRootSystemRegistry(tenantId));
    } catch (RegistryException e) {
        log.error("Error while accessing registry", e);
    } catch (FileNotFoundException e) {
        log.error("Error while find lifecycle.xml", e);
    } catch (XMLStreamException e) {
        log.error("Error while parsing Lifecycle.xml", e);
    }
    KeyManagerConfigurationDataRetriever keyManagerConfigurationDataRetriever = new KeyManagerConfigurationDataRetriever(tenantDomain);
    keyManagerConfigurationDataRetriever.startLoadKeyManagerConfigurations();
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) KeyManagerConfigurationDataRetriever(org.wso2.carbon.apimgt.impl.loader.KeyManagerConfigurationDataRetriever) FileNotFoundException(java.io.FileNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) FileNotFoundException(java.io.FileNotFoundException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)

Example 80 with APIManagerConfiguration

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

the class JMSListenerComponent 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;
    }
    JMSListenerStartupShutdownListener jmsListenerStartupShutdownListener = new JMSListenerStartupShutdownListener();
    registration = context.getBundleContext().registerService(ServerStartupObserver.class, jmsListenerStartupShutdownListener, null);
    registration = context.getBundleContext().registerService(ServerShutdownHandler.class, jmsListenerStartupShutdownListener, null);
    registration = context.getBundleContext().registerService(JMSListenerShutDownService.class, jmsListenerStartupShutdownListener, 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) ServerShutdownHandler(org.wso2.carbon.core.ServerShutdownHandler) JMSListenerStartupShutdownListener(org.wso2.carbon.apimgt.jms.listener.utils.JMSListenerStartupShutdownListener) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)122 Test (org.junit.Test)76 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)67 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)51 HashMap (java.util.HashMap)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)36 RealmService (org.wso2.carbon.user.core.service.RealmService)33 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)31 ArrayList (java.util.ArrayList)26 API (org.wso2.carbon.apimgt.api.model.API)25 Before (org.junit.Before)24 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)24 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)24 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)23 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)23 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)22 MessageContext (org.apache.synapse.MessageContext)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)20 Cache (javax.cache.Cache)19 Map (java.util.Map)18