Search in sources :

Example 1 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException 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();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 2 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException 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;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) OMElement(org.apache.axiom.om.OMElement) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) XMLStreamException(javax.xml.stream.XMLStreamException) MediationInfo(org.wso2.carbon.apimgt.persistence.dto.MediationInfo) Collection(org.wso2.carbon.registry.core.Collection) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 3 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method addMediationPolicy.

@Override
public Mediation addMediationPolicy(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();
        if (registry.resourceExists(resourcePath)) {
            throw new MediationPolicyPersistenceException("Mediation policy already exists for the given name " + mediation.getName(), ExceptionCodes.MEDIATION_POLICY_API_ALREADY_EXISTS);
        }
        Resource policy = registry.newResource();
        policy.setContent(mediation.getConfig());
        policy.setMediaType("application/xml");
        registry.put(resourcePath, policy);
        mediation.setId(policy.getUUID());
        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();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 4 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getMediationPolicy.

@Override
public Mediation getMediationPolicy(Organization org, String apiId, String mediationPolicyId) throws MediationPolicyPersistenceException {
    boolean isTenantFlowStarted = false;
    Mediation mediation = null;
    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);
        String policyPath = GovernanceUtils.getArtifactPath(registry, mediationPolicyId);
        if (!policyPath.startsWith(apiResourcePath)) {
            throw new MediationPolicyPersistenceException("Policy not foud ", ExceptionCodes.POLICY_NOT_FOUND);
        }
        Resource mediationResource = registry.get(policyPath);
        if (mediationResource != null) {
            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();
            String[] path = policyPath.split(RegistryConstants.PATH_SEPARATOR);
            String resourceType = path[(path.length - 2)];
            mediation = new Mediation();
            mediation.setConfig(contentString);
            mediation.setType(resourceType);
            mediation.setId(mediationResource.getUUID());
            mediation.setName(mediationPolicyName);
        }
    } catch (RegistryException | APIPersistenceException | IOException | XMLStreamException e) {
        String msg = "Error occurred  while getting Api Specific mediation policies ";
        throw new MediationPolicyPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return mediation;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) OMElement(org.apache.axiom.om.OMElement) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) IOException(java.io.IOException) Mediation(org.wso2.carbon.apimgt.persistence.dto.Mediation) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) XMLStreamException(javax.xml.stream.XMLStreamException) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 5 with MediationPolicyPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method deleteMediationPolicy.

@Override
public void deleteMediationPolicy(Organization org, String apiId, String mediationPolicyId) 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 apiResourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + api.apiProvider + RegistryConstants.PATH_SEPARATOR + api.apiName + RegistryConstants.PATH_SEPARATOR + api.apiVersion;
        String policyPath = GovernanceUtils.getArtifactPath(registry, mediationPolicyId);
        if (!policyPath.startsWith(apiResourcePath)) {
            throw new MediationPolicyPersistenceException("Policy not foud ", ExceptionCodes.POLICY_NOT_FOUND);
        }
        if (registry.resourceExists(policyPath)) {
            registry.delete(policyPath);
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error occurred  while getting Api Specific mediation policies ";
        throw new MediationPolicyPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

MediationPolicyPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 Mediation (org.wso2.carbon.apimgt.api.model.Mediation)5 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)5 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)5 Registry (org.wso2.carbon.registry.core.Registry)5 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)5 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)5 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)4 Resource (org.wso2.carbon.registry.core.Resource)4 MediationInfo (org.wso2.carbon.apimgt.persistence.dto.MediationInfo)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 OMAttribute (org.apache.axiom.om.OMAttribute)2 OMElement (org.apache.axiom.om.OMElement)2 Mediation (org.wso2.carbon.apimgt.persistence.dto.Mediation)1 Collection (org.wso2.carbon.registry.core.Collection)1