Search in sources :

Example 31 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class AbstractAPIManager method getAPIorAPIProductByUUID.

/**
 * Get API or APIProduct by registry artifact id
 *
 * @param uuid                  Registry artifact id
 * @param requestedTenantDomain tenantDomain for the registry
 * @return ApiTypeWrapper wrapping the API or APIProduct of the provided artifact id
 * @throws APIManagementException
 */
public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String requestedTenantDomain) throws APIManagementException {
    boolean tenantFlowStarted = false;
    try {
        Registry registry;
        if (requestedTenantDomain != null) {
            int id = getTenantManager().getTenantId(requestedTenantDomain);
            startTenantFlow(requestedTenantDomain);
            tenantFlowStarted = true;
            if (APIConstants.WSO2_ANONYMOUS_USER.equals(this.username)) {
                registry = getRegistryService().getGovernanceUserRegistry(this.username, id);
            } else if (this.tenantDomain != null && !this.tenantDomain.equals(requestedTenantDomain)) {
                registry = getRegistryService().getGovernanceSystemRegistry(id);
            } else {
                registry = this.registry;
            }
        } else {
            registry = this.registry;
        }
        GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(uuid);
        if (apiArtifact != null) {
            String type = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
            if (APIConstants.API_PRODUCT.equals(type)) {
                APIProduct apiProduct = getApiProduct(registry, apiArtifact);
                String productTenantDomain = getTenantDomain(apiProduct.getId());
                if (APIConstants.API_GLOBAL_VISIBILITY.equals(apiProduct.getVisibility())) {
                    return new ApiTypeWrapper(apiProduct);
                }
                if (this.tenantDomain == null || !this.tenantDomain.equals(productTenantDomain)) {
                    throw new APIManagementException("User " + username + " does not have permission to view API Product : " + apiProduct.getId().getName());
                }
                return new ApiTypeWrapper(apiProduct);
            } else {
                API api = getApiForPublishing(registry, apiArtifact);
                String apiTenantDomain = getTenantDomain(api.getId());
                if (APIConstants.API_GLOBAL_VISIBILITY.equals(api.getVisibility())) {
                    return new ApiTypeWrapper(api);
                }
                if (this.tenantDomain == null || !this.tenantDomain.equals(apiTenantDomain)) {
                    throw new APIManagementException("User " + username + " does not have permission to view API : " + api.getId().getApiName());
                }
                return new ApiTypeWrapper(api);
            }
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (RegistryException | org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Failed to get API";
        throw new APIManagementException(msg, e);
    } finally {
        if (tenantFlowStarted) {
            endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) 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) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API)

Example 32 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.

the class AbstractAPIManager method getThumbnailLastUpdatedTime.

/**
 * get the thumbnailLastUpdatedTime for a thumbnail for a given api
 *
 * @param apiIdentifier
 * @return
 * @throws APIManagementException
 */
@Override
public String getThumbnailLastUpdatedTime(APIIdentifier apiIdentifier) throws APIManagementException {
    String artifactPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getApiName() + RegistryConstants.PATH_SEPARATOR + apiIdentifier.getVersion();
    String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
    try {
        if (registry.resourceExists(thumbPath)) {
            Resource res = registry.get(thumbPath);
            Date lastModifiedTime = res.getLastModified();
            return lastModifiedTime == null ? String.valueOf(res.getCreatedTime().getTime()) : String.valueOf(lastModifiedTime.getTime());
        }
    } catch (RegistryException e) {
        String msg = "Error while loading API icon from the registry";
        throw new APIManagementException(msg, e);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Date(java.util.Date)

Example 33 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException 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 34 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException 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 35 with RegistryException

use of org.wso2.carbon.registry.core.exceptions.RegistryException 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)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)235 Resource (org.wso2.carbon.registry.core.Resource)196 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)167 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)145 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)104 Registry (org.wso2.carbon.registry.core.Registry)95 Test (org.junit.Test)81 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)81 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)67 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)61 UserStoreException (org.wso2.carbon.user.api.UserStoreException)60 API (org.wso2.carbon.apimgt.api.model.API)58 IOException (java.io.IOException)57 ArrayList (java.util.ArrayList)55 QName (javax.xml.namespace.QName)42 APIResource (org.wso2.carbon.apimgt.api.doc.model.APIResource)40 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)40 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)40