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