Search in sources :

Example 46 with Mediation

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;
}
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 47 with Mediation

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;
}
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 48 with Mediation

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;
}
Also used : QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) NotificationException(org.wso2.carbon.apimgt.impl.notification.exception.NotificationException) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException) MonetizationException(org.wso2.carbon.apimgt.api.MonetizationException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) ParseException(org.json.simple.parser.ParseException) MalformedURLException(java.net.MalformedURLException) OMException(org.apache.axiom.om.OMException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 49 with Mediation

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;
}
Also used : StringWriter(java.io.StringWriter) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 50 with Mediation

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

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)35 Mediation (org.wso2.carbon.apimgt.api.model.Mediation)23 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)22 IOException (java.io.IOException)21 Resource (org.wso2.carbon.registry.core.Resource)19 XMLStreamException (javax.xml.stream.XMLStreamException)12 QName (javax.xml.namespace.QName)11 OMElement (org.apache.axiom.om.OMElement)11 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)11 MediationPolicyPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException)11 InputStream (java.io.InputStream)10 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)10 ArrayList (java.util.ArrayList)9 Collection (org.wso2.carbon.registry.core.Collection)9 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)8 OMAttribute (org.apache.axiom.om.OMAttribute)7 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)6 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)6 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)6 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)6