Search in sources :

Example 1 with DevPortalAPI

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

the class RegistryPersistenceImpl method searchContentForDevPortal.

@Override
public DevPortalContentSearchResult searchContentForDevPortal(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
    log.debug("Requested query for devportal content search: " + searchQuery);
    Map<String, String> attributes = RegistrySearchUtil.getDevPortalSearchAttributes(searchQuery, ctx, isAllowDisplayAPIsWithMultipleStatus());
    if (log.isDebugEnabled()) {
        log.debug("Search attributes : " + attributes);
    }
    DevPortalContentSearchResult result = null;
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(ctx.getUserame(), org.getName());
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        String tenantAwareUsername = getTenantAwareUsername(ctx.getUserame());
        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 DevPortalContentSearchResult();
            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);
                        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();
                        DevPortalAPI devAPI;
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            devAPI = RegistryPersistenceUtil.getDevPortalAPIForSearch(apiArtifact);
                            docSearch.setApiName(devAPI.getApiName());
                            docSearch.setApiProvider(devAPI.getProviderName());
                            docSearch.setApiVersion(devAPI.getVersion());
                            docSearch.setApiUUID(devAPI.getId());
                            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();
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            DevPortalAPI devAPI = RegistryPersistenceUtil.getDevPortalAPIForSearch(apiArtifact);
                            DevPortalSearchContent content = new DevPortalSearchContent();
                            content.setContext(devAPI.getContext());
                            content.setDescription(devAPI.getDescription());
                            content.setId(devAPI.getId());
                            content.setName(devAPI.getApiName());
                            content.setProvider(RegistryPersistenceUtil.replaceEmailDomainBack(devAPI.getProviderName()));
                            content.setVersion(devAPI.getVersion());
                            content.setStatus(devAPI.getStatus());
                            content.setBusinessOwner(devAPI.getBusinessOwner());
                            content.setBusinessOwnerEmail(devAPI.getBusinessOwnerEmail());
                            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 e) {
        throw new APIPersistenceException("Error while searching for content ", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
Also used : DevPortalSearchContent(org.wso2.carbon.apimgt.persistence.dto.DevPortalSearchContent) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) ArrayList(java.util.ArrayList) DevPortalContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DevPortalContentSearchResult) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) 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) 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)

Example 2 with DevPortalAPI

use of org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI 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 3 with DevPortalAPI

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

the class RegistryPersistenceImplTestCase method testGetDevPortalAPI.

@Test
public void testGetDevPortalAPI() throws Exception {
    Registry registry = Mockito.mock(UserRegistry.class);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.get(anyString())).thenReturn(resource);
    Tag[] tags = new Tag[1];
    Tag tag = new Tag();
    tag.setTagName("testTag");
    tags[0] = tag;
    Mockito.when(registry.getTags(anyString())).thenReturn(tags);
    GenericArtifact artifact = PersistenceHelper.getSampleAPIArtifact();
    String apiUUID = artifact.getId();
    String apiDescription = artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    DevPortalAPI devAPI = apiPersistenceInstance.getDevPortalAPI(org, apiUUID);
    Assert.assertEquals("API UUID does not match", apiUUID, devAPI.getId());
    Assert.assertEquals("API Description does not match", apiDescription, devAPI.getDescription());
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) 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) Tag(org.wso2.carbon.registry.core.Tag) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with DevPortalAPI

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

the class APIConsumerImpl method getLightweightAPI.

/**
 * Get minimal details of API by API identifier
 *
 * @param identifier APIIdentifier object
 * @return API of the provided APIIdentifier
 * @throws APIManagementException
 */
