Search in sources :

Example 91 with Organization

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

the class APIProviderImpl method createAPIProduct.

/**
 * Create an Api Product
 *
 * @param apiProduct API Product
 * @throws APIManagementException if failed to create APIProduct
 */
protected String createAPIProduct(APIProduct apiProduct) throws APIManagementException {
    String apiProductUUID = null;
    // Validate Transports and Security
    validateAndSetTransports(apiProduct);
    validateAndSetAPISecurity(apiProduct);
    PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
    PublisherAPIProduct addedAPIProduct;
    try {
        publisherAPIProduct.setApiProductName(apiProduct.getId().getName());
        publisherAPIProduct.setProviderName(apiProduct.getId().getProviderName());
        publisherAPIProduct.setVersion(apiProduct.getId().getVersion());
        addedAPIProduct = apiPersistenceInstance.addAPIProduct(new Organization(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()), publisherAPIProduct);
        apiProductUUID = addedAPIProduct.getId();
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while creating API product ", e);
    }
    return apiProductUUID;
}
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) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)

Example 92 with Organization

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

the class APIConsumerImpl method removeSubscription.

/**
 * Removes a subscription specified by SubscribedAPI object
 *
 * @param subscription SubscribedAPI object
 * @param organization Organization
 * @throws APIManagementException
 */
@Override
public void removeSubscription(SubscribedAPI subscription, String organization) throws APIManagementException {
    String uuid = subscription.getUUID();
    if (subscription != null) {
        Application application = subscription.getApplication();
        Identifier identifier = subscription.getApiId() != null ? subscription.getApiId() : subscription.getProductId();
        String userId = application.getSubscriber().getName();
        removeSubscription(identifier, userId, application.getId(), organization);
        if (log.isDebugEnabled()) {
            String appName = application.getName();
            String logMessage = "Identifier:  " + identifier.toString() + " subscription (uuid : " + uuid + ") removed from app " + appName;
            log.debug(logMessage);
        }
        // get the workflow state once the executor is executed.
        WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(application.getId()), WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION);
        int tenantId = APIUtil.getTenantId(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        // wfDTO is null when simple wf executor is used because wf state is not stored in the db and is always approved.
        if (wfDTO != null) {
            if (WorkflowStatus.APPROVED.equals(wfDTO.getStatus())) {
                SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), tenantId, tenantDomain, subscription.getSubscriptionId(), subscription.getUUID(), identifier.getId(), identifier.getUUID(), application.getId(), application.getUUID(), identifier.getTier(), subscription.getSubStatus());
                APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
            }
        } else {
            SubscriptionEvent subscriptionEvent = new SubscriptionEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.SUBSCRIPTIONS_DELETE.name(), tenantId, tenantDomain, subscription.getSubscriptionId(), subscription.getUUID(), identifier.getId(), identifier.getUUID(), application.getId(), application.getUUID(), identifier.getTier(), subscription.getSubStatus());
            APIUtil.sendNotification(subscriptionEvent, APIConstants.NotifierType.SUBSCRIPTIONS.name());
        }
    } else {
        throw new APIManagementException(String.format("Subscription for UUID:%s does not exist.", subscription.getUUID()));
    }
}
Also used : SubscriptionEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent) ApplicationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) SubscriptionWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 93 with Organization

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

the class APIProviderImpl method getAPIProductbyUUID.

