Search in sources :

Example 16 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class APIProviderImpl method getAPIbyUUID.

@Override
public API getAPIbyUUID(String uuid, String organization) throws APIManagementException {
    Organization org = new Organization(organization);
    try {
        PublisherAPI publisherAPI = apiPersistenceInstance.getPublisherAPI(org, uuid);
        if (publisherAPI != null) {
            API api = APIMapper.INSTANCE.toApi(publisherAPI);
            APIIdentifier apiIdentifier = api.getId();
            apiIdentifier.setUuid(uuid);
            api.setId(apiIdentifier);
            checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(), api.getAccessControlRoles());
            // ///////////////// Do processing on the data object//////////
            populateRevisionInformation(api, uuid);
            populateAPIInformation(uuid, organization, api);
            loadMediationPoliciesToAPI(api, organization);
            populateAPIStatus(api);
            populateDefaultVersion(api);
            return api;
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Failed to get API", e);
    } catch (OASPersistenceException e) {
        throw new APIManagementException("Error while retrieving the OAS definition", e);
    } catch (ParseException e) {
        throw new APIManagementException("Error while parsing the OAS definition", e);
    } catch (AsyncSpecPersistenceException e) {
        throw new APIManagementException("Error while retrieving the Async API definition", e);
    }
}
Also used : AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ParseException(org.json.simple.parser.ParseException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 17 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class APIProviderImpl method getLightweightAPIByUUID.

/**
 * Get minimal details of API by registry artifact id
 *
 * @param uuid Registry artifact id
 * @param organization identifier of the organization
 * @return API of the provided artifact id
 * @throws APIManagementException
 */
@Override
public API getLightweightAPIByUUID(String uuid, String organization) throws APIManagementException {
    try {
        Organization org = new Organization(organization);
        PublisherAPI publisherAPI = apiPersistenceInstance.getPublisherAPI(org, uuid);
        if (publisherAPI != null) {
            API api = APIMapper.INSTANCE.toApi(publisherAPI);
            checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(), api.getAccessControlRoles());
            // / populate relavant external info
            // environment
            String environmentString = null;
            if (api.getEnvironments() != null) {
                environmentString = String.join(",", api.getEnvironments());
            }
            api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
            // CORS . if null is returned, set default config from the configuration
            if (api.getCorsConfiguration() == null) {
                api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
            }
            api.setOrganization(organization);
            return api;
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException e) {
        String msg = "Failed to get API with uuid " + uuid;
        throw new APIManagementException(msg, e);
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 18 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetAllApis.

@Test
public void testGetAllApis() throws GovernanceException, APIManagementException, APIPersistenceException {
    PowerMockito.mockStatic(APIUtil.class);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiPersistenceInstance);
    PublisherAPISearchResult value = new PublisherAPISearchResult();
    List<PublisherAPIInfo> publisherAPIInfoList = new ArrayList<PublisherAPIInfo>();
    PublisherAPIInfo pubInfo = new PublisherAPI();
    pubInfo.setApiName("TestAPI");
    pubInfo.setContext("/test");
    pubInfo.setId("xxxxxx");
    pubInfo.setProviderName("test");
    pubInfo.setType("API");
    pubInfo.setVersion("1");
    publisherAPIInfoList.add(pubInfo);
    value.setPublisherAPIInfoList(publisherAPIInfoList);
    PowerMockito.when(apiPersistenceInstance.searchAPIsForPublisher(any(Organization.class), any(String.class), any(Integer.class), any(Integer.class), any(UserContext.class), any(String.class), any(String.class))).thenReturn(value);
    List<API> apis = abstractAPIManager.getAllAPIs();
    Assert.assertNotNull(apis);
    Assert.assertEquals(apis.size(), 1);
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) ArrayList(java.util.ArrayList) PublisherAPIInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIInfo) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) PublisherAPISearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherAPISearchResult) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchContentForPublisher.

