Search in sources :

Example 6 with Tenant

use of org.wso2.carbon.user.core.tenant.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 7 with Tenant

use of org.wso2.carbon.user.core.tenant.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 8 with Tenant

use of org.wso2.carbon.user.core.tenant.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)

Example 9 with Tenant

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

the class AbstractAPIManager method getAllDocumentation.

public List<Documentation> getAllDocumentation(APIIdentifier apiId, String loggedUsername) throws APIManagementException {
    List<Documentation> documentationList = new ArrayList<Documentation>();
    try {
        String tenantDomain = getTenantDomain(apiId);
        Registry registryType;
        /* If the API provider is a tenant, load tenant registry*/
        boolean isTenantMode = (tenantDomain != null);
        if ((isTenantMode && this.tenantDomain == null) || (isTenantMode && isTenantDomainNotMatching(tenantDomain))) {
            // Tenant store anonymous mode
            int tenantId = getTenantManager().getTenantId(tenantDomain);
            registryType = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId);
        } else {
            registryType = registry;
        }
        String apiOrAPIProductDocPath = APIUtil.getAPIOrAPIProductDocPath(apiId);
        String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
        String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
        if (registry.resourceExists(apiOrAPIProductDocPath)) {
            Resource resource = registry.get(apiOrAPIProductDocPath);
            if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                String[] docsPaths = ((org.wso2.carbon.registry.core.Collection) resource).getChildren();
                for (String docPath : docsPaths) {
                    if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
                        Resource docResource = registry.get(docPath);
                        GenericArtifactManager artifactManager = getAPIGenericArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
                        GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
                        Documentation doc = APIUtil.getDocumentation(docArtifact, apiId.getProviderName());
                        Date contentLastModifiedDate;
                        Date docLastModifiedDate = docResource.getLastModified();
                        if (Documentation.DocumentSourceType.INLINE.equals(doc.getSourceType())) {
                            String contentPath = APIUtil.getAPIDocContentPath(apiId, doc.getName());
                            try {
                                contentLastModifiedDate = registryType.get(contentPath).getLastModified();
                                doc.setLastUpdated((contentLastModifiedDate.after(docLastModifiedDate) ? contentLastModifiedDate : docLastModifiedDate));
                            } catch (org.wso2.carbon.registry.core.secure.AuthorizationFailedException e) {
                            // do nothing. Permission not allowed to access the doc.
                            }
                        } else if (Documentation.DocumentSourceType.MARKDOWN.equals(doc.getSourceType())) {
                            String contentPath = APIUtil.getAPIDocContentPath(apiId, doc.getName());
                            try {
                                contentLastModifiedDate = registryType.get(contentPath).getLastModified();
                                doc.setLastUpdated((contentLastModifiedDate.after(docLastModifiedDate) ? contentLastModifiedDate : docLastModifiedDate));
                            } catch (org.wso2.carbon.registry.core.secure.AuthorizationFailedException e) {
                            // do nothing. Permission not allowed to access the doc.
                            }
                        } else {
                            doc.setLastUpdated(docLastModifiedDate);
                        }
                        documentationList.add(doc);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Failed to get documentations for api " + apiId.getApiName();
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Failed to get documentations for api " + apiId.getApiName();
        throw new APIManagementException(msg, e);
    }
    return documentationList;
}
Also used : ArrayList(java.util.ArrayList) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) 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) Date(java.util.Date) Collection(org.wso2.carbon.registry.core.Collection)

Example 10 with Tenant

use of org.wso2.carbon.user.core.tenant.Tenant 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)

Aggregations

UserStoreException (org.wso2.carbon.user.api.UserStoreException)194 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)180 ArrayList (java.util.ArrayList)177 SQLException (java.sql.SQLException)170 PreparedStatement (java.sql.PreparedStatement)156 Connection (java.sql.Connection)155 HashMap (java.util.HashMap)128 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)110 ResultSet (java.sql.ResultSet)107 IOException (java.io.IOException)98 Map (java.util.Map)78 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)77 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)71 RealmService (org.wso2.carbon.user.core.service.RealmService)64 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)55 Test (org.testng.annotations.Test)54 List (java.util.List)50 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)48 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)48