Search in sources :

Example 66 with RegistryException

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

the class RegistryPersistenceImpl method getRegistry.

protected RegistryHolder getRegistry(String requestedTenantDomain) throws APIPersistenceException {
    String userTenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    log.debug("Accessing system registry in tenant domain " + userTenantDomain + ". Requested tenant domain: " + requestedTenantDomain);
    boolean tenantFlowStarted = false;
    Registry registry;
    RegistryHolder holder = new RegistryHolder();
    try {
        if (requestedTenantDomain != null) {
            int id = getTenantManager().getTenantId(requestedTenantDomain);
            RegistryPersistenceUtil.startTenantFlow(requestedTenantDomain);
            tenantFlowStarted = true;
            if (userTenantDomain != null && !userTenantDomain.equals(requestedTenantDomain)) {
                // cross tenant
                log.debug("Cross tenant user from tenant " + userTenantDomain + " accessing " + requestedTenantDomain + " registry");
                loadTenantRegistry(id);
                registry = getRegistryService().getGovernanceSystemRegistry(id);
                holder.setTenantId(id);
                ServiceReferenceHolder.setUserRealm((ServiceReferenceHolder.getInstance().getRealmService().getBootstrapRealm()));
            } else {
                log.debug("Same tenant accessing registry of tenant " + userTenantDomain + ":" + tenantId);
                loadTenantRegistry(tenantId);
                registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
                RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
                holder.setTenantId(tenantId);
                ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
            }
        } else {
            log.debug("Same tenant user accessing registry of tenant " + userTenantDomain + ":" + tenantId);
            loadTenantRegistry(tenantId);
            registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
            RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
            ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
            holder.setTenantId(tenantId);
        }
    } catch (RegistryException | UserStoreException | APIManagementException e) {
        String msg = "Failed to get API";
        throw new APIPersistenceException(msg, e);
    }
    holder.setRegistry(registry);
    holder.setTenantFlowStarted(tenantFlowStarted);
    return holder;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.api.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 67 with RegistryException

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

the class RegistryPersistenceImpl method saveAPIStatus.

/**
 * Persist API Status into a property of API Registry resource
 *
 * @param artifactId API artifact ID
 * @param apiStatus  Current status of the API
 * @throws APIManagementException on error
 */
private void saveAPIStatus(Registry registry, String artifactId, String apiStatus) throws APIManagementException {
    try {
        Resource resource = registry.get(artifactId);
        if (resource != null) {
            String propValue = resource.getProperty(APIConstants.API_STATUS);
            if (propValue == null) {
                resource.addProperty(APIConstants.API_STATUS, apiStatus);
            } else {
                resource.setProperty(APIConstants.API_STATUS, apiStatus);
            }
            registry.put(artifactId, resource);
        }
    } catch (RegistryException e) {
        handleException("Error while adding API", e);
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 68 with RegistryException

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

the class RegistryPersistenceImpl method changeAPILifeCycle.

@Override
public void changeAPILifeCycle(Organization org, String apiId, String status) throws APIPersistenceException {
    GenericArtifactManager artifactManager = null;
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        if (GovernanceUtils.findGovernanceArtifactConfiguration(APIConstants.API_KEY, registry) != null) {
            artifactManager = new GenericArtifactManager(registry, APIConstants.API_KEY);
            GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
            String action = LCManagerFactory.getInstance().getLCManager().getTransitionAction(apiArtifact.getLifecycleState().toUpperCase(), status.toUpperCase());
            apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
        } else {
            log.warn("Couldn't find GovernanceArtifactConfiguration of RXT: " + APIConstants.API_KEY + ". Tenant id set in registry : " + ((UserRegistry) registry).getTenantId() + ", Tenant domain set in PrivilegedCarbonContext: " + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
        }
    } catch (GovernanceException e) {
        throw new APIPersistenceException("Error while changing the lifecycle. ", e);
    } catch (RegistryException e) {
        throw new APIPersistenceException("Error while accessing the registry. ", e);
    } catch (PersistenceException e) {
        throw new APIPersistenceException("Error while accessing the lifecycle. ", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 69 with RegistryException

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

the class RegistryPersistenceImpl method addMediationPolicy.

@Override
public Mediation addMediationPolicy(Organization org, String apiId, Mediation mediation) throws MediationPolicyPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        BasicAPI api = getbasicAPIInfo(apiId, registry);
        if (api == null) {
            throw new MediationPolicyPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
        }
        String resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + api.apiProvider + RegistryConstants.PATH_SEPARATOR + api.apiName + RegistryConstants.PATH_SEPARATOR + api.apiVersion + RegistryConstants.PATH_SEPARATOR + mediation.getType() + RegistryConstants.PATH_SEPARATOR + mediation.getName();
        if (registry.resourceExists(resourcePath)) {
            throw new MediationPolicyPersistenceException("Mediation policy already exists for the given name " + mediation.getName(), ExceptionCodes.MEDIATION_POLICY_API_ALREADY_EXISTS);
        }
        Resource policy = registry.newResource();
        policy.setContent(mediation.getConfig());
        policy.setMediaType("application/xml");
        registry.put(resourcePath, policy);
        mediation.setId(policy.getUUID());
        return mediation;
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error while adding the mediation to the registry";
        throw new MediationPolicyPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 70 with RegistryException

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

the class RegistryPersistenceImpl method getThumbnail.

@Override
public ResourceFile getThumbnail(Organization org, String apiId) throws ThumbnailPersistenceException {
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
        if (apiArtifact == null) {
            return null;
        }
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        String artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String artifactPath = apiPath.substring(0, prependIndex);
        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 | APIPersistenceException e) {
        String msg = "Error while loading API icon of API " + apiId + " from the registry";
        throw new ThumbnailPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return null;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceFile(org.wso2.carbon.apimgt.persistence.dto.ResourceFile) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) 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