@Override
public PublisherContentSearchResult searchContentForPublisher(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
    log.debug("Requested query for publisher content search: " + searchQuery);
    Map<String, String> attributes = RegistrySearchUtil.getPublisherSearchAttributes(searchQuery, ctx);
    if (log.isDebugEnabled()) {
        log.debug("Search attributes : " + attributes);
    }
    boolean isTenantFlowStarted = false;
    PublisherContentSearchResult result = null;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        String requestedTenantDomain = org.getName();
        String tenantAwareUsername = getTenantAwareUsername(RegistryPersistenceUtil.getTenantAdminUserName(requestedTenantDomain));
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(tenantAwareUsername);
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifactManager docArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
        int maxPaginationLimit = getMaxPaginationLimit();
        PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
        int tenantId = holder.getTenantId();
        if (tenantId == -1) {
            tenantId = MultitenantConstants.SUPER_TENANT_ID;
        }
        UserRegistry systemUserRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME, tenantId);
        ContentBasedSearchService contentBasedSearchService = new ContentBasedSearchService();
        SearchResultsBean resultsBean = contentBasedSearchService.searchByAttribute(attributes, systemUserRegistry);
        String errorMsg = resultsBean.getErrorMessage();
        if (errorMsg != null) {
            throw new APIPersistenceException("Error while searching " + errorMsg);
        }
        ResourceData[] resourceData = resultsBean.getResourceDataList();
        int totalLength = PaginationContext.getInstance().getLength();
        if (resourceData != null) {
            result = new PublisherContentSearchResult();
            List<SearchContent> contentData = new ArrayList<SearchContent>();
            if (log.isDebugEnabled()) {
                log.debug("Number of records Found: " + resourceData.length);
            }
            for (ResourceData data : resourceData) {
                String resourcePath = data.getResourcePath();
                if (resourcePath.contains(APIConstants.APIMGT_REGISTRY_LOCATION)) {
                    int index = resourcePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
                    resourcePath = resourcePath.substring(index);
                    Resource resource = registry.get(resourcePath);
                    if (APIConstants.DOCUMENT_RXT_MEDIA_TYPE.equals(resource.getMediaType()) || APIConstants.DOCUMENTATION_INLINE_CONTENT_TYPE.equals(resource.getMediaType())) {
                        if (resourcePath.contains(APIConstants.INLINE_DOCUMENT_CONTENT_DIR)) {
                            int indexOfContents = resourcePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
                            resourcePath = resourcePath.substring(0, indexOfContents) + data.getName();
                        }
                        DocumentSearchContent docSearch = new DocumentSearchContent();
                        Resource docResource = registry.get(resourcePath);
                        String docArtifactId = docResource.getUUID();
                        GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId);
                        Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
                        // API associatedAPI = null;
                        // APIProduct associatedAPIProduct = null;
                        int indexOfDocumentation = resourcePath.indexOf(APIConstants.DOCUMENTATION_KEY);
                        String apiPath = resourcePath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
                        Resource apiResource = registry.get(apiPath);
                        String apiArtifactId = apiResource.getUUID();
                        PublisherAPI pubAPI;
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            String accociatedType;
                            if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.AuditLogConstants.API_PRODUCT)) {
                                // associatedAPIProduct = APIUtil.getAPIProduct(apiArtifact, registry);
                                accociatedType = APIConstants.API_PRODUCT;
                            } else {
                                // associatedAPI = APIUtil.getAPI(apiArtifact, registry);
                                accociatedType = APIConstants.API;
                            }
                            pubAPI = RegistryPersistenceUtil.getAPIForSearch(apiArtifact);
                            docSearch.setApiName(pubAPI.getApiName());
                            docSearch.setApiProvider(pubAPI.getProviderName());
                            docSearch.setApiVersion(pubAPI.getVersion());
                            docSearch.setApiUUID(pubAPI.getId());
                            docSearch.setAssociatedType(accociatedType);
                            docSearch.setDocType(doc.getType());
                            docSearch.setId(doc.getId());
                            docSearch.setSourceType(doc.getSourceType());
                            docSearch.setVisibility(doc.getVisibility());
                            docSearch.setName(doc.getName());
                            contentData.add(docSearch);
                        } else {
                            throw new GovernanceException("artifact id is null of " + apiPath);
                        }
                    } else {
                        String apiArtifactId = resource.getUUID();
                        // API api;
                        // APIProduct apiProduct;
                        String type;
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.API_PRODUCT)) {
                                // apiProduct = APIUtil.getAPIProduct(apiArtifact, registry);
                                // apiProductSet.add(apiProduct);
                                type = APIConstants.API_PRODUCT;
                            } else {
                                // api = APIUtil.getAPI(apiArtifact, registry);
                                // apiSet.add(api);
                                type = APIConstants.API;
                            }
                            PublisherAPI pubAPI = RegistryPersistenceUtil.getAPIForSearch(apiArtifact);
                            PublisherSearchContent content = new PublisherSearchContent();
                            content.setContext(pubAPI.getContext());
                            content.setDescription(pubAPI.getDescription());
                            content.setId(pubAPI.getId());
                            content.setName(pubAPI.getApiName());
                            content.setProvider(RegistryPersistenceUtil.replaceEmailDomainBack(pubAPI.getProviderName()));
                            content.setType(type);
                            content.setVersion(pubAPI.getVersion());
                            content.setStatus(pubAPI.getStatus());
                            content.setAdvertiseOnly(pubAPI.isAdvertiseOnly());
                            contentData.add(content);
                        } else {
                            throw new GovernanceException("artifact id is null for " + resourcePath);
                        }
                    }
                }
            }
            result.setTotalCount(totalLength);
            result.setReturnedCount(contentData.size());
            result.setResults(contentData);
        }
    } catch (RegistryException | IndexerException | DocumentationPersistenceException | APIManagementException e) {
        throw new APIPersistenceException("Error while searching for content ", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
Also used : DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) ArrayList(java.util.ArrayList) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) SearchResultsBean(org.wso2.carbon.registry.indexing.service.SearchResultsBean) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException) DevPortalSearchContent(org.wso2.carbon.apimgt.persistence.dto.DevPortalSearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) SearchContent(org.wso2.carbon.apimgt.persistence.dto.SearchContent) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) ResourceData(org.wso2.carbon.registry.common.ResourceData) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) Documentation(org.wso2.carbon.apimgt.persistence.dto.Documentation) Resource(org.wso2.carbon.registry.core.Resource) 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) ContentBasedSearchService(org.wso2.carbon.registry.indexing.service.ContentBasedSearchService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PublisherContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherContentSearchResult)

