Search in sources :

Example 31 with Tenant

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

the class AbstractAPIManager method getGraphqlSchemaDefinition.

/**
 * Returns the graphQL content in registry specified by the wsdl name
 *
 * @param apiId Api Identifier
 * @return graphQL content matching name if exist else null
 */
@Override
public String getGraphqlSchemaDefinition(APIIdentifier apiId) throws APIManagementException {
    String apiTenantDomain = getTenantDomain(apiId);
    String schema;
    try {
        Registry registryType;
        // Tenant store anonymous mode if current tenant and the required tenant is not matching
        if (this.tenantDomain == null || isTenantDomainNotMatching(apiTenantDomain)) {
            int tenantId = getTenantManager().getTenantId(apiTenantDomain);
            registryType = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId);
        } else {
            registryType = registry;
        }
        schema = schemaDef.getGraphqlSchemaDefinition(apiId, registryType);
    } catch (org.wso2.carbon.user.api.UserStoreException | RegistryException e) {
        String msg = "Failed to get graphql schema definition of Graphql API : " + apiId;
        throw new APIManagementException(msg, e);
    }
    return schema;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 32 with Tenant

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

the class AbstractAPIManager method getApplicationByUUID.

/**
 * Returns the corresponding application given the uuid with keys for a specific tenant.
 *
 * @param uuid         uuid of the Application.
 * @param tenantDomain domain of the accessed store.
 * @return it will return Application corresponds to the uuid provided.
 * @throws APIManagementException
 */
public Application getApplicationByUUID(String uuid, String tenantDomain) throws APIManagementException {
    Application application = apiMgtDAO.getApplicationByUUID(uuid);
    if (application != null) {
        Set<APIKey> keys = getApplicationKeys(application.getId(), tenantDomain);
        for (APIKey key : keys) {
            if (APIConstants.JWT.equals(application.getTokenType())) {
                key.setAccessToken("");
            }
            application.addKey(key);
        }
        int subscriptionCount = apiMgtDAO.getSubscriptionCountByApplicationId(application, tenantDomain);
        application.setSubscriptionCount(subscriptionCount);
    }
    return application;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 33 with Tenant

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

the class AbstractAPIManager method getIcon.

public ResourceFile getIcon(APIIdentifier identifier) throws APIManagementException {
    String artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
    String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
    String tenantDomain = getTenantDomain(identifier);
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            startTenantFlow(tenantDomain);
            isTenantFlowStarted = true;
        }
        /* If the API provider is a tenant, load tenant registry*/
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            int id = getTenantManager().getTenantId(tenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                registry = getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        if (registry.resourceExists(thumbPath)) {
            Resource res = registry.get(thumbPath);
            return new ResourceFile(res.getContentStream(), res.getMediaType());
        } else if (registry.resourceExists(oldThumbPath)) {
            Resource res = registry.get(oldThumbPath);
            return new ResourceFile(res.getContentStream(), res.getMediaType());
        }
    } catch (RegistryException e) {
        String msg = "Error while loading API icon of API " + identifier.getApiName() + ":" + identifier.getVersion() + " from the registry";
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Error while loading API icon of API " + identifier.getApiName() + ":" + identifier.getVersion();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
    return null;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 34 with Tenant

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

the class AbstractAPIManager method getAPIProductbyUUID.

/**
 * Get API Product by registry artifact id
 *
 * @param uuid                  Registry artifact id
 * @param requestedTenantDomain tenantDomain for the registry
 * @return API Product of the provided artifact id
 * @throws APIManagementException
 */
public APIProduct getAPIProductbyUUID(String uuid, String requestedTenantDomain) throws APIManagementException {
    try {
        Registry registry;
        if (requestedTenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(requestedTenantDomain)) {
            int id = getTenantManager().getTenantId(requestedTenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                // at this point, requested tenant = carbon.super but logged in user is anonymous or tenant
                registry = getRegistryService().getGovernanceSystemRegistry(MultitenantConstants.SUPER_TENANT_ID);
            } else {
                // both requested tenant and logged in user's tenant are carbon.super
                registry = this.registry;
            }
        }
        GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry, APIConstants.API_KEY);
        GenericArtifact apiProductArtifact = artifactManager.getGenericArtifact(uuid);
        if (apiProductArtifact != null) {
            APIProduct apiProduct = getApiProduct(registry, apiProductArtifact);
            WorkflowDTO workflowDTO = APIUtil.getAPIWorkflowStatus(apiProduct.getUuid(), WF_TYPE_AM_API_PRODUCT_STATE);
            if (workflowDTO != null) {
                WorkflowStatus status = workflowDTO.getStatus();
                apiProduct.setWorkflowStatus(status.toString());
            }
            return apiProduct;
        } else {
            String msg = "Failed to get API Product. API Product artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (RegistryException e) {
        String msg = "Failed to get API Product";
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Failed to get API Product";
        throw new APIManagementException(msg, e);
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) WorkflowStatus(org.wso2.carbon.apimgt.impl.workflow.WorkflowStatus) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 35 with Tenant

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

the class AbstractAPIManager method getDocumentationContent.

public String getDocumentationContent(Identifier identifier, String documentationName) throws APIManagementException {
    String contentPath = StringUtils.EMPTY;
    String identifierType = StringUtils.EMPTY;
    if (identifier instanceof APIIdentifier) {
        contentPath = APIUtil.getAPIDocPath((APIIdentifier) identifier) + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + documentationName;
        identifierType = APIConstants.API_IDENTIFIER_TYPE;
    } else if (identifier instanceof APIProductIdentifier) {
        contentPath = APIUtil.getProductDocPath((APIProductIdentifier) identifier) + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + documentationName;
        identifierType = APIConstants.API_PRODUCT_IDENTIFIER_TYPE;
    }
    String tenantDomain = getTenantDomain(identifier);
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            startTenantFlow(tenantDomain);
            isTenantFlowStarted = true;
        }
        /* If the API provider is a tenant, load tenant registry*/
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            int id = getTenantManager().getTenantId(tenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                registry = getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        if (registry.resourceExists(contentPath)) {
            Resource docContent = registry.get(contentPath);
            Object content = docContent.getContent();
            if (content != null) {
                return new String((byte[]) docContent.getContent(), Charset.defaultCharset());
            }
        }
    } catch (RegistryException e) {
        String msg = "No document content found for documentation: " + documentationName + " of " + identifierType + " : " + identifier.getName();
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Failed to get document content found for documentation: " + documentationName + " of " + identifierType + " : " + identifier.getName();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
    return null;
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) JSONObject(org.json.simple.JSONObject)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)180 UserStoreException (org.wso2.carbon.user.api.UserStoreException)88 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)83 ArrayList (java.util.ArrayList)79 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 PreparedStatement (java.sql.PreparedStatement)51 SQLException (java.sql.SQLException)50 IOException (java.io.IOException)49 Connection (java.sql.Connection)49 HashMap (java.util.HashMap)44 ResultSet (java.sql.ResultSet)43 JSONObject (org.json.simple.JSONObject)41 Resource (org.wso2.carbon.registry.core.Resource)40 Registry (org.wso2.carbon.registry.core.Registry)38 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)34 API (org.wso2.carbon.apimgt.api.model.API)34 Test (org.junit.Test)33 File (java.io.File)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)30