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;
}
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);
}
}
}
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;
}
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;
}
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;
}
Aggregations