Example 20 with PublisherAPI

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPI in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method setSoapToRestSequences.

protected void setSoapToRestSequences(PublisherAPI publisherAPI, Registry registry) throws RegistryException {
    if (publisherAPI.getSoapToRestSequences() != null && !publisherAPI.getSoapToRestSequences().isEmpty()) {
        List<SOAPToRestSequence> sequence = publisherAPI.getSoapToRestSequences();
        for (SOAPToRestSequence soapToRestSequence : sequence) {
            String apiResourceName = soapToRestSequence.getPath();
            if (apiResourceName.startsWith("/")) {
                apiResourceName = apiResourceName.substring(1);
            }
            String resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(publisherAPI.getProviderName()) + RegistryConstants.PATH_SEPARATOR + publisherAPI.getApiName() + RegistryConstants.PATH_SEPARATOR + publisherAPI.getVersion() + RegistryConstants.PATH_SEPARATOR;
            if (soapToRestSequence.getDirection() == Direction.OUT) {
                resourcePath = resourcePath + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR + "out" + RegistryConstants.PATH_SEPARATOR;
            } else {
                resourcePath = resourcePath + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR + "in" + RegistryConstants.PATH_SEPARATOR;
            }
            resourcePath = resourcePath + apiResourceName + "_" + soapToRestSequence.getMethod() + ".xml";
            Resource regResource;
            if (!registry.resourceExists(resourcePath)) {
                regResource = registry.newResource();
                regResource.setContent(soapToRestSequence.getContent());
                regResource.addProperty("method", soapToRestSequence.getMethod());
                if (regResource.getProperty("resourcePath") != null) {
                    regResource.removeProperty("resourcePath");
                }
                regResource.addProperty("resourcePath", apiResourceName);
                regResource.setMediaType("text/xml");
                registry.put(resourcePath, regResource);
            }
        }
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Aggregations

PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)29 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)24 API (org.wso2.carbon.apimgt.api.model.API)23 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)22 Test (org.junit.Test)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 Resource (org.wso2.carbon.registry.core.Resource)20 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)17 QName (javax.xml.namespace.QName)16 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)15 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)15 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)14 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)14 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)13 RealmService (org.wso2.carbon.user.core.service.RealmService)13 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)13 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 Registry (org.wso2.carbon.registry.core.Registry)9 HashSet (java.util.HashSet)8