use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class AbstractAPIManager method getApiSpecificMediationPolicy.
/**
* Returns Mediation policy specify by given identifier
*
* @param identifier API or Product identifier
* @param apiResourcePath registry path to the API resource
* @param mediationPolicyId mediation policy identifier
* @return Mediation object contains details of the mediation policy or null
*/
@Override
public Mediation getApiSpecificMediationPolicy(Identifier identifier, String apiResourcePath, String mediationPolicyId) throws APIManagementException {
// Get registry resource correspond to given policy identifier
Resource mediationResource = getApiSpecificMediationResourceFromUuid(identifier, mediationPolicyId, apiResourcePath);
Mediation mediation = null;
if (mediationResource != null) {
try {
// Get mediation policy config content
String contentString = IOUtils.toString(mediationResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
// Extracting name specified in the mediation config
OMElement omElement = AXIOMUtil.stringToOM(contentString);
OMAttribute attribute = omElement.getAttribute(new QName("name"));
String mediationPolicyName = attribute.getAttributeValue();
mediation = new Mediation();
mediation.setUuid(mediationResource.getUUID());
mediation.setName(mediationPolicyName);
// Extracting mediation policy type from registry path
String resourcePath = mediationResource.getPath();
String[] path = resourcePath.split(RegistryConstants.PATH_SEPARATOR);
String resourceType = path[(path.length - 2)];
mediation.setType(resourceType);
mediation.setConfig(contentString);
} catch (XMLStreamException e) {
String errorMsg = "Error occurred while getting omElement out of mediation content";
throw new APIManagementException(errorMsg, e);
} catch (IOException e) {
String errorMsg = "Error occurred while converting content stream into string ";
throw new APIManagementException(errorMsg, e);
} catch (RegistryException e) {
String errorMsg = "Error occurred while accessing content stream of mediation" + " policy";
throw new APIManagementException(errorMsg, e);
}
}
return mediation;
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class AbstractAPIManager method getCustomMediationResourceFromUuid.
/**
* Returns the mediation policy registry resource correspond to the given identifier
*
* @param mediationPolicyId uuid of the mediation resource
* @return Registry resource of given identifier or null
* @throws APIManagementException If failed to get the registry resource of given uuid
*/
@Override
public Resource getCustomMediationResourceFromUuid(String mediationPolicyId) throws APIManagementException {
String resourcePath = APIConstants.API_CUSTOM_SEQUENCE_LOCATION;
try {
Resource resource = registry.get(resourcePath);
// resource : customsequences
if (resource instanceof Collection) {
Collection typeCollection = (Collection) resource;
String[] typeArray = typeCollection.getChildren();
for (String type : typeArray) {
Resource typeResource = registry.get(type);
// typeResource: in/ out/ fault
if (typeResource instanceof Collection) {
String[] policyArray = ((Collection) typeResource).getChildren();
if (policyArray.length > 0) {
for (String policy : policyArray) {
Resource mediationResource = registry.get(policy);
// mediationResource: eg .log_in_msg.xml
String resourceId = mediationResource.getUUID();
if (resourceId.equals(mediationPolicyId)) {
// registry resource
return mediationResource;
}
}
}
}
}
}
} catch (RegistryException e) {
String msg = "Error while accessing registry objects";
throw new APIManagementException(msg, e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class APIUtil method getMediationPolicyAttributes.
/**
* Returns attributes correspond to the given mediation policy name and direction
*
* @param policyName name of the sequence
* @param tenantId logged in user's tenantId
* @param direction in/out/fault
* @param identifier API identifier
* @return attributes(path, uuid) of the given mediation sequence or null
* @throws APIManagementException If failed to get the uuid of the mediation sequence
*/
public static Map<String, String> getMediationPolicyAttributes(String policyName, int tenantId, String direction, APIIdentifier identifier) throws APIManagementException {
org.wso2.carbon.registry.api.Collection seqCollection = null;
String seqCollectionPath = "";
Map<String, String> mediationPolicyAttributes = new HashMap<>(3);
try {
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN);
} else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT);
} else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
}
if (seqCollection == null) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(getSequencePath(identifier, direction));
}
if (seqCollection != null) {
String[] childPaths = seqCollection.getChildren();
for (String childPath : childPaths) {
Resource mediationPolicy = registry.get(childPath);
OMElement seqElment = APIUtil.buildOMElement(mediationPolicy.getContentStream());
String seqElmentName = seqElment.getAttributeValue(new QName("name"));
if (policyName.equals(seqElmentName)) {
mediationPolicyAttributes.put("path", childPath);
mediationPolicyAttributes.put("uuid", mediationPolicy.getUUID());
mediationPolicyAttributes.put("name", policyName);
return mediationPolicyAttributes;
}
}
}
// If the sequence not found the default sequences, check in custom sequences
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(getSequencePath(identifier, direction));
if (seqCollection != null) {
String[] childPaths = seqCollection.getChildren();
for (String childPath : childPaths) {
Resource mediationPolicy = registry.get(childPath);
OMElement seqElment = APIUtil.buildOMElement(mediationPolicy.getContentStream());
if (policyName.equals(seqElment.getAttributeValue(new QName("name")))) {
mediationPolicyAttributes.put("path", childPath);
mediationPolicyAttributes.put("uuid", mediationPolicy.getUUID());
mediationPolicyAttributes.put("name", policyName);
return mediationPolicyAttributes;
}
}
}
} catch (Exception e) {
String msg = "Issue is in accessing the Registry";
log.error(msg);
throw new APIManagementException(msg, e);
}
return mediationPolicyAttributes;
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method updateMediationPolicy.
@Override
public Mediation updateMediationPolicy(Organization org, String apiId, Mediation mediation) throws MediationPolicyPersistenceException {
boolean isTenantFlowStarted = false;
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 resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + api.apiProvider + RegistryConstants.PATH_SEPARATOR + api.apiName + RegistryConstants.PATH_SEPARATOR + api.apiVersion + RegistryConstants.PATH_SEPARATOR + mediation.getType() + RegistryConstants.PATH_SEPARATOR + mediation.getName();
Resource policy = registry.get(resourcePath);
policy.setContent(mediation.getConfig());
registry.put(resourcePath, policy);
return mediation;
} catch (RegistryException | APIPersistenceException e) {
String msg = "Error while adding the mediation to the registry";
throw new MediationPolicyPersistenceException(msg, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.Mediation 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;
}
Aggregations