use of org.wso2.carbon.apimgt.api.model.Mediation 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.api.model.Mediation 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.api.model.Mediation in project carbon-apimgt by wso2.
the class APIProviderImpl method getDefaultSequence.
/**
* Get the mediation sequence which matches the given type and name from the custom sequences.
*
* @param type : The sequence type.
* @param name : The name of the sequence.
* @return : The mediation sequence which matches the given parameters. Returns null if no matching sequence is
* found.
*/
private Resource getDefaultSequence(String type, String name) throws APIManagementException {
String defaultSequenceFileLocation = "";
try {
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (APIConstants.FAULT_SEQUENCE.equals(type)) {
defaultSequenceFileLocation = APIConstants.API_CUSTOM_FAULTSEQUENCE_LOCATION;
} else if (APIConstants.OUT_SEQUENCE.equals(type)) {
defaultSequenceFileLocation = APIConstants.API_CUSTOM_OUTSEQUENCE_LOCATION;
} else {
defaultSequenceFileLocation = APIConstants.API_CUSTOM_INSEQUENCE_LOCATION;
}
if (registry.resourceExists(defaultSequenceFileLocation)) {
org.wso2.carbon.registry.api.Collection defaultSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(defaultSequenceFileLocation);
if (defaultSeqCollection != null) {
String[] faultSeqChildPaths = defaultSeqCollection.getChildren();
for (String defaultSeqChildPath : faultSeqChildPaths) {
Resource defaultSequence = registry.get(defaultSeqChildPath);
OMElement seqElement = APIUtil.buildOMElement(defaultSequence.getContentStream());
if (name.equals(seqElement.getAttributeValue(new QName("name")))) {
return defaultSequence;
}
}
}
}
} catch (RegistryException e) {
throw new APIManagementException("Error while retrieving registry for tenant " + tenantId, e);
} catch (org.wso2.carbon.registry.api.RegistryException e) {
throw new APIManagementException("Error while processing the " + defaultSequenceFileLocation + " in the registry", e);
} catch (Exception e) {
throw new APIManagementException("Error while building the OMElement from the sequence " + name, e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.Mediation in project carbon-apimgt by wso2.
the class APIProviderImpl method getSequenceFileContent.
/**
* Method to get the user specified mediation sequence.
*
* @param apiIdentifier : The identifier of the api.
* @param type : Mediation type. {in, out, fault}
* @param name : The name of the sequence that needed.
* @return : The content of the mediation sequence.
*/
public String getSequenceFileContent(APIIdentifier apiIdentifier, String type, String name) throws APIManagementException {
Resource requiredSequence;
InputStream sequenceStream;
String sequenceText = "";
try {
if (apiIdentifier != null && type != null && name != null) {
if (log.isDebugEnabled()) {
log.debug("Check the default " + type + "sequences for " + name);
}
requiredSequence = getDefaultSequence(type, name);
if (requiredSequence == null) {
if (log.isDebugEnabled()) {
log.debug("Check the custom " + type + " sequences for " + name);
}
requiredSequence = getCustomSequence(apiIdentifier, type, name);
}
// Convert the content stream to a string.
if (requiredSequence != null) {
sequenceStream = requiredSequence.getContentStream();
StringWriter stringWriter = new StringWriter();
IOUtils.copy(sequenceStream, stringWriter);
sequenceText = stringWriter.toString();
} else {
log.error("No sequence for the name " + name + "is found!");
}
} else {
log.error("Invalid arguments.");
}
} catch (APIManagementException e) {
log.error(e.getMessage());
throw new APIManagementException(e);
} catch (RegistryException e) {
log.error(e.getMessage());
throw new APIManagementException(e);
} catch (IOException e) {
log.error(e.getMessage());
throw new APIManagementException(e);
}
return sequenceText;
}
use of org.wso2.carbon.apimgt.api.model.Mediation 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