use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor in project knox by apache.
the class JsonFilterReader method filterBufferedNode.
private void filterBufferedNode(Level node) {
for (UrlRewriteFilterPathDescriptor selector : bufferingConfig.getSelectors()) {
JsonPath.Expression path = (JsonPath.Expression) selector.compiledPath(JPATH_COMPILER);
List<JsonPath.Match> matches = path.evaluate(node.node);
for (JsonPath.Match match : matches) {
if (selector instanceof UrlRewriteFilterApplyDescriptor) {
if (match.getNode().isTextual()) {
filterBufferedValue(match, (UrlRewriteFilterApplyDescriptor) selector);
}
} else if (selector instanceof UrlRewriteFilterDetectDescriptor) {
UrlRewriteFilterDetectDescriptor detectConfig = (UrlRewriteFilterDetectDescriptor) selector;
JsonPath.Expression detectPath = (JsonPath.Expression) detectConfig.compiledPath(JPATH_COMPILER);
List<JsonPath.Match> detectMatches = detectPath.evaluate(node.node);
for (JsonPath.Match detectMatch : detectMatches) {
if (detectMatch.getNode().isTextual()) {
String detectValue = detectMatch.getNode().asText();
Pattern detectPattern = detectConfig.compiledValue(REGEX_COMPILER);
if (detectPattern.matcher(detectValue).matches()) {
filterBufferedValues(node, detectConfig.getSelectors());
}
}
}
}
}
}
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor 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));
}
}
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor in project knox by apache.
the class XmlFilterReader method processCharacters.
private void processCharacters(Characters event) throws XPathExpressionException {
// System.out.println( "T[" + event.isCData() + "," + event.isWhiteSpace() + "," + event.isIgnorableWhiteSpace() + "]=" + event );
Level level = stack.peek();
Node node = stack.peek().node;
if (event.isCData()) {
node.appendChild(document.createCDATASection(event.getData()));
} else {
node.appendChild(document.createTextNode(event.getData()));
}
if (!currentlyBuffering()) {
String value = event.getData();
if (!event.isWhiteSpace()) {
if (level.scopeConfig == null || level.scopeConfig.getSelectors().isEmpty()) {
value = filterText(extractQName(node), value, null);
} else {
UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath(level);
if (path instanceof UrlRewriteFilterApplyDescriptor) {
String rule = ((UrlRewriteFilterApplyDescriptor) path).rule();
value = filterText(extractQName(node), value, rule);
}
}
}
if (event.isCData()) {
writer.write("<![CDATA[");
writer.write(value);
writer.write("]]>");
} else {
writer.write(StringEscapeUtils.escapeXml(value));
}
}
}
Aggregations