use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor in project knox by apache.
the class XmlFilterReader method processBufferedElement.
private void processBufferedElement(Level level, UrlRewriteFilterGroupDescriptor config) throws XPathExpressionException {
for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
if (selector instanceof UrlRewriteFilterApplyDescriptor) {
XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
if (node != null) {
UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
if (node instanceof Element) {
Element element = (Element) node;
String value = element.getTextContent();
value = filterText(extractQName(element), value, apply.rule());
element.setTextContent(value);
} else if (node instanceof Text) {
Text text = (Text) node;
String value = text.getWholeText();
value = filterText(extractQName(text.getParentNode()), value, apply.rule());
text.replaceWholeText(value);
} else if (node instanceof Attr) {
Attr attr = (Attr) node;
String value = attr.getValue();
value = filterAttribute(extractQName(attr.getOwnerElement()), extractQName(attr), value, apply.rule());
attr.setValue(value);
} else {
throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
}
}
} else if (selector instanceof UrlRewriteFilterDetectDescriptor) {
XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
if (node != null) {
UrlRewriteFilterDetectDescriptor detect = (UrlRewriteFilterDetectDescriptor) selector;
String value = null;
if (node instanceof Element) {
Element element = (Element) node;
value = element.getTextContent();
} else if (node instanceof Text) {
Text text = (Text) node;
value = text.getWholeText();
} else if (node instanceof Attr) {
Attr attr = (Attr) node;
value = attr.getValue();
} else {
throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
}
if (detect.compiledValue(REGEX_COMPILER).matcher(value).matches()) {
processBufferedElement(level, detect);
}
}
} else {
throw new IllegalArgumentException(RES.unexpectedRewritePathSelector(selector));
}
}
}
Aggregations