use of javax.xml.xpath.XPathExpressionException in project ddf by codice.
the class ContextualEvaluator method getIndexableText.
/**
* Extract the text from the specified XML Document that is to be indexed using the specified
* XPath selectors.
*
* @param document
* @param xpathSelectors
* @return
*/
private static String getIndexableText(String document, String[] xpathSelectors) {
String methodName = "getIndexableText";
List<String> indexedText = new ArrayList<String>();
LOGGER.debug("xpathSelectors.size = {}", xpathSelectors.length);
StringBuilder sbuilder = new StringBuilder();
try {
// TODO Is this safe for all cases? Can there be multiple default namespaces such that
// this would screw up the metadata?
// Treat the "default namespace" (i.e., xmlns="http://some.namespace") the same as the
// "no namespace" (i.e., xmlns="")
// so that user-specified XPath Selectors do not need to specify a namespace for
// expressions in the default namespace
// (For example, user can specify //fileTitle vs. //namespace:fileTitle, where a
// NamespaceContext/NamespaceResolver
// would try to resolve the namespace they specified)
// The regex below, "xmlns=['\"].*?['\"]", looks for:
// xmlns="any chars between single or double quotes"
document = document.replaceAll("xmlns=['\"].*?['\"]", "");
XPathHelper xHelper = new XPathHelper(document);
for (String xpath : xpathSelectors) {
LOGGER.debug("Processing xpath selector: {}", xpath);
NodeList nodeList = (NodeList) xHelper.evaluate(xpath, XPathConstants.NODESET);
LOGGER.debug("nodeList length = {}", nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
Attr attribute = (Attr) node;
LOGGER.debug("Adding text [{}]", attribute.getNodeValue());
sbuilder.append(attribute.getNodeValue() + " ");
// On each element node detected, traverse all of its children. Look for
// any Text nodes it has, adding their text values to the list of indexable text
} else if (node.getNodeType() == Node.ELEMENT_NODE) {
Element elem = (Element) node;
traverse(elem, indexedText);
// getTextContent() concatenates *all* text from all descendant Text nodes
// without
// any white space between each Text node's value, e.g., JohnDoe vs. John
// Doe
// That's not good ...
} else {
LOGGER.debug("Unsupported node type: " + node.getNodeType() + ", node name = " + node.getNodeName());
}
}
}
} catch (XPathExpressionException e1) {
LOGGER.debug("Unable to evaluate XPath", e1);
}
// Append all of the Text nodes' values to the single indexable text string
for (String text : indexedText) {
sbuilder.append(text);
}
return sbuilder.toString();
}
use of javax.xml.xpath.XPathExpressionException in project admin-console-beta by connexta.
the class OpenSearchSourceUtils method verifyOpenSearchCapabilities.
/**
* Verifies that an endpoint given by the URL (and credentials) has OpenSearch capabilities.
*
* Possible Error Codes to be returned
* - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#CANNOT_CONNECT}
* - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#UNAUTHORIZED}
* - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#UNKNOWN_ENDPOINT}
*
* @param urlField endpoint url to verify
* @param creds optional credentials for authentication
* @return an empty {@code Report} on success, otherwise a {@code Report} containing {@link org.codice.ddf.admin.common.report.message.ErrorMessage}s
*/
protected ReportImpl verifyOpenSearchCapabilities(UrlField urlField, CredentialsField creds) {
ReportWithResultImpl<String> responseBodyResult = requestUtils.sendGetRequest(urlField, creds, GET_CAPABILITIES_PARAMS);
if (responseBodyResult.containsErrorMsgs()) {
return responseBodyResult;
}
Document capabilitiesXml;
try {
capabilitiesXml = createDocument(responseBodyResult.result());
} catch (Exception e) {
LOGGER.debug("Failed to read response from OpenSearch endpoint.");
responseBodyResult.addArgumentMessage(unknownEndpointError(urlField.path()));
return responseBodyResult;
}
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(SOURCES_NAMESPACE_CONTEXT);
try {
if ((Boolean) xpath.compile(TOTAL_RESULTS_XPATH).evaluate(capabilitiesXml, XPathConstants.BOOLEAN)) {
return responseBodyResult;
}
} catch (XPathExpressionException e) {
LOGGER.debug("Failed to compile OpenSearch totalResults XPath.");
responseBodyResult.addArgumentMessage(unknownEndpointError(urlField.path()));
return responseBodyResult;
}
responseBodyResult.addArgumentMessage(unknownEndpointError(urlField.path()));
return responseBodyResult;
}
use of javax.xml.xpath.XPathExpressionException in project ddf by codice.
the class TestCatalog method testCswUpdateRemoveAttributesByCqlConstraint.
@Test
public void testCswUpdateRemoveAttributesByCqlConstraint() {
Response response = ingestCswRecord();
String id;
try {
id = getMetacardIdFromCswInsertResponse(response);
} catch (IOException | XPathExpressionException e) {
fail("Could not retrieve the ingested record's ID from the response.");
return;
}
String url = REST_PATH.getUrl() + id;
when().get(url).then().log().all().assertThat().body(hasXPath("//metacard/dateTime[@name='modified']"), hasXPath("//metacard/string[@name='title']"), hasXPath("//metacard/geometry[@name='location']"));
ValidatableResponse validatableResponse = given().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML).body(getFileContent(CSW_REQUEST_RESOURCE_PATH + "/CswUpdateRemoveAttributesByCqlConstraintRequest")).post(CSW_PATH.getUrl()).then();
validatableResponse.body(hasXPath("//TransactionResponse/TransactionSummary/totalDeleted", is("0")), hasXPath("//TransactionResponse/TransactionSummary/totalInserted", is("0")), hasXPath("//TransactionResponse/TransactionSummary/totalUpdated", is("1")));
when().get(url).then().log().all().assertThat().body(not(hasXPath("//metacard/string[@name='title']")), not(hasXPath("//metacard/geometry[@name='location']")), // Check that an attribute that was not updated was not changed.
hasXPath("//metacard/dateTime[@name='modified']"), hasXPath("//metacard/string[@name='topic.category']/value", is("Hydrography--Dictionaries")));
deleteMetacard(id);
}
use of javax.xml.xpath.XPathExpressionException in project ddf by codice.
the class TestCatalog method testCswGetRecordsWithHitsResultType.
@Test
public void testCswGetRecordsWithHitsResultType() {
Response response = ingestCswRecord();
String query = getCswQuery("AnyText", "*", "application/xml", "http://www.opengis.net/cat/csw/2.0.2");
String id;
try {
id = getMetacardIdFromCswInsertResponse(response);
} catch (IOException | XPathExpressionException e) {
fail("Could not retrieve the ingested record's ID from the response.");
return;
}
//test with resultType="results" first
ValidatableResponse validatableResponse = given().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML).body(query).post(CSW_PATH.getUrl()).then();
validatableResponse.body(hasXPath("/GetRecordsResponse/SearchResults/Record"));
//test with resultType="hits"
query = query.replace("results", "hits");
validatableResponse = given().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML).body(query).post(CSW_PATH.getUrl()).then();
//assert that no records have been returned
validatableResponse.body(not(hasXPath("//Record")));
//testing with resultType='validate' is not
//possible due to DDF-1537, this test will need
//to be updated to test this once it is fixed.
deleteMetacard(id);
}
use of javax.xml.xpath.XPathExpressionException in project ddf by codice.
the class TestCatalog method testCswUpdateByNewRecord.
@Test
public void testCswUpdateByNewRecord() {
Response response = ingestCswRecord();
String requestXml = getFileContent(CSW_REQUEST_RESOURCE_PATH + "/CswUpdateRecordRequest");
String id;
try {
id = getMetacardIdFromCswInsertResponse(response);
} catch (IOException | XPathExpressionException e) {
fail("Could not retrieve the ingested record's ID from the response.");
return;
}
requestXml = requestXml.replace("identifier placeholder", id);
ValidatableResponse validatableResponse = given().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML).body(requestXml).post(CSW_PATH.getUrl()).then();
validatableResponse.body(hasXPath("//TransactionResponse/TransactionSummary/totalDeleted", is("0")), hasXPath("//TransactionResponse/TransactionSummary/totalInserted", is("0")), hasXPath("//TransactionResponse/TransactionSummary/totalUpdated", is("1")));
String url = REST_PATH.getUrl() + id;
when().get(url).then().log().all().assertThat().body(hasXPath("//metacard/dateTime[@name='modified']/value", startsWith("2015-08-10")), hasXPath("//metacard/string[@name='title']/value", is("Updated Title")), hasXPath("//metacard/string[@name='topic.category']/value", is("Updated Subject")), hasXPath("(//metacard/geometry[@name='location']/value/Polygon/exterior/LinearRing/pos)[1]", is("1.0 2.0")), hasXPath("(//metacard/geometry[@name='location']/value/Polygon/exterior/LinearRing/pos)[2]", is("3.0 2.0")), hasXPath("(//metacard/geometry[@name='location']/value/Polygon/exterior/LinearRing/pos)[3]", is("3.0 4.0")), hasXPath("(//metacard/geometry[@name='location']/value/Polygon/exterior/LinearRing/pos)[4]", is("1.0 4.0")), hasXPath("(//metacard/geometry[@name='location']/value/Polygon/exterior/LinearRing/pos)[5]", is("1.0 2.0")));
deleteMetacard(id);
}
Aggregations