Search in sources :

Example 21 with APIProduct

use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.

the class APIProviderImpl method updateAPIProductForStateChange.

/**
 * Update API Product in registry for lifecycle state change
 *
 * @param apiProduct    API Product Object
 * @param currentStatus Current state of API Product
 * @param newStatus     New state of API Product
 * @return boolean indicates success or failure
 * @throws APIManagementException if there is an error when updating API Product for lifecycle state
 * @throws FaultGatewaysException if there is an error when updating API Product for lifecycle state
 */
public void updateAPIProductForStateChange(APIProduct apiProduct, String currentStatus, String newStatus) throws APIManagementException, FaultGatewaysException {
    String provider = apiProduct.getId().getProviderName();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(provider));
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        }
        if (!currentStatus.equals(newStatus)) {
            apiProduct.setState(newStatus);
            // If API status changed to publish we should add it to recently added APIs list
            // this should happen in store-publisher cluster domain if deployment is distributed
            // IF new API published we will add it to recently added APIs
            Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.RECENTLY_ADDED_API_CACHE_NAME).removeAll();
            if (APIConstants.RETIRED.equals(newStatus)) {
                cleanUpPendingSubscriptionCreationProcessesByAPI(apiProduct.getUuid());
                apiMgtDAO.removeAllSubscriptions(apiProduct.getUuid());
                deleteAPIProductRevisions(apiProduct.getUuid(), tenantDomain);
            }
            PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
            try {
                apiPersistenceInstance.updateAPIProduct(new Organization(apiProduct.getOrganization()), publisherAPIProduct);
            } catch (APIPersistenceException e) {
                handleException("Error while persisting the updated API Product", e);
            }
        }
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)

Example 22 with APIProduct

use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.

the class APIProviderImpl method deleteAPIProduct.

public void deleteAPIProduct(APIProduct apiProduct) throws APIManagementException {
    APIProductIdentifier identifier = apiProduct.getId();
    try {
        // int apiId = apiMgtDAO.getAPIID(identifier, null);
        long subsCount = apiMgtDAO.getAPISubscriptionCountByAPI(identifier);
        if (subsCount > 0) {
            // Logging as a WARN since this isn't an error scenario.
            String message = "Cannot remove the API Product as active subscriptions exist.";
            log.warn(message);
            throw new APIManagementException(message);
        }
        // gatewayType check is required when API Management is deployed on
        // other servers to avoid synapse
        deleteAPIProductRevisions(apiProduct.getUuid(), apiProduct.getOrganization());
        apiPersistenceInstance.deleteAPIProduct(new Organization(apiProduct.getOrganization()), apiProduct.getUuid());
        apiMgtDAO.deleteAPIProduct(identifier);
        cleanUpPendingAPIStateChangeTask(apiProduct.getProductId(), true);
        if (log.isDebugEnabled()) {
            String logMessage = "API Product Name: " + identifier.getName() + ", API Product Version " + identifier.getVersion() + " successfully removed from the database.";
            log.debug(logMessage);
        }
        JSONObject apiLogObject = new JSONObject();
        apiLogObject.put(APIConstants.AuditLogConstants.NAME, identifier.getName());
        apiLogObject.put(APIConstants.AuditLogConstants.VERSION, identifier.getVersion());
        apiLogObject.put(APIConstants.AuditLogConstants.PROVIDER, identifier.getProviderName());
        APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API_PRODUCT, apiLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
        GatewayArtifactsMgtDAO.getInstance().deleteGatewayArtifacts(apiProduct.getUuid());
    } catch (APIPersistenceException e) {
        handleException("Failed to remove the API product", e);
    } catch (WorkflowException e) {
        handleException("Error while removing the pending workflows of API Product", e);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JSONObject(org.json.simple.JSONObject) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException)

Example 23 with APIProduct

use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.

the class APIProviderImpl method addAPIProductSwagger.

@Override
public void addAPIProductSwagger(String productId, Map<API, List<APIProductResource>> apiToProductResourceMapping, APIProduct apiProduct, String orgId) throws APIManagementException {
    APIDefinition parser = new OAS3Parser();
    SwaggerData swaggerData = new SwaggerData(apiProduct);
    String apiProductSwagger = parser.generateAPIDefinition(swaggerData);
    apiProductSwagger = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, apiProductSwagger);
    saveSwaggerDefinition(productId, apiProductSwagger, orgId);
    apiProduct.setDefinition(apiProductSwagger);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) OAS3Parser(org.wso2.carbon.apimgt.impl.definitions.OAS3Parser)

Example 24 with APIProduct

use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.

the class APIProviderImpl method searchPaginatedContent.

