Search in sources :

Example 56 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException 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);
}
Also used : RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 57 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.

the class APIUtil method searchAPIsByDoc.

/**
 * Search Apis by Doc Content
 *
 * @param registry     - Registry which is searched
 * @param tenantID     - Tenant id of logged in domain
 * @param username     - Logged in username
 * @param searchTerm   - Search value for doc
 * @param searchClient - Search client
 * @return - Documentation to APIs map
 * @throws APIManagementException - If failed to get ArtifactManager for given tenant
 */
public static Map<Documentation, API> searchAPIsByDoc(Registry registry, int tenantID, String username, String searchTerm, String searchClient) throws APIManagementException {
    Map<Documentation, API> apiDocMap = new HashMap<Documentation, API>();
    try {
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifactManager docArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
        if (docArtifactManager == null) {
            String errorMessage = "Doc artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        SolrClient client = SolrClient.getInstance();
        Map<String, String> fields = new HashMap<String, String>();
        fields.put(APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, "*" + APIConstants.API_ROOT_LOCATION + "*");
        fields.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, "*");
        if (tenantID == -1) {
            tenantID = MultitenantConstants.SUPER_TENANT_ID;
        }
        // PaginationContext.init(0, 10000, "ASC", APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, Integer.MAX_VALUE);
        SolrDocumentList documentList = client.query(searchTerm, tenantID, fields);
        org.wso2.carbon.user.api.AuthorizationManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
        username = MultitenantUtils.getTenantAwareUsername(username);
        for (SolrDocument document : documentList) {
            String filePath = (String) document.getFieldValue("path_s");
            String fileName = (String) document.getFieldValue("resourceName_s");
            int index = filePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
            filePath = filePath.substring(index);
            API api = null;
            Documentation doc = null;
            boolean isAuthorized;
            int indexOfContents = filePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
            String documentationPath = filePath.substring(0, indexOfContents) + fileName;
            String path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + documentationPath);
            if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
                isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
            } else {
                isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
            }
            if (isAuthorized) {
                Resource docResource = registry.get(documentationPath);
                String docArtifactId = docResource.getUUID();
                if (docArtifactId != null) {
                    GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId);
                    doc = APIUtil.getDocumentation(docArtifact);
                }
                int indexOfDocumentation = filePath.indexOf(APIConstants.DOCUMENTATION_KEY);
                String apiPath = documentationPath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
                path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + apiPath);
                if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
                    isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
                } else {
                    isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
                }
                if (isAuthorized) {
                    Resource resource = registry.get(apiPath);
                    String apiArtifactId = resource.getUUID();
                    if (apiArtifactId != null) {
                        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiArtifactId);
                        api = APIUtil.getAPI(apiArtifact, registry);
                    } else {
                        throw new GovernanceException("artifact id is null of " + apiPath);
                    }
                }
            }
            if (doc != null && api != null) {
                if (APIConstants.STORE_CLIENT.equals(searchClient)) {
                    if (APIConstants.PUBLISHED.equals(api.getStatus()) || APIConstants.PROTOTYPED.equals(api.getStatus())) {
                        apiDocMap.put(doc, api);
                    }
                } else {
                    apiDocMap.put(doc, api);
                }
            }
        }
    } catch (IndexerException e) {
        handleException("Failed to search APIs with type Doc", e);
    } catch (RegistryException e) {
        handleException("Failed to search APIs with type Doc", e);
    } catch (UserStoreException e) {
        handleException("Failed to search APIs with type Doc", e);
    }
    return apiDocMap;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint) SolrDocument(org.apache.solr.common.SolrDocument) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SolrClient(org.wso2.carbon.registry.indexing.solr.SolrClient) UserStoreException(org.wso2.carbon.user.api.UserStoreException) API(org.wso2.carbon.apimgt.api.model.API) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException)

Example 58 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.

the class APIUtil method getTenantDomainsByState.

/**
 * Get tenants by state
 *
 * @param state state of the tenant
 * @return set of tenants
 * @throws UserStoreException
 */
public static Set<String> getTenantDomainsByState(String state) throws UserStoreException {
    boolean isActive = state.equalsIgnoreCase(APIConstants.TENANT_STATE_ACTIVE);
    if (isActive) {
        return getActiveTenantDomains();
    }
    Tenant[] tenants = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getAllTenants();
    Set<String> tenantDomains = new HashSet<>();
    for (Tenant tenant : tenants) {
        if (!tenant.isActive()) {
            tenantDomains.add(tenant.getDomain());
        }
    }
    return tenantDomains;
}
Also used : Tenant(org.wso2.carbon.user.api.Tenant) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 59 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.

the class APIUtil method getActiveTenantDomains.

/**
 * Get active tenant domains
 *
 * @return
 * @throws UserStoreException
 */
public static Set<String> getActiveTenantDomains() throws UserStoreException {
    Set<String> tenantDomains;
    Tenant[] tenants = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getAllTenants();
    if (tenants == null || tenants.length == 0) {
        tenantDomains = Collections.<String>emptySet();
    } else {
        tenantDomains = new HashSet<String>();
        for (Tenant tenant : tenants) {
            if (tenant.isActive()) {
                tenantDomains.add(tenant.getDomain());
            }
        }
        if (!tenantDomains.isEmpty()) {
            tenantDomains.add(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        }
    }
    return tenantDomains;
}
Also used : Tenant(org.wso2.carbon.user.api.Tenant)

Example 60 with UserStoreException

use of org.wso2.carbon.user.api.UserStoreException in project carbon-apimgt by wso2.

the class APIUtil method clearResourcePermissions.

/**
 * This function is to set resource permissions based on its visibility
 *
 * @param artifactPath API/Product resource path
 * @throws APIManagementException Throwing exception
 */
public static void clearResourcePermissions(String artifactPath, Identifier id, int tenantId) throws APIManagementException {
    try {
        String resourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + artifactPath);
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(id.getProviderName()));
        if (!org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            org.wso2.carbon.user.api.AuthorizationManager authManager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getAuthorizationManager();
            authManager.clearResourceAuthorizations(resourcePath);
        } else {
            RegistryAuthorizationManager authorizationManager = new RegistryAuthorizationManager(ServiceReferenceHolder.getUserRealm());
            authorizationManager.clearResourceAuthorizations(resourcePath);
        }
    } catch (UserStoreException e) {
        handleException("Error while adding role permissions to API", e);
    }
}
Also used : RegistryAuthorizationManager(org.wso2.carbon.registry.core.jdbc.realm.RegistryAuthorizationManager) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Aggregations

UserStoreException (org.wso2.carbon.user.api.UserStoreException)127 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)65 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)47 Test (org.junit.Test)37 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)37 RealmService (org.wso2.carbon.user.core.service.RealmService)36 ArrayList (java.util.ArrayList)33 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)33 API (org.wso2.carbon.apimgt.api.model.API)31 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)27 HashMap (java.util.HashMap)25 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)23 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)23 Resource (org.wso2.carbon.registry.core.Resource)23 Endpoint (org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)21 JSONObject (org.json.simple.JSONObject)20 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)20 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)20 HashSet (java.util.HashSet)19 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)18