Search in sources :

Example 76 with Registry

use of org.wso2.carbon.registry.api.Registry 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 77 with Registry

use of org.wso2.carbon.registry.api.Registry 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 78 with Registry

use of org.wso2.carbon.registry.api.Registry 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)

Example 79 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getPublisherAPI.

@Override
public PublisherAPI getPublisherAPI(Organization org, String apiId) throws APIPersistenceException {
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        tenantFlowStarted = holder.isTenantFlowStarted();
        Registry registry = holder.getRegistry();
        GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
        if (apiArtifact != null) {
            API api = RegistryPersistenceUtil.getApiForPublishing(registry, apiArtifact);
            String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
            int prependIndex = apiPath.lastIndexOf("/api");
            String apiSourcePath = apiPath.substring(0, prependIndex);
            String definitionPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME;
            String asyncApiDefinitionPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ASYNC_API_DEFINITION_RESOURCE_NAME;
            if (registry.resourceExists(definitionPath)) {
                Resource apiDocResource = registry.get(definitionPath);
                String apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
                api.setSwaggerDefinition(apiDocContent);
            }
            if (APIConstants.API_TYPE_SOAPTOREST.equals(api.getType())) {
                List<SOAPToRestSequence> list = getSoapToRestSequences(registry, api, Direction.IN);
                list.addAll(getSoapToRestSequences(registry, api, Direction.OUT));
                api.setSoapToRestSequences(list);
            } else if (APIConstants.API_TYPE_WEBSUB.equals(api.getType()) || APIConstants.API_TYPE_WS.equals(api.getType()) || APIConstants.API_TYPE_SSE.equals(api.getType()) || APIConstants.API_TYPE_WEBHOOK.equals(api.getType())) {
                if (asyncApiDefinitionPath != null) {
                    if (registry.resourceExists(asyncApiDefinitionPath)) {
                        Resource apiDocResource = registry.get(asyncApiDefinitionPath);
                        String apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
                        api.setAsyncApiDefinition(apiDocContent);
                    }
                }
            }
            PublisherAPI pubApi = APIMapper.INSTANCE.toPublisherApi(api);
            if (log.isDebugEnabled()) {
                log.debug("API for id " + apiId + " : " + pubApi.toString());
            }
            return pubApi;
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + apiId + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (RegistryException e) {
        String msg = "Failed to get API";
        throw new APIPersistenceException(msg, e);
    } catch (APIManagementException e) {
        String msg = "Failed to get API";
        throw new APIPersistenceException(msg, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) 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) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API)

Example 80 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchPaginatedPublisherAPIs.

private PublisherAPISearchResult searchPaginatedPublisherAPIs(Registry userRegistry, int tenantIDLocal, String searchQuery, int start, int offset) throws APIManagementException {
    int totalLength = 0;
    PublisherAPISearchResult searchResults = new PublisherAPISearchResult();
    try {
        final int maxPaginationLimit = getMaxPaginationLimit();
        PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
        List<GovernanceArtifact> governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(searchQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE, true);
        totalLength = PaginationContext.getInstance().getLength();
        boolean isFound = true;
        if (governanceArtifacts == null || governanceArtifacts.size() == 0) {
            if (searchQuery.contains(APIConstants.API_OVERVIEW_PROVIDER)) {
                searchQuery = searchQuery.replaceAll(APIConstants.API_OVERVIEW_PROVIDER, APIConstants.API_OVERVIEW_OWNER);
                governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(searchQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE, true);
                if (governanceArtifacts == null || governanceArtifacts.size() == 0) {
                    isFound = false;
                }
            } else {
                isFound = false;
            }
        }
        if (!isFound) {
            return searchResults;
        }
        // Check to see if we can speculate that there are more APIs to be loaded
        if (maxPaginationLimit == totalLength) {
            // Remove the additional 1 added earlier when setting max pagination limit
            --totalLength;
        }
        List<PublisherAPIInfo> publisherAPIInfoList = new ArrayList<PublisherAPIInfo>();
        int tempLength = 0;
        for (GovernanceArtifact artifact : governanceArtifacts) {
            PublisherAPIInfo apiInfo = new PublisherAPIInfo();
            String artifactPath = GovernanceUtils.getArtifactPath(userRegistry, artifact.getId());
            Resource apiResource = userRegistry.get(artifactPath);
            apiInfo.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
            apiInfo.setId(artifact.getId());
            apiInfo.setApiName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
            apiInfo.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
            apiInfo.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
            apiInfo.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
            apiInfo.setStatus(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS));
            apiInfo.setThumbnail(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
            apiInfo.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
            apiInfo.setAudience(artifact.getAttribute(APIConstants.API_OVERVIEW_AUDIENCE));
            apiInfo.setCreatedTime(String.valueOf(apiResource.getCreatedTime().getTime()));
            apiInfo.setUpdatedTime(apiResource.getLastModified());
            apiInfo.setGatewayVendor(String.valueOf(artifact.getAttribute(APIConstants.API_GATEWAY_VENDOR)));
            apiInfo.setAdvertiseOnly(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY)));
            publisherAPIInfoList.add(apiInfo);
            // Ensure the APIs returned matches the length, there could be an additional API
            // returned due incrementing the pagination limit when getting from registry
            tempLength++;
            if (tempLength >= totalLength) {
                break;
            }
        }
        // Sort the publisherAPIInfoList according to the API name.
        Collections.sort(publisherAPIInfoList, new PublisherAPISearchResultComparator());
        searchResults.setPublisherAPIInfoList(publisherAPIInfoList);
        searchResults.setReturnedAPIsCount(publisherAPIInfoList.size());
        searchResults.setTotalAPIsCount(totalLength);
    } catch (RegistryException e) {
        String msg = "Failed to search APIs with type";
        throw new APIManagementException(msg, e);
    } finally {
        PaginationContext.destroy();
    }
    return searchResults;
}
Also used : GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PublisherAPIInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIInfo) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPISearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherAPISearchResult) PublisherAPISearchResultComparator(org.wso2.carbon.apimgt.persistence.utils.PublisherAPISearchResultComparator)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)230 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)225 Resource (org.wso2.carbon.registry.core.Resource)210 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)197 Registry (org.wso2.carbon.registry.core.Registry)125 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)114 Test (org.junit.Test)111 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)107 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)91 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)89 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)80 IOException (java.io.IOException)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)74 UserStoreException (org.wso2.carbon.user.api.UserStoreException)71 API (org.wso2.carbon.apimgt.api.model.API)70 ArrayList (java.util.ArrayList)67 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)58 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)51 HashMap (java.util.HashMap)48 OMElement (org.apache.axiom.om.OMElement)48