Search in sources :

Example 6 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project carbon-business-process by wso2.

the class XMLDocument method set.

/**
 * Function to set/replace/update an object (String / Element) to matching the xPath provided. (In case new element
 * is added, this api will clone it and merge the new node to the target location pointed by xPath and return the new cloned node)
 *
 * @param xPathStr xPath to the location object need to set
 * @param obj      String or Node
 * @return returns the node get updated when the set object is String, or returns newly added Node in case object is Element
 * @throws XPathExpressionException If expression cannot be evaluated
 * @throws BPMNXmlException         is thrown due to : Provided XPath and object does not match, provided object is not a Node or String
 *                                  result is NodeList, not a Text node or Element
 */
public Node set(String xPathStr, Object obj) throws XPathExpressionException, BPMNXmlException {
    NodeList evalResult = (NodeList) Utils.evaluateXPath(this.doc, xPathStr, XPathConstants.NODESET);
    if (evalResult.getLength() == 1) {
        Node targetNode = evalResult.item(0);
        if (obj instanceof String && targetNode instanceof Text) {
            // if string is provided, assume that user
            // need to replace the node value
            targetNode.setNodeValue((String) obj);
            // return updated Text Node
            return targetNode;
        } else if ((obj instanceof Integer || obj instanceof Byte || obj instanceof Character || obj instanceof Short || obj instanceof Long || obj instanceof Float || obj instanceof Double || obj instanceof Boolean) && targetNode instanceof Text) {
            // need to replace the node value
            targetNode.setNodeValue(obj.toString());
            // return updated Text Node
            return targetNode;
        } else if (obj instanceof Element && targetNode instanceof Element && targetNode.getParentNode() != null) {
            // if the user provides Node object,
            // assume that need to replace the target node
            Node targetParent = targetNode.getParentNode();
            Node nextSibling = targetNode.getNextSibling();
            // remove the target node
            targetParent.removeChild(targetNode);
            // add new node
            Node newNode = doc.importNode((Node) obj, true);
            if (nextSibling != null) {
                // If next sibling exists we have to put the new node before it
                targetParent.insertBefore(newNode, nextSibling);
            } else {
                targetParent.appendChild(newNode);
            }
            // return new node
            return newNode;
        } else {
            // provided XPath and object to set does not match
            throw new BPMNXmlException("Provided XPath and provided object does not match");
        }
    } else if (evalResult.getLength() > 0) {
        throw new BPMNXmlException("Error in provided xPath. Evaluation result is NodeList, not a Text node or Element");
    } else {
        throw new BPMNXmlException("Error in provided xPath. Evaluation result is not a Text node or Element");
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) BPMNXmlException(org.wso2.carbon.bpmn.core.types.datatypes.xml.BPMNXmlException)

Example 7 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project carbon-business-process by wso2.

the class LiteralBasedOrgEntityProvider method getOrganizationalEntities.

public List<OrganizationalEntityDAO> getOrganizationalEntities(PeopleQueryEvaluator peopleQueryEvaluator, TFrom tFrom, EvaluationContext evaluationContext) {
    TLiteral literal = tFrom.getLiteral();
    List<OrganizationalEntityDAO> orgEntityList = new ArrayList<OrganizationalEntityDAO>();
    Element domNode = (Element) literal.getDomNode();
    if (domNode != null) {
        NodeList orgEntityNodes = domNode.getElementsByTagNameNS(HumanTaskConstants.organizationalEntityQname.getNamespaceURI(), HumanTaskConstants.organizationalEntityQname.getLocalPart());
        // There should be only one organizational Entity
        if (orgEntityNodes.getLength() == 1) {
            Node orgEntityNode = orgEntityNodes.item(0);
            addOrgEntitiesForOrganizationEntityNode(orgEntityNode, peopleQueryEvaluator, orgEntityList);
        } else {
            NodeList elements = domNode.getElementsByTagNameNS(HumanTaskConstants.userQname.getNamespaceURI(), HumanTaskConstants.userQname.getLocalPart());
            if (elements.getLength() == 1) {
                // There should only be one user element
                CommonTaskUtil.addOrgEntityForUserNode(elements.item(0), peopleQueryEvaluator, orgEntityList);
            }
        }
    }
    return orgEntityList;
}
Also used : TLiteral(org.wso2.carbon.humantask.TLiteral) OrganizationalEntityDAO(org.wso2.carbon.humantask.core.dao.OrganizationalEntityDAO) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList)

Example 8 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project product-mi-tooling by wso2.

the class LogConfigDelegate method addLogger.

