Search in sources :

Example 6 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method addApiSpecificMediationPolicy.

@Override
public Mediation addApiSpecificMediationPolicy(String apiId, Mediation mediationPolicy, String organization) throws APIManagementException {
    if (StringUtils.isNotBlank(mediationPolicy.getName()) && mediationPolicy.getName().length() > APIConstants.MAX_LENGTH_MEDIATION_POLICY_NAME) {
        throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.MEDIATION_POLICY_NAME_TOO_LONG, APIConstants.MAX_LENGTH_MEDIATION_POLICY_NAME + ""));
    }
    try {
        org.wso2.carbon.apimgt.persistence.dto.Mediation mappedPolicy = new org.wso2.carbon.apimgt.persistence.dto.Mediation();
        mappedPolicy.setConfig(mediationPolicy.getConfig());
        mappedPolicy.setName(mediationPolicy.getName());
        mappedPolicy.setType(mediationPolicy.getType());
        org.wso2.carbon.apimgt.persistence.dto.Mediation returnedMappedPolicy = apiPersistenceInstance.addMediationPolicy(new Organization(organization), apiId, mappedPolicy);
        if (returnedMappedPolicy != null) {
            mediationPolicy.setUuid(returnedMappedPolicy.getId());
            return mediationPolicy;
        }
    } catch (MediationPolicyPersistenceException e) {
        if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) {
            throw new APIMgtResourceNotFoundException(e);
        } else if (e.getErrorHandler() == ExceptionCodes.MEDIATION_POLICY_API_ALREADY_EXISTS) {
            throw new APIManagementException(ExceptionCodes.MEDIATION_POLICY_API_ALREADY_EXISTS);
        } else {
            throw new APIManagementException("Error while saving mediation policy ", e);
        }
    }
    return null;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 7 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException 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)

Example 8 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method getApiSpecificMediationPolicyByPolicyId.

@Override
public Mediation getApiSpecificMediationPolicyByPolicyId(String apiId, String policyId, String organization) throws APIManagementException {
    try {
        org.wso2.carbon.apimgt.persistence.dto.Mediation policy = apiPersistenceInstance.getMediationPolicy(new Organization(organization), apiId, policyId);
        if (policy != null) {
            Mediation mediation = new Mediation();
            mediation.setName(policy.getName());
            mediation.setUuid(policy.getId());
            mediation.setType(policy.getType());
            mediation.setConfig(policy.getConfig());
            return 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 null;
}
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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 9 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method updateApiSpecificMediationPolicyContent.

@Override
public Mediation updateApiSpecificMediationPolicyContent(String apiId, Mediation mediationPolicy, String organization) throws APIManagementException {
    try {
        org.wso2.carbon.apimgt.persistence.dto.Mediation mappedPolicy = new org.wso2.carbon.apimgt.persistence.dto.Mediation();
        mappedPolicy.setConfig(mediationPolicy.getConfig());
        mappedPolicy.setName(mediationPolicy.getName());
        mappedPolicy.setType(mediationPolicy.getType());
        mappedPolicy.setId(mediationPolicy.getUuid());
        org.wso2.carbon.apimgt.persistence.dto.Mediation returnedMappedPolicy = apiPersistenceInstance.updateMediationPolicy(new Organization(organization), apiId, mappedPolicy);
        if (returnedMappedPolicy != null) {
            return mediationPolicy;
        }
    } catch (MediationPolicyPersistenceException e) {
        if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) {
            throw new APIMgtResourceNotFoundException(e);
        } else {
            throw new APIManagementException("Error while saving mediation policy ", e);
        }
    }
    return null;
}
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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 10 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException 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)

Aggregations

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