Search in sources :

Example 1 with LifeCycleTransition

use of org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycleTransition in project carbon-apimgt by wso2.

the class ImportUtils method getLifeCycleAction.

/**
 * This method returns the lifecycle action which can be used to transit from currentStatus to targetStatus.
 *
 * @param tenantDomain  Tenant domain
 * @param currentStatus Current status to do status transition
 * @param targetStatus  Target status to do status transition
 * @return Lifecycle action or null if target is not reachable
 * @throws APIImportExportException If getting lifecycle action failed
 */
public static String getLifeCycleAction(String tenantDomain, String currentStatus, String targetStatus, APIProvider provider) throws APIManagementException {
    // No need to change the lifecycle if both the statuses are same
    if (StringUtils.equalsIgnoreCase(currentStatus, targetStatus)) {
        return null;
    }
    LifeCycle lifeCycle = new LifeCycle();
    // Parse DOM of APILifeCycle
    try {
        String data = provider.getLifecycleConfiguration(tenantDomain);
        DocumentBuilderFactory factory = APIUtil.getSecuredDocumentBuilder();
        DocumentBuilder builder = factory.newDocumentBuilder();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
        Document doc = builder.parse(inputStream);
        Element root = doc.getDocumentElement();
        // Get all nodes with state
        NodeList states = root.getElementsByTagName("state");
        int nStates = states.getLength();
        for (int i = 0; i < nStates; i++) {
            Node node = states.item(i);
            Node id = node.getAttributes().getNamedItem("id");
            if (id != null && !id.getNodeValue().isEmpty()) {
                LifeCycleTransition lifeCycleTransition = new LifeCycleTransition();
                NodeList transitions = node.getChildNodes();
                int nTransitions = transitions.getLength();
                for (int j = 0; j < nTransitions; j++) {
                    Node transition = transitions.item(j);
                    // Add transitions
                    if (ImportExportConstants.NODE_TRANSITION.equals(transition.getNodeName())) {
                        Node target = transition.getAttributes().getNamedItem("target");
                        Node action = transition.getAttributes().getNamedItem("event");
                        if (target != null && action != null) {
                            lifeCycleTransition.addTransition(target.getNodeValue().toLowerCase(), action.getNodeValue());
                        }
                    }
                }
                lifeCycle.addLifeCycleState(id.getNodeValue().toLowerCase(), lifeCycleTransition);
            }
        }
    } catch (ParserConfigurationException | SAXException e) {
        throw new APIManagementException("Error parsing APILifeCycle for tenant: " + tenantDomain, e);
    } catch (UnsupportedEncodingException e) {
        throw new APIManagementException("Error parsing unsupported encoding for APILifeCycle in tenant: " + tenantDomain, e);
    } catch (IOException e) {
        throw new APIManagementException("Error reading APILifeCycle for tenant: " + tenantDomain, e);
    }
    // Retrieve lifecycle action
    LifeCycleTransition transition = lifeCycle.getTransition(currentStatus.toLowerCase());
    if (transition != null) {
        return transition.getAction(targetStatus.toLowerCase());
    }
    return null;
}
Also used : LifeCycle(org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) JsonElement(com.google.gson.JsonElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) LifeCycleTransition(org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycleTransition) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

JsonElement (com.google.gson.JsonElement)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 LifeCycle (org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycle)1 LifeCycleTransition (org.wso2.carbon.apimgt.impl.importexport.lifecycle.LifeCycleTransition)1 SAXException (org.xml.sax.SAXException)1