public API getLightweightAPI(APIIdentifier identifier, String orgId) throws APIManagementException {
    String uuid = null;
    try {
        Organization org = new Organization(orgId);
        if (identifier.getUUID() != null) {
            uuid = identifier.getUUID();
        } else {
            uuid = apiMgtDAO.getUUIDFromIdentifier(identifier.getProviderName(), identifier.getApiName(), identifier.getVersion(), orgId);
        }
        DevPortalAPI devPortalApi = apiPersistenceInstance.getDevPortalAPI(org, uuid);
        if (devPortalApi != null) {
            API api = APIMapper.INSTANCE.toApi(devPortalApi);
            api.setOrganization(orgId);
            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 : DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) API(org.wso2.carbon.apimgt.api.model.API) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 5 with DevPortalAPI

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

the class APIConsumerImplTest method testRemoveSubscription.

@Test
public void testRemoveSubscription() throws APIManagementException, WorkflowException, APIPersistenceException {
    String uuid = UUID.randomUUID().toString();
    String apiUUID = UUID.randomUUID().toString();
    Subscriber subscriber = new Subscriber("sub1");
    Application application = new Application("app1", subscriber);
    application.setId(1);
    APIIdentifier identifier = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
    identifier.setUuid(apiUUID);
    SubscribedAPI subscribedAPIOld = new SubscribedAPI(subscriber, identifier);
    subscribedAPIOld.setApplication(application);
    Mockito.when(apiMgtDAO.isAppAllowed(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
    Mockito.when(apiMgtDAO.checkAPIUUIDIsARevisionUUID(Mockito.anyString())).thenReturn(null);
    DevPortalAPI devPortalAPI = Mockito.mock(DevPortalAPI.class);
    Mockito.when(apiPersistenceInstance.getDevPortalAPI(any(Organization.class), any(String.class))).thenReturn(devPortalAPI);
    SubscribedAPI subscribedAPINew = new SubscribedAPI(subscriber, identifier);
    subscribedAPINew.setUUID(uuid);
    subscribedAPINew.setApplication(application);
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO, apiPersistenceInstance);
    apiConsumer.removeSubscription(subscribedAPINew, "org1");
    Mockito.verify(apiMgtDAO, Mockito.times(1)).getApplicationNameFromId(Mockito.anyInt());
    String workflowExtRef = "test_wf_ref";
    String workflowExtRef1 = "complete_wf_ref";
    Mockito.when(apiMgtDAO.getExternalWorkflowReferenceForSubscription((APIIdentifier) Mockito.any(), Mockito.anyInt(), Mockito.anyString())).thenReturn(workflowExtRef, workflowExtRef1);
    SubscriptionWorkflowDTO subscriptionWorkflowDTO = new SubscriptionWorkflowDTO();
    subscriptionWorkflowDTO.setWorkflowReference("1");
    Mockito.when(apiMgtDAO.retrieveWorkflow(workflowExtRef)).thenReturn(subscriptionWorkflowDTO);
    SubscribedAPI subscribedAPI = new SubscribedAPI("api1");
    subscribedAPI.setTier(new Tier("Gold"));
    Mockito.when(apiMgtDAO.getSubscriptionById(Mockito.anyInt())).thenReturn(subscribedAPI);
    Mockito.when(apiMgtDAO.getSubscriptionStatus(uuid, 1)).thenReturn(APIConstants.SubscriptionStatus.ON_HOLD);
    apiConsumer.removeSubscription(subscribedAPINew, "org1");
    Mockito.when(apiMgtDAO.retrieveWorkflow(workflowExtRef1)).thenReturn(subscriptionWorkflowDTO);
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
    apiConsumer.removeSubscription(subscribedAPINew, "org1");
    Mockito.verify(apiMgtDAO, Mockito.times(2)).retrieveWorkflow(Mockito.anyString());
}
Also used : DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Matchers.anyString(org.mockito.Matchers.anyString) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)11 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7 API (org.wso2.carbon.apimgt.api.model.API)6 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)5 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)4 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)4 Registry (org.wso2.carbon.registry.core.Registry)3 Resource (org.wso2.carbon.registry.core.Resource)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 ParseException (org.json.simple.parser.ParseException)2 Matchers.anyString (org.mockito.Matchers.anyString)2 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)2 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)2 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)2 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)2 OASPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException)2