Search in sources :

Example 1 with MediationInfo

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

the class RegistryPersistenceImpl method getAllMediationPolicies.

@Override
public List<MediationInfo> getAllMediationPolicies(Organization org, String apiId) throws MediationPolicyPersistenceException {
    boolean isTenantFlowStarted = false;
    List<MediationInfo> mediationList = new ArrayList<MediationInfo>();
    MediationInfo mediation;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        BasicAPI api = getbasicAPIInfo(apiId, registry);
        if (api == null) {
            throw new MediationPolicyPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
        }
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiResourcePath = apiPath.substring(0, prependIndex);
        // apiResourcePath = apiResourcePath.substring(0, apiResourcePath.lastIndexOf("/"));
        // Getting API registry resource
        Resource resource = registry.get(apiResourcePath);
        // resource eg: /_system/governance/apimgt/applicationdata/provider/admin/calculatorAPI/2.0
        if (resource instanceof Collection) {
            Collection typeCollection = (Collection) resource;
            String[] typeArray = typeCollection.getChildren();
            for (String type : typeArray) {
                // Check for mediation policy sequences
                if ((type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN)) || (type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT)) || (type.equalsIgnoreCase(apiResourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT))) {
                    Resource typeResource = registry.get(type);
                    // typeResource : in / out / fault
                    if (typeResource instanceof Collection) {
                        String[] mediationPolicyArr = ((Collection) typeResource).getChildren();
                        if (mediationPolicyArr.length > 0) {
                            for (String mediationPolicy : mediationPolicyArr) {
                                Resource policyResource = registry.get(mediationPolicy);
                                // policyResource eg: custom_in_message
                                // Get uuid of the registry resource
                                String resourceId = policyResource.getUUID();
                                // Get mediation policy config
                                try {
                                    String contentString = IOUtils.toString(policyResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
                                    // Extract name from the policy config
                                    OMElement omElement = AXIOMUtil.stringToOM(contentString);
                                    OMAttribute attribute = omElement.getAttribute(new QName("name"));
                                    String mediationPolicyName = attribute.getAttributeValue();
                                    mediation = new MediationInfo();
                                    mediation.setId(resourceId);
                                    mediation.setName(mediationPolicyName);
                                    // Extracting mediation policy type from the registry resource path
                                    String resourceType = type.substring(type.lastIndexOf("/") + 1);
                                    mediation.setType(resourceType);
                                    mediationList.add(mediation);
                                } catch (XMLStreamException e) {
                                    // If exception been caught flow will continue with next mediation policy
                                    log.error("Error occurred while getting omElement out of" + " mediation content", e);
                                } catch (IOException e) {
                                    log.error("Error occurred while converting the content " + "stream of mediation " + mediationPolicy + " to string", e);
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error occurred  while getting Api Specific mediation policies ";
        throw new MediationPolicyPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return mediationList;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) OMElement(org.apache.axiom.om.OMElement) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) XMLStreamException(javax.xml.stream.XMLStreamException) MediationInfo(org.wso2.carbon.apimgt.persistence.dto.MediationInfo) Collection(org.wso2.carbon.registry.core.Collection) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with MediationInfo

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

the class APIProviderImpl method getAllApiSpecificMediationPolicies.

@Override
public List<Mediation> getAllApiSpecificMediationPolicies(String apiId, String organization) throws APIManagementException {
    List<Mediation> mappedList = new ArrayList<Mediation>();
    try {
        List<MediationInfo> list = apiPersistenceInstance.getAllMediationPolicies(new Organization(organization), apiId);
        if (list != null) {
            for (MediationInfo mediationInfo : list) {
                Mediation mediation = new Mediation();
                mediation.setName(mediationInfo.getName());
                mediation.setUuid(mediationInfo.getId());
                mediation.setType(mediationInfo.getType());
                mappedList.add(mediation);
            }
        }
    } catch (MediationPolicyPersistenceException e) {
        if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) {
            throw new APIMgtResourceNotFoundException(e);
        } else {
            throw new APIManagementException("Error while accessing mediation policies ", e);
        }
    }
    return mappedList;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MediationInfo(org.wso2.carbon.apimgt.persistence.dto.MediationInfo) ArrayList(java.util.ArrayList) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 3 with MediationInfo

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

the class APIProviderImpl method loadMediationPoliciesToAPI.

private void loadMediationPoliciesToAPI(API api, String organization) throws APIManagementException {
    if (APIUtil.isSequenceDefined(api.getInSequence()) || APIUtil.isSequenceDefined(api.getOutSequence()) || APIUtil.isSequenceDefined(api.getFaultSequence())) {
        Organization org = new Organization(organization);
        String apiUUID = api.getUuid();
        // get all policies
        try {
            List<MediationInfo> localPolicies = apiPersistenceInstance.getAllMediationPolicies(org, apiUUID);
            List<Mediation> globalPolicies = null;
            if (APIUtil.isSequenceDefined(api.getInSequence())) {
                boolean found = false;
                for (MediationInfo mediationInfo : localPolicies) {
                    if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN.equals(mediationInfo.getType()) && api.getInSequence().equals(mediationInfo.getName())) {
                        org.wso2.carbon.apimgt.persistence.dto.Mediation mediationPolicy = apiPersistenceInstance.getMediationPolicy(org, apiUUID, mediationInfo.getId());
                        Mediation mediation = new Mediation();
                        mediation.setConfig(mediationPolicy.getConfig());
                        mediation.setName(mediationPolicy.getName());
                        mediation.setUuid(mediationPolicy.getId());
                        mediation.setType(APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN);
                        mediation.setGlobal(false);
                        api.setInSequenceMediation(mediation);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // global policy
                    if (globalPolicies == null) {
                        globalPolicies = getAllGlobalMediationPolicies();
                    }
                    for (Mediation m : globalPolicies) {
                        if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN.equals(m.getType()) && api.getInSequence().equals(m.getName())) {
                            Mediation mediation = getGlobalMediationPolicy(m.getUuid());
                            mediation.setGlobal(true);
                            api.setInSequenceMediation(mediation);
                            found = true;
                            break;
                        }
                    }
                }
            }
            if (APIUtil.isSequenceDefined(api.getOutSequence())) {
                boolean found = false;
                for (MediationInfo mediationInfo : localPolicies) {
                    if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT.equals(mediationInfo.getType()) && api.getOutSequence().equals(mediationInfo.getName())) {
                        org.wso2.carbon.apimgt.persistence.dto.Mediation mediationPolicy = apiPersistenceInstance.getMediationPolicy(org, apiUUID, mediationInfo.getId());
                        Mediation mediation = new Mediation();
                        mediation.setConfig(mediationPolicy.getConfig());
                        mediation.setName(mediationPolicy.getName());
                        mediation.setUuid(mediationPolicy.getId());
                        mediation.setType(APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT);
                        mediation.setGlobal(false);
                        api.setOutSequenceMediation(mediation);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // global policy
                    if (globalPolicies == null) {
                        globalPolicies = getAllGlobalMediationPolicies();
                    }
                    for (Mediation m : globalPolicies) {
                        if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT.equals(m.getType()) && api.getOutSequence().equals(m.getName())) {
                            Mediation mediation = getGlobalMediationPolicy(m.getUuid());
                            mediation.setGlobal(true);
                            api.setOutSequenceMediation(mediation);
                            found = true;
                            break;
                        }
                    }
                }
            }
            if (APIUtil.isSequenceDefined(api.getFaultSequence())) {
                boolean found = false;
                for (MediationInfo mediationInfo : localPolicies) {
                    if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT.equals(mediationInfo.getType()) && api.getFaultSequence().equals(mediationInfo.getName())) {
                        org.wso2.carbon.apimgt.persistence.dto.Mediation mediationPolicy = apiPersistenceInstance.getMediationPolicy(org, apiUUID, mediationInfo.getId());
                        Mediation mediation = new Mediation();
                        mediation.setConfig(mediationPolicy.getConfig());
                        mediation.setName(mediationPolicy.getName());
                        mediation.setUuid(mediationPolicy.getId());
                        mediation.setType(APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
                        mediation.setGlobal(false);
                        api.setFaultSequenceMediation(mediation);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // global policy
                    if (globalPolicies == null) {
                        globalPolicies = getAllGlobalMediationPolicies();
                    }
                    for (Mediation m : globalPolicies) {
                        if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT.equals(m.getType()) && api.getFaultSequence().equals(m.getName())) {
                            Mediation mediation = getGlobalMediationPolicy(m.getUuid());
                            mediation.setGlobal(true);
                            api.setFaultSequenceMediation(mediation);
                            found = true;
                            break;
                        }
                    }
                }
            }
        } catch (MediationPolicyPersistenceException e) {
            throw new APIManagementException("Error while loading medation policies", e);
        }
    }
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MediationInfo(org.wso2.carbon.apimgt.persistence.dto.MediationInfo)

Aggregations

MediationInfo (org.wso2.carbon.apimgt.persistence.dto.MediationInfo)3 MediationPolicyPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException)3 ArrayList (java.util.ArrayList)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 Mediation (org.wso2.carbon.apimgt.api.model.Mediation)2 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)2 IOException (java.io.IOException)1 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)1 Collection (org.wso2.carbon.registry.core.Collection)1 Registry (org.wso2.carbon.registry.core.Registry)1 Resource (org.wso2.carbon.registry.core.Resource)1 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)1 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)1