Search in sources :

Example 41 with XPathExpression

use of javax.xml.xpath.XPathExpression in project cloudstack by apache.

the class PaloAltoResource method requestWithCommit.

/* Runs a sequence of commands and attempts to commit at the end. */
/* Uses the Command pattern to enable overriding of the response handling if needed. */
private synchronized boolean requestWithCommit(ArrayList<IPaloAltoCommand> commandList) throws ExecutionException {
    boolean result = true;
    if (commandList.size() > 0) {
        // CHECK IF THERE IS PENDING CHANGES THAT HAVE NOT BEEN COMMITTED...
        String pending_changes;
        Map<String, String> check_params = new HashMap<String, String>();
        check_params.put("type", "op");
        check_params.put("cmd", "<check><pending-changes></pending-changes></check>");
        String check_response = request(PaloAltoMethod.GET, check_params);
        Document check_doc = getDocument(check_response);
        XPath check_xpath = XPathFactory.newInstance().newXPath();
        try {
            XPathExpression expr = check_xpath.compile("/response[@status='success']/result/text()");
            pending_changes = (String) expr.evaluate(check_doc, XPathConstants.STRING);
        } catch (XPathExpressionException e) {
            throw new ExecutionException(e.getCause().getMessage());
        }
        if (pending_changes.equals("yes")) {
            throw new ExecutionException("The Palo Alto has uncommited changes, so no changes can be made.  Try again later or contact your administrator.");
        } else {
            // ADD A CONFIG LOCK TO CAPTURE THE PALO ALTO RESOURCE
            String add_lock_status;
            Map<String, String> add_lock_params = new HashMap<String, String>();
            add_lock_params.put("type", "op");
            add_lock_params.put("cmd", "<request><config-lock><add></add></config-lock></request>");
            String add_lock_response = request(PaloAltoMethod.GET, add_lock_params);
            Document add_lock_doc = getDocument(add_lock_response);
            XPath add_lock_xpath = XPathFactory.newInstance().newXPath();
            try {
                XPathExpression expr = add_lock_xpath.compile("/response[@status='success']/result/text()");
                add_lock_status = (String) expr.evaluate(add_lock_doc, XPathConstants.STRING);
            } catch (XPathExpressionException e) {
                throw new ExecutionException(e.getCause().getMessage());
            }
            if (add_lock_status.length() == 0) {
                throw new ExecutionException("The Palo Alto is locked, no changes can be made at this time.");
            }
            try {
                // RUN THE SEQUENCE OF COMMANDS
                for (IPaloAltoCommand command : commandList) {
                    // run commands and modify result boolean
                    result = (result && command.execute());
                }
                // COMMIT THE CHANGES (ALSO REMOVES CONFIG LOCK)
                String commit_job_id;
                Map<String, String> commit_params = new HashMap<String, String>();
                commit_params.put("type", "commit");
                commit_params.put("cmd", "<commit></commit>");
                String commit_response = requestWithPolling(PaloAltoMethod.GET, commit_params);
                Document commit_doc = getDocument(commit_response);
                XPath commit_xpath = XPathFactory.newInstance().newXPath();
                try {
                    XPathExpression expr = commit_xpath.compile("/response[@status='success']/result/job/id/text()");
                    commit_job_id = (String) expr.evaluate(commit_doc, XPathConstants.STRING);
                } catch (XPathExpressionException e) {
                    throw new ExecutionException(e.getCause().getMessage());
                }
                if (commit_job_id.length() == 0) {
                    // no commit was done, so release the lock...
                    // REMOVE THE CONFIG LOCK TO RELEASE THE PALO ALTO RESOURCE
                    String remove_lock_status;
                    Map<String, String> remove_lock_params = new HashMap<String, String>();
                    remove_lock_params.put("type", "op");
                    remove_lock_params.put("cmd", "<request><config-lock><remove></remove></config-lock></request>");
                    String remove_lock_response = request(PaloAltoMethod.GET, remove_lock_params);
                    Document remove_lock_doc = getDocument(remove_lock_response);
                    XPath remove_lock_xpath = XPathFactory.newInstance().newXPath();
                    try {
                        XPathExpression expr = remove_lock_xpath.compile("/response[@status='success']/result/text()");
                        remove_lock_status = (String) expr.evaluate(remove_lock_doc, XPathConstants.STRING);
                    } catch (XPathExpressionException e) {
                        throw new ExecutionException(e.getCause().getMessage());
                    }
                    if (remove_lock_status.length() == 0) {
                        throw new ExecutionException("Could not release the Palo Alto device.  Please notify an administrator!");
                    }
                }
            } catch (ExecutionException ex) {
                // REVERT TO RUNNING
                String revert_job_id;
                Map<String, String> revert_params = new HashMap<String, String>();
                revert_params.put("type", "op");
                revert_params.put("cmd", "<load><config><from>running-config.xml</from></config></load>");
                requestWithPolling(PaloAltoMethod.GET, revert_params);
                // REMOVE THE CONFIG LOCK TO RELEASE THE PALO ALTO RESOURCE
                String remove_lock_status;
                Map<String, String> remove_lock_params = new HashMap<String, String>();
                remove_lock_params.put("type", "op");
                remove_lock_params.put("cmd", "<request><config-lock><remove></remove></config-lock></request>");
                String remove_lock_response = request(PaloAltoMethod.GET, remove_lock_params);
                Document remove_lock_doc = getDocument(remove_lock_response);
                XPath remove_lock_xpath = XPathFactory.newInstance().newXPath();
                try {
                    XPathExpression expr = remove_lock_xpath.compile("/response[@status='success']/result/text()");
                    remove_lock_status = (String) expr.evaluate(remove_lock_doc, XPathConstants.STRING);
                } catch (XPathExpressionException e) {
                    throw new ExecutionException(e.getCause().getMessage());
                }
                if (remove_lock_status.length() == 0) {
                    throw new ExecutionException("Could not release the Palo Alto device.  Please notify an administrator!");
                }
                // Bubble up the reason we reverted...
                throw ex;
            }
            return result;
        }
    } else {
        // nothing to do
        return true;
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document) ExecutionException(com.cloud.utils.exception.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 42 with XPathExpression

use of javax.xml.xpath.XPathExpression in project cloudstack by apache.

the class PaloAltoResource method login.

private boolean login(String username, String password) throws ExecutionException {
    Map<String, String> params = new HashMap<String, String>();
    params.put("type", "keygen");
    params.put("user", username);
    params.put("password", password);
    String keygenBody;
    try {
        keygenBody = request(PaloAltoMethod.GET, params);
    } catch (ExecutionException e) {
        return false;
    }
    Document keygen_doc = getDocument(keygenBody);
    XPath xpath = XPathFactory.newInstance().newXPath();
    try {
        XPathExpression expr = xpath.compile("/response[@status='success']/result/key/text()");
        _key = (String) expr.evaluate(keygen_doc, XPathConstants.STRING);
    } catch (XPathExpressionException e) {
        throw new ExecutionException(e.getCause().getMessage());
    }
    if (_key != null) {
        return true;
    }
    return false;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ExecutionException(com.cloud.utils.exception.ExecutionException) Document(org.w3c.dom.Document)

Example 43 with XPathExpression

use of javax.xml.xpath.XPathExpression in project lucene-solr by apache.

the class SimplePostTool method getNodesFromXP.

//
// Utility methods for XPath handing
//
/**
   * Gets all nodes matching an XPath
   */
public static NodeList getNodesFromXP(Node n, String xpath) throws XPathExpressionException {
    XPathFactory factory = XPathFactory.newInstance();
    XPath xp = factory.newXPath();
    XPathExpression expr = xp.compile(xpath);
    return (NodeList) expr.evaluate(n, XPathConstants.NODESET);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) NodeList(org.w3c.dom.NodeList)

Example 44 with XPathExpression

use of javax.xml.xpath.XPathExpression in project gerrit by GerritCodeReview.

the class HtmlDomUtil method compact.

private static void compact(Document doc) {
    try {
        String expr = "//text()[normalize-space(.) = '']";
        XPathFactory xp = XPathFactory.newInstance();
        XPathExpression e = xp.newXPath().compile(expr);
        NodeList empty = (NodeList) e.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < empty.getLength(); i++) {
            Node node = empty.item(i);
            node.getParentNode().removeChild(node);
        }
    } catch (XPathExpressionException e) {
    // Don't do the whitespace removal.
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) XPathFactory(javax.xml.xpath.XPathFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 45 with XPathExpression

use of javax.xml.xpath.XPathExpression in project oxCore by GluuFederation.

the class Response method getAttributes.

public Map<String, List<String>> getAttributes() throws XPathExpressionException {
    Map<String, List<String>> result = new HashMap<String, List<String>>();
    XPath xPath = XPathFactory.newInstance().newXPath();
    xPath.setNamespaceContext(NAMESPACES);
    XPathExpression query = xPath.compile("/samlp:Response/saml:Assertion/saml:AttributeStatement/saml:Attribute");
    NodeList nodes = (NodeList) query.evaluate(xmlDoc, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        Node nameNode = node.getAttributes().getNamedItem("Name");
        if (nameNode == null) {
            continue;
        }
        String attributeName = nameNode.getNodeValue();
        List<String> attributeValues = new ArrayList<String>();
        NodeList nameChildNodes = node.getChildNodes();
        for (int j = 0; j < nameChildNodes.getLength(); j++) {
            Node nameChildNode = nameChildNodes.item(j);
            if (nameChildNode.getNamespaceURI().equalsIgnoreCase("urn:oasis:names:tc:SAML:2.0:assertion") && nameChildNode.getLocalName().equals("AttributeValue")) {
                NodeList valueChildNodes = nameChildNode.getChildNodes();
                for (int k = 0; k < valueChildNodes.getLength(); k++) {
                    Node valueChildNode = valueChildNodes.item(k);
                    attributeValues.add(valueChildNode.getNodeValue());
                }
            }
        }
        result.put(attributeName, attributeValues);
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)98 XPath (javax.xml.xpath.XPath)69 NodeList (org.w3c.dom.NodeList)56 Document (org.w3c.dom.Document)48 XPathExpressionException (javax.xml.xpath.XPathExpressionException)40 XPathFactory (javax.xml.xpath.XPathFactory)40 Node (org.w3c.dom.Node)38 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Element (org.w3c.dom.Element)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 InputSource (org.xml.sax.InputSource)9