Search in sources :

Example 31 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method deleteDocumentation.

@Override
public void deleteDocumentation(Organization org, String apiId, String docId) throws DocumentationPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when removing documentation of " + apiId + " Document ID " + docId;
            log.error(errorMessage);
            throw new DocumentationPersistenceException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(docId);
        String docPath = artifact.getPath();
        if (docPath != null) {
            if (registry.resourceExists(docPath)) {
                registry.delete(docPath);
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        throw new DocumentationPersistenceException("Failed to delete documentation", 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) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 32 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchAPIsForDevPortal.

@Override
public DevPortalAPISearchResult searchAPIsForDevPortal(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
    String requestedTenantDomain = org.getName();
    boolean isTenantFlowStarted = false;
    DevPortalAPISearchResult result = null;
    try {
        RegistryHolder holder = getRegistry(ctx.getUserame(), requestedTenantDomain);
        Registry userRegistry = holder.getRegistry();
        int tenantIDLocal = holder.getTenantId();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        log.debug("Requested query for devportal search: " + searchQuery);
        String modifiedQuery = RegistrySearchUtil.getDevPortalSearchQuery(searchQuery, ctx, isAllowDisplayAPIsWithMultipleStatus(), isAllowDisplayAPIsWithMultipleVersions());
        log.debug("Modified query for devportal search: " + modifiedQuery);
        String userNameLocal;
        if (holder.isAnonymousMode()) {
            userNameLocal = APIConstants.WSO2_ANONYMOUS_USER;
        } else {
            userNameLocal = getTenantAwareUsername(ctx.getUserame());
        }
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userNameLocal);
        if (searchQuery != null && searchQuery.startsWith(APIConstants.DOCUMENTATION_SEARCH_TYPE_PREFIX)) {
            result = searchPaginatedDevPortalAPIsByDoc(userRegistry, tenantIDLocal, searchQuery.split(":")[1], userNameLocal, start, offset);
        } else {
            result = searchPaginatedDevPortalAPIs(userRegistry, tenantIDLocal, modifiedQuery, start, offset);
        }
    } catch (APIManagementException e) {
        throw new APIPersistenceException("Error while searching APIs ", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) DevPortalAPISearchResult(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPISearchResult) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry)

Example 33 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceUtil method getAPIForSearch.

public static PublisherAPI getAPIForSearch(GenericArtifact apiArtifact) throws APIPersistenceException {
    PublisherAPI api = new PublisherAPI();
    try {
        api.setContext(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
        api.setDescription(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
        api.setId(apiArtifact.getId());
        api.setStatus(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS));
        api.setApiName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
        api.setProviderName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
        ;
        api.setVersion(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
        api.setAdvertiseOnly(Boolean.parseBoolean(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY)));
    } catch (GovernanceException e) {
        throw new APIPersistenceException("Error while extracting api attributes ", e);
    }
    return api;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException)

Example 34 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceUtil method getDevPortalAPIForSearch.

public static DevPortalAPI getDevPortalAPIForSearch(GenericArtifact apiArtifact) throws APIPersistenceException {
    DevPortalAPI api = new DevPortalAPI();
    try {
        api.setContext(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
        api.setDescription(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
        api.setId(apiArtifact.getId());
        api.setStatus(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS));
        api.setApiName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
        api.setProviderName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
        ;
        api.setVersion(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
    } catch (GovernanceException e) {
        throw new APIPersistenceException("Error while extracting api attributes ", e);
    }
    return api;
}
Also used : DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException)

Example 35 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testUpdateAPI.

@Test
public void testUpdateAPI() throws APIPersistenceException, RegistryException, APIManagementException {
    PublisherAPI publisherAPI = new PublisherAPI();
    publisherAPI.setDescription("Modified description");
    API api = APIMapper.INSTANCE.toApi(publisherAPI);
    Registry registry = Mockito.mock(UserRegistry.class);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.get(anyString())).thenReturn(resource);
    Tag[] tags = new Tag[0];
    Mockito.when(registry.getTags(anyString())).thenReturn(tags);
    GenericArtifact existArtifact = PersistenceHelper.getSampleAPIArtifact();
    String apiUUID = existArtifact.getId();
    PowerMockito.mockStatic(RegistryPersistenceUtil.class);
    GenericArtifactManager manager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
    Mockito.when(manager.getGenericArtifact(apiUUID)).thenReturn(existArtifact);
    Mockito.doNothing().when(manager).updateGenericArtifact(existArtifact);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
    GenericArtifact updatedArtifact = PersistenceHelper.getSampleAPIArtifact();
    updatedArtifact.setAttribute(APIConstants.API_OVERVIEW_DESCRIPTION, api.getDescription());
    PowerMockito.when(RegistryPersistenceUtil.createAPIArtifactContent(any(GenericArtifact.class), any(API.class))).thenReturn(updatedArtifact);
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, existArtifact);
    PublisherAPI updatedAPI = apiPersistenceInstance.updateAPI(org, publisherAPI);
    Assert.assertEquals("Updated API description does not match", "Modified description", updatedAPI.getDescription());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) Tag(org.wso2.carbon.registry.core.Tag) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)83 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)55 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)49 Registry (org.wso2.carbon.registry.core.Registry)49 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)47 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 Resource (org.wso2.carbon.registry.core.Resource)40 API (org.wso2.carbon.apimgt.api.model.API)35 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)34 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)32 Test (org.junit.Test)29 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)27 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 ArrayList (java.util.ArrayList)21 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)19 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)18 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)16