use of javax.xml.xpath.XPathFactory in project perun by CESNET.
the class MuPasswordManagerModule method parseUCO.
/**
* Parse UCO from XML body response and convert it to map of parameters.
*
* @param document XML document to be parsed
* @param requestID unique ID of a request
* @return Map of response params
* @throws InternalErrorException
*/
private Map<String, String> parseUCO(Document document, int requestID) throws InternalErrorException {
Map<String, String> result = new HashMap<>();
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression ucoExpr;
try {
ucoExpr = xpath.compile("//resp/uco/text()");
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
}
try {
String uco = (String) ucoExpr.evaluate(document, XPathConstants.STRING);
result.put("urn:perun:user:attribute-def:def:login-namespace:mu", uco);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on resulting document for request ID: " + requestID, ex);
}
return result;
}
use of javax.xml.xpath.XPathFactory in project perun by CESNET.
the class MuPasswordManagerModule method parseResponse.
/**
* Parse XML response from IS MU to XML document.
*
* @param inputStream Stream to be parsed to Document
* @param requestID ID of request made to IS MU.
* @return XML document for further processing
* @throws InternalErrorException
*/
private Document parseResponse(InputStream inputStream, int requestID) throws InternalErrorException {
//Create new document factory builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException ex) {
throw new InternalErrorException("Error when creating newDocumentBuilder. Request ID: " + requestID, ex);
}
String response = null;
try {
response = convertStreamToString(inputStream, "UTF-8");
} catch (IOException ex) {
log.error("Unable to convert InputStream to String: {}", ex);
}
log.trace("Request ID: " + requestID + " Response: " + response);
Document doc;
try {
doc = builder.parse(new InputSource(new StringReader(response)));
} catch (SAXParseException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Request ID: " + requestID, ex);
} catch (SAXException ex) {
throw new InternalErrorException("Problem with parsing is more complex, not only invalid characters. Request ID: " + requestID, ex);
} catch (IOException ex) {
throw new InternalErrorException("Error when parsing uri by document builder. Problem with input or output. Request ID: " + requestID, ex);
}
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression isErrorExpr;
XPathExpression getErrorTextExpr;
XPathExpression getDbErrorTextExpr;
try {
isErrorExpr = xpath.compile("//resp/stav/text()");
getErrorTextExpr = xpath.compile("//resp/error/text()");
getDbErrorTextExpr = xpath.compile("//resp/dberror/text()");
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query. Request ID: " + requestID, ex);
}
// OK or ERROR
String responseStatus;
try {
responseStatus = (String) isErrorExpr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on document to resolve response status. Request ID: " + requestID, ex);
}
log.trace("Request ID: " + requestID + " Response status: " + responseStatus);
if ("OK".equals(responseStatus)) {
return doc;
} else {
try {
String error = (String) getErrorTextExpr.evaluate(doc, XPathConstants.STRING);
if (error == null || error.isEmpty()) {
error = (String) getDbErrorTextExpr.evaluate(doc, XPathConstants.STRING);
}
throw new InternalErrorException("IS MU (password manager backend) responded with error to a Request ID: " + requestID + " Error: " + error);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on document to resolve error status. Request ID: " + requestID, ex);
}
}
}
use of javax.xml.xpath.XPathFactory in project ats-framework by Axway.
the class XmlUtilities method getByXpath.
/**
* Get values matching passed XPath expression
* @param node
* @param expression XPath expression
* @return matching values
* @throws Exception
*/
private static String[] getByXpath(Node node, String expression) throws Exception {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression xPathExpression = xPath.compile(expression);
NodeList nlist = (NodeList) xPathExpression.evaluate(node, XPathConstants.NODESET);
int nodeListSize = nlist.getLength();
List<String> values = new ArrayList<String>(nodeListSize);
for (int index = 0; index < nlist.getLength(); index++) {
Node aNode = nlist.item(index);
values.add(aNode.getTextContent());
}
return values.toArray(new String[values.size()]);
}
use of javax.xml.xpath.XPathFactory in project perun by CESNET.
the class AbstractPublicationSystemStrategy method getValueFromXpath.
/**
* Get xml Node and xpath expression to get value from node by this xpath.
*
* @param node node for getting value from
* @param xpathExpression expression for xpath to looking for value in node
* @param resultType type of resulting / expected object (string number node nodelist ...)
* @return object extracted from node by xpath
* @throws InternalErrorException
*/
protected Object getValueFromXpath(Node node, String xpathExpression, QName resultType) throws InternalErrorException {
//Prepare xpath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr;
try {
expr = xpath.compile(xpathExpression);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when compiling xpath query.", ex);
}
Object result;
try {
result = expr.evaluate(node, resultType);
} catch (XPathExpressionException ex) {
throw new InternalErrorException("Error when evaluate xpath query on node.", ex);
}
return result;
}
use of javax.xml.xpath.XPathFactory in project cloudstack by apache.
the class OvmObject method xmlToList.
public List<String> xmlToList(String path, Document xmlDocument) throws Ovm3ResourceException {
List<String> list = new ArrayList<String>();
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();
try {
XPathExpression xPathExpression = xPath.compile(path);
NodeList nodeList = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);
for (int ind = 0; ind < nodeList.getLength(); ind++) {
if (!nodeList.item(ind).getTextContent().isEmpty()) {
list.add("" + nodeList.item(ind).getTextContent());
} else {
list.add("" + nodeList.item(ind).getNodeValue());
}
}
return list;
} catch (XPathExpressionException e) {
throw new Ovm3ResourceException("Problem parsing XML to List: ", e);
}
}
Aggregations