Search in sources :

Example 1 with CollectionImpl

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

the class RegistryPersistenceImpl method deleteAPIProduct.

@Override
public void deleteAPIProduct(Organization org, String apiId) throws APIPersistenceException {
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        tenantFlowStarted = holder.isTenantFlowStarted();
        Registry registry = holder.getRegistry();
        GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when deleting API Product" + apiId;
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact apiProductArtifact = artifactManager.getGenericArtifact(apiId);
        APIProductIdentifier identifier = new APIProductIdentifier(apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
        // this is the product resource collection path
        String productResourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
        // this is the product rxt instance path
        String apiProductArtifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
        Resource apiProductResource = registry.get(productResourcePath);
        String productResourceUUID = apiProductResource.getUUID();
        if (productResourceUUID == null) {
            throw new APIManagementException("artifact id is null for : " + productResourcePath);
        }
        Resource apiArtifactResource = registry.get(apiProductArtifactPath);
        String apiArtifactResourceUUID = apiArtifactResource.getUUID();
        if (apiArtifactResourceUUID == null) {
            throw new APIManagementException("artifact id is null for : " + apiProductArtifactPath);
        }
        // Delete the dependencies associated with the api product artifact
        GovernanceArtifact[] dependenciesArray = apiProductArtifact.getDependencies();
        if (dependenciesArray.length > 0) {
            for (GovernanceArtifact artifact : dependenciesArray) {
                registry.delete(artifact.getPath());
            }
        }
        // delete registry resources
        artifactManager.removeGenericArtifact(apiProductArtifact);
        artifactManager.removeGenericArtifact(productResourceUUID);
        /* remove empty directories */
        String apiProductCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
        if (registry.resourceExists(apiProductCollectionPath)) {
            // at the moment product versioning is not supported so we are directly deleting this collection as
            // this is known to be empty
            registry.delete(apiProductCollectionPath);
        }
        String productProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
        if (registry.resourceExists(productProviderPath)) {
            Resource providerCollection = registry.get(productProviderPath);
            CollectionImpl collection = (CollectionImpl) providerCollection;
            // if there is no api product for given provider delete the provider directory
            if (collection.getChildCount() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("No more API Products from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
                }
                registry.delete(productProviderPath);
            }
        }
    } 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) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) 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) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl)

Example 2 with CollectionImpl

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

the class RegistryPersistenceImpl method deleteAPI.

@Override
public void deleteAPI(Organization org, String apiId) throws APIPersistenceException {
    boolean transactionCommitted = false;
    boolean tenantFlowStarted = false;
    Registry registry = null;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        registry = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        registry.beginTransaction();
        GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when deleting API " + apiId;
            log.error(errorMessage);
            throw new APIPersistenceException(errorMessage);
        }
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
        APIIdentifier identifier = new APIIdentifier(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
        // Delete the dependencies associated  with the api artifact
        GovernanceArtifact[] dependenciesArray = apiArtifact.getDependencies();
        if (dependenciesArray.length > 0) {
            for (GovernanceArtifact artifact : dependenciesArray) {
                registry.delete(artifact.getPath());
            }
        }
        artifactManager.removeGenericArtifact(apiArtifact);
        String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
        Resource apiResource = registry.get(path);
        String artifactId = apiResource.getUUID();
        artifactManager.removeGenericArtifact(artifactId);
        String thumbPath = RegistryPersistenceUtil.getIconPath(identifier);
        if (registry.resourceExists(thumbPath)) {
            registry.delete(thumbPath);
        }
        String wsdlArchivePath = RegistryPersistenceUtil.getWsdlArchivePath(identifier);
        if (registry.resourceExists(wsdlArchivePath)) {
            registry.delete(wsdlArchivePath);
        }
        /*Remove API Definition Resource - swagger*/
        String apiDefinitionFilePath = APIConstants.API_DOC_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + '-' + identifier.getVersion() + '-' + identifier.getProviderName();
        if (registry.resourceExists(apiDefinitionFilePath)) {
            registry.delete(apiDefinitionFilePath);
        }
        /*remove empty directories*/
        String apiCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName();
        if (registry.resourceExists(apiCollectionPath)) {
            Resource apiCollection = registry.get(apiCollectionPath);
            CollectionImpl collection = (CollectionImpl) apiCollection;
            // if there is no other versions of apis delete the directory of the api
            if (collection.getChildCount() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("No more versions of the API found, removing API collection from registry");
                }
                registry.delete(apiCollectionPath);
            }
        }
        String apiProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName();
        if (registry.resourceExists(apiProviderPath)) {
            Resource providerCollection = registry.get(apiProviderPath);
            CollectionImpl collection = (CollectionImpl) providerCollection;
            // if there is no api for given provider delete the provider directory
            if (collection.getChildCount() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("No more APIs from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
                }
                registry.delete(apiProviderPath);
            }
        }
        registry.commitTransaction();
        transactionCommitted = true;
    } catch (RegistryException e) {
        throw new APIPersistenceException("Failed to remove the API : " + apiId, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
        try {
            if (!transactionCommitted) {
                registry.rollbackTransaction();
            }
        } catch (RegistryException ex) {
            throw new APIPersistenceException("Error occurred while rolling back the transaction. ", ex);
        }
    }
}
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) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) 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) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 3 with CollectionImpl

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

the class AbstractAPIManagerTestCase method testGetAllApiSpecificMediationPolicies.