public APIProduct getAPIProductbyUUID(String uuid, String organization) throws APIManagementException {
    try {
        Organization org = new Organization(organization);
        PublisherAPIProduct publisherAPIProduct = apiPersistenceInstance.getPublisherAPIProduct(org, uuid);
        if (publisherAPIProduct != null) {
            APIProduct product = APIProductMapper.INSTANCE.toApiProduct(publisherAPIProduct);
            product.setID(new APIProductIdentifier(publisherAPIProduct.getProviderName(), publisherAPIProduct.getApiProductName(), publisherAPIProduct.getVersion(), uuid));
            checkAccessControlPermission(userNameWithoutChange, product.getAccessControl(), product.getAccessControlRoles());
            populateRevisionInformation(product, uuid);
            populateAPIProductInformation(uuid, organization, product);
            populateAPIStatus(product);
            populateAPITier(product);
            return product;
        } else {
            String msg = "Failed to get API Product. API Product artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException | OASPersistenceException | ParseException e) {
        String msg = "Failed to get API Product";
        throw new APIManagementException(msg, e);
    }
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) 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) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) ParseException(org.json.simple.parser.ParseException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 94 with Organization

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

the class APIProviderImpl method createNewAPIVersion.

public API createNewAPIVersion(String existingApiId, String newVersion, Boolean isDefaultVersion, String organization) throws APIManagementException {
    API existingAPI = getAPIbyUUID(existingApiId, organization);
    if (existingAPI == null) {
        throw new APIMgtResourceNotFoundException("API not found for id " + existingApiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, existingApiId));
    }
    if (newVersion.equals(existingAPI.getId().getVersion())) {
        throw new APIMgtResourceAlreadyExistsException("Version " + newVersion + " exists for api " + existingAPI.getId().getApiName());
    }
    existingAPI.setOrganization(organization);
    APIIdentifier existingAPIId = existingAPI.getId();
    String existingAPICreatedTime = existingAPI.getCreatedTime();
    String existingAPIStatus = existingAPI.getStatus();
    boolean isExsitingAPIdefaultVersion = existingAPI.isDefaultVersion();
    String existingContext = existingAPI.getContext();
    String existingVersionTimestamp = existingAPI.getVersionTimestamp();
    APIIdentifier newApiId = new APIIdentifier(existingAPI.getId().getProviderName(), existingAPI.getId().getApiName(), newVersion);
    existingAPI.setUuid(null);
    existingAPI.setId(newApiId);
    existingAPI.setStatus(APIConstants.CREATED);
    existingAPI.setDefaultVersion(isDefaultVersion);
    existingAPI.setVersionTimestamp("");
    // We need to change the context by setting the new version
    // This is a change that is coming with the context version strategy
    String existingAPIContextTemplate = existingAPI.getContextTemplate();
    existingAPI.setContext(existingAPIContextTemplate.replace("{version}", newVersion));
    Map<String, List<OperationPolicy>> operationPoliciesMap = extractAndDropOperationPoliciesFromURITemplate(existingAPI.getUriTemplates());
    API newAPI = addAPI(existingAPI);
    String newAPIId = newAPI.getUuid();
    if (!operationPoliciesMap.isEmpty()) {
        // clone common or API specific operation policy.
        Map<String, String> clonedOperationPolicyMap = cloneOperationPoliciesToAPI(existingApiId, newAPI, operationPoliciesMap);
        // attach policy to uri template.
        attachOperationPoliciesToAPI(newAPI, clonedOperationPolicyMap, operationPoliciesMap);
    }
    // copy docs
    List<Documentation> existingDocs = getAllDocumentation(existingApiId, organization);
    if (existingDocs != null) {
        for (Documentation documentation : existingDocs) {
            Documentation newDoc = addDocumentation(newAPIId, documentation, organization);
            DocumentationContent content = getDocumentationContent(existingApiId, documentation.getId(), // TODO see whether we can optimize this
            organization);
            if (content != null) {
                addDocumentationContent(newAPIId, newDoc.getId(), organization, content);
            }
        }
    }
    // copy icon
    ResourceFile icon = getIcon(existingApiId, organization);
    if (icon != null) {
        setThumbnailToAPI(newAPIId, icon, organization);
    }
    // copy sequences
    List<Mediation> mediationPolicies = getAllApiSpecificMediationPolicies(existingApiId, organization);
    if (mediationPolicies != null) {
        for (Mediation mediation : mediationPolicies) {
            Mediation policy = getApiSpecificMediationPolicyByPolicyId(existingApiId, mediation.getUuid(), organization);
            addApiSpecificMediationPolicy(newAPIId, policy, organization);
        }
    }
    // copy wsdl
    if (!APIConstants.API_TYPE_SOAPTOREST.equals(existingAPI.getType()) && existingAPI.getWsdlUrl() != null) {
        ResourceFile wsdl = getWSDL(existingApiId, organization);
        if (wsdl != null) {
            addWSDLResource(newAPIId, wsdl, null, organization);
        }
    }
    // copy graphql definition
    String graphQLSchema = getGraphqlSchemaDefinition(existingApiId, organization);
    if (graphQLSchema != null) {
        saveGraphqlSchemaDefinition(newAPIId, graphQLSchema, organization);
    }
    // update old api
    // revert back to old values before update.
    existingAPI.setUuid(existingApiId);
    existingAPI.setStatus(existingAPIStatus);
    existingAPI.setId(existingAPIId);
    existingAPI.setContext(existingContext);
    existingAPI.setCreatedTime(existingAPICreatedTime);
    // update existing api with the original timestamp
    existingAPI.setVersionTimestamp(existingVersionTimestamp);
    if (isDefaultVersion) {
        existingAPI.setDefaultVersion(false);
    } else {
        existingAPI.setDefaultVersion(isExsitingAPIdefaultVersion);
    }
    try {
        apiPersistenceInstance.updateAPI(new Organization(organization), APIMapper.INSTANCE.toPublisherApi(existingAPI));
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while updating API details", e);
    }
    return getAPIbyUUID(newAPIId, organization);
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) DocumentationContent(org.wso2.carbon.apimgt.api.model.DocumentationContent) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) 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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 95 with Organization

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

the class APIProviderImpl method deleteAPIProduct.

@Override
public void deleteAPIProduct(APIProductIdentifier identifier, String apiProductUUID, String organization) throws APIManagementException {
    if (StringUtils.isEmpty(apiProductUUID)) {
        if (identifier.getUUID() != null) {
            apiProductUUID = identifier.getUUID();
        } else {
            apiProductUUID = apiMgtDAO.getUUIDFromIdentifier(identifier, organization);
        }
    }
    APIProduct apiProduct = getAPIProductbyUUID(apiProductUUID, organization);
    apiProduct.setOrganization(organization);
    deleteAPIProduct(apiProduct);
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)304 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)106 API (org.wso2.carbon.apimgt.api.model.API)100 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)89 ArrayList (java.util.ArrayList)79 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)72 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)65 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)64 IOException (java.io.IOException)61 Registry (org.wso2.carbon.registry.core.Registry)58 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)57 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)56 HashMap (java.util.HashMap)54 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)53 Resource (org.wso2.carbon.registry.core.Resource)51 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)49 JSONObject (org.json.simple.JSONObject)45 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 URISyntaxException (java.net.URISyntaxException)42