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;
}
}
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;
}
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);
}
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.
}
}
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;
}
Aggregations