Search in sources :

Example 1 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class AbstractAPIManager method searchPaginatedAPIProducts.

@Override
public Map<String, Object> searchPaginatedAPIProducts(String searchQuery, String requestedTenantDomain, int start, int end) throws APIManagementException {
    Map<String, Object> result = new HashMap<String, Object>();
    boolean isTenantFlowStarted = false;
    if (log.isDebugEnabled()) {
        log.debug("Original search query received : " + searchQuery);
    }
    try {
        boolean isTenantMode = (requestedTenantDomain != null);
        if (isTenantMode && !org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(requestedTenantDomain)) {
            isTenantFlowStarted = true;
            startTenantFlow(requestedTenantDomain);
        } else {
            requestedTenantDomain = org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
            isTenantFlowStarted = true;
            startTenantFlow(requestedTenantDomain);
        }
        Registry userRegistry;
        int tenantIDLocal = 0;
        String userNameLocal = this.username;
        if ((isTenantMode && this.tenantDomain == null) || (isTenantMode && isTenantDomainNotMatching(requestedTenantDomain))) {
            // Tenant store anonymous mode
            tenantIDLocal = getTenantManager().getTenantId(requestedTenantDomain);
            userRegistry = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantIDLocal);
            userNameLocal = CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME;
        } else {
            userRegistry = this.registry;
            tenantIDLocal = tenantId;
        }
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userNameLocal);
        result = searchPaginatedAPIProducts(userRegistry, getSearchQuery(searchQuery), start, end);
    } catch (Exception e) {
        String msg = "Failed to Search APIs";
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
    return result;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) JSONObject(org.json.simple.JSONObject) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) JSONException(org.json.JSONException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) BlockConditionNotFoundException(org.wso2.carbon.apimgt.api.BlockConditionNotFoundException) PolicyNotFoundException(org.wso2.carbon.apimgt.api.PolicyNotFoundException) IOException(java.io.IOException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApplicationNameWhiteSpaceValidationException(org.wso2.carbon.apimgt.api.ApplicationNameWhiteSpaceValidationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) ApplicationNameWithInvalidCharactersException(org.wso2.carbon.apimgt.api.ApplicationNameWithInvalidCharactersException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 2 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIMConfigServiceImpl method updateWorkflowConfig.

@Override
public void updateWorkflowConfig(String organization, String workflowConfig) throws APIManagementException {
    if (organization == null) {
        organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
        int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
        if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
            APIUtil.loadTenantRegistry(tenantId);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        if (registry.resourceExists(APIConstants.WORKFLOW_EXECUTOR_LOCATION)) {
            Resource resource = registry.get(APIConstants.WORKFLOW_EXECUTOR_LOCATION);
            byte[] data = IOUtils.toByteArray(new StringReader(workflowConfig));
            resource.setContent(data);
            resource.setMediaType(APIConstants.WORKFLOW_MEDIA_TYPE);
            registry.put(APIConstants.WORKFLOW_EXECUTOR_LOCATION, resource);
        }
    } catch (RegistryException | IOException e) {
        String msg = "Error while retrieving External Stores Configuration from registry";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) StringReader(java.io.StringReader) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 3 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIMConfigServiceImpl method addSelfSighupConfig.

@Override
public void addSelfSighupConfig(String organization, String selfSignUpConfig) throws APIManagementException {
    if (organization == null) {
        organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
        int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
        if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
            APIUtil.loadTenantRegistry(tenantId);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        if (!registry.resourceExists(APIConstants.SELF_SIGN_UP_CONFIG_LOCATION)) {
            byte[] data = IOUtils.toByteArray(new StringReader(selfSignUpConfig));
            Resource resource = registry.newResource();
            resource.setContent(data);
            resource.setMediaType(APIConstants.SELF_SIGN_UP_CONFIG_MEDIA_TYPE);
            registry.put(APIConstants.SELF_SIGN_UP_CONFIG_LOCATION, resource);
        }
    } catch (RegistryException | IOException e) {
        String msg = "Error while adding Self-SignUp Configuration from registry";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) StringReader(java.io.StringReader) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 4 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIMConfigServiceImpl method getExternalStoreConfig.

@Override
public String getExternalStoreConfig(String organization) throws APIManagementException {
    if (organization == null) {
        organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
        int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
        if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
            APIUtil.loadTenantRegistry(tenantId);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        if (registry.resourceExists(APIConstants.EXTERNAL_API_STORES_LOCATION)) {
            Resource resource = registry.get(APIConstants.EXTERNAL_API_STORES_LOCATION);
            return new String((byte[]) resource.getContent(), Charset.defaultCharset());
        } else {
            return null;
        }
    } catch (RegistryException e) {
        String msg = "Error while retrieving External Stores Configuration from registry";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 5 with UserRegistry

use of org.wso2.carbon.registry.core.session.UserRegistry in project carbon-apimgt by wso2.

the class APIConsumerImplTest method testGetDeniedTiers.

@Test
public void testGetDeniedTiers() throws APIManagementException, org.wso2.carbon.user.core.UserStoreException {
    UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(Mockito.anyString())).thenReturn("true", "false");
    APIConsumerImpl apiConsumer = new UserAwareAPIConsumerWrapper(userRegistry, apiMgtDAO);
    Mockito.when(userRegistry.getUserRealm()).thenReturn(userRealm);
    Mockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    String[] currentUserRoles = { "role1", "role2" };
    PowerMockito.when(APIUtil.getListOfRoles(Mockito.anyString())).thenReturn(currentUserRoles);
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
    TierPermissionDTO tierPermissionDTO = new TierPermissionDTO();
    TierPermissionDTO tierPermissionDTO1 = new TierPermissionDTO();
    tierPermissionDTO.setRoles(new String[] { "role1" });
    Set<TierPermissionDTO> tierPermissionDTOs = new HashSet<TierPermissionDTO>();
    tierPermissionDTOs.add(tierPermissionDTO);
    Mockito.when(apiMgtDAO.getThrottleTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
    tierPermissionDTO.setRoles(new String[] { "role3" });
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
    Mockito.when(apiMgtDAO.getTierPermissions(Mockito.anyInt())).thenReturn(tierPermissionDTOs);
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 0);
    tierPermissionDTO.setPermissionType(APIConstants.TIER_PERMISSION_ALLOW);
    Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[0]);
    tierPermissionDTOs.add(tierPermissionDTO1);
    tierPermissionDTO1.setRoles(new String[] { "role4" });
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 1);
    Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenReturn(new String[] { "role1", "role2" });
    tierPermissionDTO1.setRoles(new String[] { "role2" });
    tierPermissionDTO1.setTierName("Silver");
    Assert.assertEquals(apiConsumer.getDeniedTiers().size(), 2);
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)147 Resource (org.wso2.carbon.registry.core.Resource)96 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)86 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)84 Test (org.junit.Test)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)66 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)65 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)53 API (org.wso2.carbon.apimgt.api.model.API)47 UserStoreException (org.wso2.carbon.user.api.UserStoreException)44 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)43 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)41 Registry (org.wso2.carbon.registry.core.Registry)39 IOException (java.io.IOException)38 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)37 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)34 ArrayList (java.util.ArrayList)33 QName (javax.xml.namespace.QName)32 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)32