@Test
public void testGetAllApiSpecificMediationPolicies() throws RegistryException, APIManagementException, IOException, XMLStreamException {
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    String parentCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
    parentCollectionPath = parentCollectionPath.substring(0, parentCollectionPath.lastIndexOf("/"));
    Collection parentCollection = new CollectionImpl();
    parentCollection.setChildren(new String[] { parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT, parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT });
    Collection childCollection = new CollectionImpl();
    childCollection.setChildren(new String[] { "mediation1" });
    Mockito.when(registry.get(parentCollectionPath)).thenThrow(RegistryException.class).thenReturn(parentCollection);
    Mockito.when(registry.get(parentCollectionPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN)).thenReturn(childCollection);
    Resource resource = new ResourceImpl();
    resource.setUUID(SAMPLE_RESOURCE_ID);
    String mediationPolicyContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"default-endpoint\">\n</sequence>";
    resource.setContent(mediationPolicyContent);
    Mockito.when(registry.get("mediation1")).thenReturn(resource);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    try {
        abstractAPIManager.getAllApiSpecificMediationPolicies(identifier);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error occurred  while getting Api Specific mediation policies "));
    }
    Assert.assertEquals(abstractAPIManager.getAllApiSpecificMediationPolicies(identifier).size(), 1);
    PowerMockito.mockStatic(IOUtils.class);
    PowerMockito.mockStatic(AXIOMUtil.class);
    PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class).thenReturn(mediationPolicyContent);
    PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(XMLStreamException.class);
    // covers exception which is only logged
    abstractAPIManager.getAllApiSpecificMediationPolicies(identifier);
    // covers exception which is only logged
    abstractAPIManager.getAllApiSpecificMediationPolicies(identifier);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with CollectionImpl

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

the class AbstractAPIManagerTestCase method testGetGlobalMediationPolicy.

@Test
public void testGetGlobalMediationPolicy() throws RegistryException, APIManagementException, XMLStreamException, IOException {
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    String resourceUUID = SAMPLE_RESOURCE_ID;
    Collection parentCollection = new CollectionImpl();
    String mediationResourcePath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION;
    String childCollectionPath = mediationResourcePath + "/testMediation";
    parentCollection.setChildren(new String[] { childCollectionPath });
    Mockito.when(registry.get(mediationResourcePath)).thenThrow(RegistryException.class).thenReturn(parentCollection);
    Collection childCollection = new CollectionImpl();
    String resourcePath = childCollectionPath + "/policy1";
    childCollection.setChildren(new String[] { resourcePath });
    Mockito.when(registry.get(childCollectionPath)).thenReturn(childCollection);
    Resource resource = new ResourceImpl(resourcePath, new ResourceDO());
    resource.setUUID(resourceUUID);
    Mockito.when(registry.get(resourcePath)).thenReturn(resource);
    try {
        abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
        Assert.fail("Registry Exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while accessing registry objects"));
    }
    // test for registry exception
    abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
    String mediationPolicyContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<sequence xmlns=\"http://ws.apache.org/ns/synapse\" name=\"default-endpoint\">\n</sequence>";
    resource.setContent(mediationPolicyContent);
    Mediation policy = abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
    Assert.assertNotNull(policy);
    PowerMockito.mockStatic(IOUtils.class);
    PowerMockito.mockStatic(AXIOMUtil.class);
    PowerMockito.when(IOUtils.toString((InputStream) Mockito.any(), Mockito.anyString())).thenThrow(IOException.class).thenReturn(mediationPolicyContent);
    PowerMockito.when(AXIOMUtil.stringToOM(Mockito.anyString())).thenThrow(XMLStreamException.class);
    // cover the logged only exceptions
    abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
    // cover the logged only exceptions
    abstractAPIManager.getGlobalMediationPolicy(resourceUUID);
}
Also used : ResourceDO(org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO) ArrayInputStream(org.apache.derby.iapi.services.io.ArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Collection(org.wso2.carbon.registry.core.Collection) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with CollectionImpl

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

the class AbstractAPIManagerTestCase method testGetAllWsdls.

@Test
public void testGetAllWsdls() throws RegistryException, APIManagementException {
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Collection parentCollection = new CollectionImpl();
    String wsdlResourcepath = APIConstants.API_WSDL_RESOURCE;
    String resourcePath = wsdlResourcepath + "/wsdl1";
    parentCollection.setChildren(new String[] { resourcePath });
    Mockito.when(registry.get(wsdlResourcepath)).thenReturn(parentCollection);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.get(resourcePath)).thenThrow(RegistryException.class).thenReturn(resource);
    Mockito.when(registry.resourceExists(wsdlResourcepath)).thenReturn(true);
    try {
        abstractAPIManager.getAllWsdls();
        Assert.fail("Exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get wsdl list"));
    }
    resource.setUUID(SAMPLE_RESOURCE_ID);
    List<Wsdl> wsdls = abstractAPIManager.getAllWsdls();
    Assert.assertNotNull(wsdls);
    Assert.assertEquals(wsdls.size(), 1);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Wsdl(org.wso2.carbon.apimgt.api.model.Wsdl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

CollectionImpl (org.wso2.carbon.registry.core.CollectionImpl)11 Resource (org.wso2.carbon.registry.core.Resource)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 Test (org.junit.Test)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 Collection (org.wso2.carbon.registry.core.Collection)9 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)9 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)9 ResourceDO (org.wso2.carbon.registry.core.jdbc.dataobjects.ResourceDO)6 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 ArrayInputStream (org.apache.derby.iapi.services.io.ArrayInputStream)4 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)4 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)4 Registry (org.wso2.carbon.registry.core.Registry)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)2 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)2 Mediation (org.wso2.carbon.apimgt.api.model.Mediation)2 Wsdl (org.wso2.carbon.apimgt.api.model.Wsdl)2