Search in sources :

Example 96 with APIIdentifier

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

the class APIProviderImpl method getCustomApiFaultSequences.

/**
 * Get the list of Custom Fault Sequences of API.
 *
 * @return List of available fault sequences
 * @throws APIManagementException
 */
public List<String> getCustomApiFaultSequences(APIIdentifier apiIdentifier) throws APIManagementException {
    Set<String> sequenceList = new TreeSet<>();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = null;
        if (apiIdentifier.getProviderName().contains("-AT-")) {
            String provider = apiIdentifier.getProviderName().replace("-AT-", "@");
            tenantDomain = MultitenantUtils.getTenantDomain(provider);
        }
        PrivilegedCarbonContext.startTenantFlow();
        isTenantFlowStarted = true;
        if (!StringUtils.isEmpty(tenantDomain)) {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        } else {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        String customOutSeqFileLocation = APIUtil.getSequencePath(apiIdentifier, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
        if (registry.resourceExists(customOutSeqFileLocation)) {
            org.wso2.carbon.registry.api.Collection faultSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(customOutSeqFileLocation);
            if (faultSeqCollection != null) {
                String[] faultSeqChildPaths = faultSeqCollection.getChildren();
                Arrays.sort(faultSeqChildPaths);
                for (String faultSeqChildPath : faultSeqChildPaths) {
                    Resource faultSequence = registry.get(faultSeqChildPath);
                    try {
                        OMElement seqElment = APIUtil.buildOMElement(faultSequence.getContentStream());
                        sequenceList.add(seqElment.getAttributeValue(new QName("name")));
                    } catch (OMException e) {
                        log.info("Error occurred when reading the sequence '" + faultSeqChildPath + "' from the registry.", e);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Error while retrieving registry for tenant " + tenantId;
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.registry.api.RegistryException e) {
        String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT + " sequences of " + apiIdentifier + " in the registry";
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new APIManagementException(e.getMessage(), e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return new ArrayList<>(sequenceList);
}
Also used : QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) ArrayList(java.util.ArrayList) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) OMException(org.apache.axiom.om.OMException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) OMException(org.apache.axiom.om.OMException)

Example 97 with APIIdentifier

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

the class APIGatewayManager method sendDeploymentEvent.

private void sendDeploymentEvent(API api, String tenantDomain, Set<String> publishedGateways) {
    APIIdentifier apiIdentifier = api.getId();
    DeployAPIInGatewayEvent deployAPIInGatewayEvent = new DeployAPIInGatewayEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.DEPLOY_API_IN_GATEWAY.name(), api.getOrganization(), api.getId().getId(), api.getUuid(), publishedGateways, apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName(), api.getType(), api.getContext());
    APIUtil.sendNotification(deployAPIInGatewayEvent, APIConstants.NotifierType.GATEWAY_PUBLISHED_API.name());
    if (debugEnabled) {
        log.debug("Event sent to Gateway with eventID " + deployAPIInGatewayEvent.getEventId() + " for api " + "with apiID " + api + " at " + deployAPIInGatewayEvent.getTimeStamp());
    }
}
Also used : APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) DeployAPIInGatewayEvent(org.wso2.carbon.apimgt.impl.notifier.events.DeployAPIInGatewayEvent)

Example 98 with APIIdentifier

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

the class APIGatewayManager method transformAPIToAPIEvent.

private Set<APIEvent> transformAPIToAPIEvent(Set<API> apiSet) {
    Set<APIEvent> apiEvents = new HashSet<>();
    for (API api : apiSet) {
        APIIdentifier id = api.getId();
        APIEvent apiEvent = new APIEvent(id.getUUID(), id.getApiName(), id.getVersion(), id.getProviderName(), api.getType(), api.getStatus());
        apiEvents.add(apiEvent);
    }
    return apiEvents;
}
Also used : APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) HashSet(java.util.HashSet)

Example 99 with APIIdentifier

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

the class APIProviderImpl method updateAPIResources.

/**
 * Update resources of the API including local scopes and resource to scope attachments.
 *
 * @param api      API
 * @param tenantId Tenant Id
 * @throws APIManagementException If fails to update local scopes of the API.
 */
private void updateAPIResources(API api, int tenantId) throws APIManagementException {
    String tenantDomain = APIUtil.getTenantDomainFromTenantId(tenantId);
    APIIdentifier apiIdentifier = api.getId();
    // Get the new URI templates for the API
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    // Get the existing local scope keys attached for the API
    Set<String> oldLocalScopeKeys = apiMgtDAO.getAllLocalScopeKeysForAPI(api.getUuid(), tenantId);
    // Get the existing URI templates for the API
    Set<URITemplate> oldURITemplates = apiMgtDAO.getURITemplatesOfAPI(api.getUuid());
    // Get the new local scope keys from URI templates
    Set<Scope> newLocalScopes = getScopesToRegisterFromURITemplates(api.getId().getApiName(), api.getOrganization(), uriTemplates);
    Set<String> newLocalScopeKeys = newLocalScopes.stream().map(Scope::getKey).collect(Collectors.toSet());
    // Get the existing versioned local scope keys attached for the API
    Set<String> oldVersionedLocalScopeKeys = apiMgtDAO.getVersionedLocalScopeKeysForAPI(api.getUuid(), tenantId);
    // Get the existing versioned local scope keys which needs to be removed (not updated) from the current updating
    // API and remove them from the oldLocalScopeKeys set before sending to KM, so that they will not be removed
    // from KM and can be still used by other versioned APIs.
    Iterator oldLocalScopesItr = oldLocalScopeKeys.iterator();
    while (oldLocalScopesItr.hasNext()) {
        String oldLocalScopeKey = (String) oldLocalScopesItr.next();
        // if the scope is used in versioned APIs and it is not in new local scope key set
        if (oldVersionedLocalScopeKeys.contains(oldLocalScopeKey) && !newLocalScopeKeys.contains(oldLocalScopeKey)) {
            // remove from old local scope key set which will be send to KM
            oldLocalScopesItr.remove();
        }
    }
    apiMgtDAO.updateURITemplates(api, tenantId);
    if (log.isDebugEnabled()) {
        log.debug("Successfully updated the URI templates of API: " + apiIdentifier + " in the database");
    }
    // Update the resource scopes of the API in KM.
    // Need to remove the old local scopes and register new local scopes and, update the resource scope mappings
    // using the updated URI templates of the API.
    deleteScopes(oldLocalScopeKeys, tenantId);
    addScopes(newLocalScopes, tenantId);
    Map<String, KeyManagerDto> tenantKeyManagers = KeyManagerHolder.getTenantKeyManagers(tenantDomain);
    for (Map.Entry<String, KeyManagerDto> keyManagerDtoEntry : tenantKeyManagers.entrySet()) {
        KeyManager keyManager = keyManagerDtoEntry.getValue().getKeyManager();
        if (keyManager != null) {
            try {
                keyManager.updateResourceScopes(api, oldLocalScopeKeys, newLocalScopes, oldURITemplates, uriTemplates);
                if (log.isDebugEnabled()) {
                    log.debug("Successfully updated the resource scopes of API: " + apiIdentifier + " in Key Manager " + keyManagerDtoEntry.getKey() + " .");
                }
            } catch (APIManagementException e) {
                log.error("Error while updating resource to scope attachment in Key Manager " + keyManagerDtoEntry.getKey(), e);
            }
        }
    }
}
Also used : URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Iterator(java.util.Iterator) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 100 with APIIdentifier

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

the class APIProviderImpl method getCustomApiInSequences.

/**
 * Get the list of Custom in sequences of API.
 *
 * @return List of in sequences
 * @throws APIManagementException
 */
public List<String> getCustomApiInSequences(APIIdentifier apiIdentifier) throws APIManagementException {
    Set<String> sequenceList = new TreeSet<>();
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = null;
        if (apiIdentifier.getProviderName().contains("-AT-")) {
            String provider = apiIdentifier.getProviderName().replace("-AT-", "@");
            tenantDomain = MultitenantUtils.getTenantDomain(provider);
        }
        PrivilegedCarbonContext.startTenantFlow();
        isTenantFlowStarted = true;
        if (!StringUtils.isEmpty(tenantDomain)) {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
        } else {
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
        }
        UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
        String customInSeqFileLocation = APIUtil.getSequencePath(apiIdentifier, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN);
        if (registry.resourceExists(customInSeqFileLocation)) {
            org.wso2.carbon.registry.api.Collection inSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(customInSeqFileLocation);
            if (inSeqCollection != null) {
                String[] inSeqChildPaths = inSeqCollection.getChildren();
                Arrays.sort(inSeqChildPaths);
                for (String inSeqChildPath : inSeqChildPaths) {
                    Resource outSequence = registry.get(inSeqChildPath);
                    try {
                        OMElement seqElment = APIUtil.buildOMElement(outSequence.getContentStream());
                        sequenceList.add(seqElment.getAttributeValue(new QName("name")));
                    } catch (OMException e) {
                        log.info("Error occurred when reading the sequence '" + inSeqChildPath + "' from the registry.", e);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Error while retrieving registry for tenant " + tenantId;
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.registry.api.RegistryException e) {
        String msg = "Error while processing the " + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN + " sequences of " + apiIdentifier + " in the registry";
        log.error(msg);
        throw new APIManagementException(msg, e);
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new APIManagementException(e.getMessage(), e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return new ArrayList<>(sequenceList);
}
Also used : QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) ArrayList(java.util.ArrayList) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) OMException(org.apache.axiom.om.OMException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) OMException(org.apache.axiom.om.OMException)

Aggregations

APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)305 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)171 API (org.wso2.carbon.apimgt.api.model.API)160 Test (org.junit.Test)155 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)119 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)105 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)85 Resource (org.wso2.carbon.registry.core.Resource)79 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)69 ArrayList (java.util.ArrayList)68 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)56 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)48 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)46 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)46 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)44 HashSet (java.util.HashSet)41 HashMap (java.util.HashMap)40 IOException (java.io.IOException)37 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)37 JSONObject (org.json.simple.JSONObject)36