public Ack addLogger(String groupId, LogConfigAddRequest request) throws ManagementApiException {
    logger.debug("Adding new Logger " + request.getName() + " for all nodes in group " + groupId);
    Ack ack = new Ack(Constants.FAIL_STATUS);
    JsonObject payload = createAddLoggerPayload(request);
    NodeList nodeList = databaseManager.fetchNodes(groupId);
    for (NodeListInner node : nodeList) {
        String nodeId = node.getNodeId();
        String mgtApiUrl = ManagementApiUtils.getMgtApiUrl(groupId, nodeId);
        String accessToken = databaseManager.getAccessToken(groupId, nodeId);
        String addLoggerUrl = mgtApiUrl.concat("logging");
        logger.debug("Adding new logger on node " + nodeId);
        CloseableHttpResponse httpResponse = Utils.doPatch(groupId, nodeId, accessToken, addLoggerUrl, payload);
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            logger.error("Error occurred while adding logger on node " + nodeId + " in group " + groupId);
            String message = HttpUtils.getJsonResponse(httpResponse).get("Error").getAsString();
            ack.setMessage(message);
            return ack;
        }
    }
    ack.setStatus(Constants.SUCCESS_STATUS);
    return ack;
}
Also used : NodeListInner(org.wso2.ei.dashboard.core.rest.model.NodeListInner) NodeList(org.wso2.ei.dashboard.core.rest.model.NodeList) Ack(org.wso2.ei.dashboard.core.rest.model.Ack) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject)

Example 9 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project product-mi-tooling by wso2.

the class LogConfigDelegate method updateLogLevel.

public Ack updateLogLevel(String groupId, LogConfigUpdateRequest request) throws ManagementApiException {
    logger.debug("Updating logger " + request.getName() + " for all nodes in group " + groupId);
    Ack ack = new Ack(Constants.FAIL_STATUS);
    JsonObject payload = createUpdateLoggerPayload(request);
    NodeList nodeList = databaseManager.fetchNodes(groupId);
    for (NodeListInner node : nodeList) {
        String nodeId = node.getNodeId();
        CloseableHttpResponse httpResponse = updateLogLevelByNodeId(groupId, nodeId, payload);
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            logger.error("Error occurred while updating logger on node " + nodeId + " in group " + groupId);
            String message = HttpUtils.getJsonResponse(httpResponse).get("Error").getAsString();
            ack.setMessage(message);
            return ack;
        }
    }
    ack.setStatus(Constants.SUCCESS_STATUS);
    return ack;
}
Also used : NodeListInner(org.wso2.ei.dashboard.core.rest.model.NodeListInner) NodeList(org.wso2.ei.dashboard.core.rest.model.NodeList) Ack(org.wso2.ei.dashboard.core.rest.model.Ack) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) JsonObject(com.google.gson.JsonObject)

Example 10 with NodeList

use of org.wso2.ei.dashboard.core.rest.model.NodeList in project product-mi-tooling by wso2.

the class UsersDelegate method addUser.

public Ack addUser(String groupId, AddUserRequest request) throws ManagementApiException {
    log.debug("Adding user " + request.getUserId() + " in group " + groupId);
    Ack ack = new Ack(Constants.FAIL_STATUS);
    JsonObject payload = createAddUserPayload(request);
    NodeList nodeList = databaseManager.fetchNodes(groupId);
    // assumption - In a group, all nodes use a shared user-store
    String nodeId = nodeList.get(0).getNodeId();
    String mgtApiUrl = ManagementApiUtils.getMgtApiUrl(groupId, nodeId);
    String accessToken = databaseManager.getAccessToken(groupId, nodeId);
    String url = mgtApiUrl.concat("users");
    Utils.doPost(groupId, nodeId, accessToken, url, payload);
    ack.setStatus(Constants.SUCCESS_STATUS);
    return ack;
}
Also used : NodeList(org.wso2.ei.dashboard.core.rest.model.NodeList) Ack(org.wso2.ei.dashboard.core.rest.model.Ack) JsonObject(com.google.gson.JsonObject)

Aggregations

NodeList (org.w3c.dom.NodeList)20 Element (org.w3c.dom.Element)12 Node (org.w3c.dom.Node)11 NodeList (org.wso2.ei.dashboard.core.rest.model.NodeList)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 List (java.util.List)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 JsonObject (com.google.gson.JsonObject)4 QName (javax.xml.namespace.QName)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 Document (org.w3c.dom.Document)4 NodeListInner (org.wso2.ei.dashboard.core.rest.model.NodeListInner)4 SAXException (org.xml.sax.SAXException)4 JsonElement (com.google.gson.JsonElement)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3