@Override
public Map<String, Object> searchPaginatedContent(String searchQuery, String organization, int start, int end) throws APIManagementException {
    ArrayList<Object> compoundResult = new ArrayList<Object>();
    Map<Documentation, API> docMap = new HashMap<Documentation, API>();
    Map<Documentation, APIProduct> productDocMap = new HashMap<Documentation, APIProduct>();
    Map<String, Object> result = new HashMap<String, Object>();
    SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
    SortedSet<APIProduct> apiProductSet = new TreeSet<APIProduct>(new APIProductNameComparator());
    String userame = userNameWithoutChange;
    Organization org = new Organization(organization);
    Map<String, Object> properties = APIUtil.getUserProperties(userame);
    String[] roles = APIUtil.getFilteredUserRoles(userame);
    UserContext ctx = new UserContext(userame, org, properties, roles);
    try {
        PublisherContentSearchResult results = apiPersistenceInstance.searchContentForPublisher(org, searchQuery, start, end, ctx);
        if (results != null) {
            List<SearchContent> resultList = results.getResults();
            for (SearchContent item : resultList) {
                if ("API".equals(item.getType())) {
                    PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
                    API api = new API(new APIIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
                    api.setUuid(publiserAPI.getId());
                    api.setContext(publiserAPI.getContext());
                    api.setContextTemplate(publiserAPI.getContext());
                    api.setStatus(publiserAPI.getStatus());
                    apiSet.add(api);
                } else if ("APIProduct".equals(item.getType())) {
                    PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
                    APIProduct api = new APIProduct(new APIProductIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
                    api.setUuid(publiserAPI.getId());
                    api.setContextTemplate(publiserAPI.getContext());
                    api.setState(publiserAPI.getStatus());
                    apiProductSet.add(api);
                } else if (item instanceof DocumentSearchContent) {
                    // doc item
                    DocumentSearchContent docItem = (DocumentSearchContent) item;
                    Documentation doc = new Documentation(DocumentationType.valueOf(docItem.getDocType().toString()), docItem.getName());
                    doc.setSourceType(DocumentSourceType.valueOf(docItem.getSourceType().toString()));
                    doc.setVisibility(DocumentVisibility.valueOf(docItem.getVisibility().toString()));
                    doc.setId(docItem.getId());
                    if ("API".equals(docItem.getAssociatedType())) {
                        API api = new API(new APIIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
                        api.setUuid(docItem.getApiUUID());
                        docMap.put(doc, api);
                    } else if ("APIProduct".equals(docItem.getAssociatedType())) {
                        APIProduct api = new APIProduct(new APIProductIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
                        api.setUuid(docItem.getApiUUID());
                        productDocMap.put(doc, api);
                    }
                }
            }
            compoundResult.addAll(apiSet);
            compoundResult.addAll(apiProductSet);
            compoundResult.addAll(docMap.entrySet());
            compoundResult.addAll(productDocMap.entrySet());
            compoundResult.sort(new ContentSearchResultNameComparator());
            result.put("length", results.getTotalCount());
        } else {
            result.put("length", compoundResult.size());
        }
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while searching content ", e);
    }
    result.put("apis", compoundResult);
    return result;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SearchContent(org.wso2.carbon.apimgt.persistence.dto.SearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) PublisherContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherContentSearchResult) JSONObject(org.json.simple.JSONObject) 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) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator) APIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIProductNameComparator)

Example 25 with APIProduct

use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.

the class APIProviderImpl method updateAPIProductSwagger.

@Override
public void updateAPIProductSwagger(String productId, Map<API, List<APIProductResource>> apiToProductResourceMapping, APIProduct apiProduct, String orgId) throws APIManagementException {
    APIDefinition parser = new OAS3Parser();
    SwaggerData updatedData = new SwaggerData(apiProduct);
    String existingProductSwagger = getAPIDefinitionOfAPIProduct(apiProduct);
    String updatedProductSwagger = parser.generateAPIDefinition(updatedData, existingProductSwagger);
    updatedProductSwagger = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, updatedProductSwagger);
    saveSwaggerDefinition(productId, updatedProductSwagger, orgId);
    apiProduct.setDefinition(updatedProductSwagger);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) OAS3Parser(org.wso2.carbon.apimgt.impl.definitions.OAS3Parser)

Aggregations

APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)71 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)52 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)51 API (org.wso2.carbon.apimgt.api.model.API)37 ArrayList (java.util.ArrayList)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)22 PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)22 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)21 Tier (org.wso2.carbon.apimgt.api.model.Tier)21 HashMap (java.util.HashMap)19 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)19 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)18 JSONObject (org.json.simple.JSONObject)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)16 HashSet (java.util.HashSet)15 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)15 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)14 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)14 ParseException (org.json.simple.parser.ParseException)12