use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor in project knox by apache.
the class XmlFilterReaderTest method testBufferedApplyForAttributes.
@Test
public void testBufferedApplyForAttributes() throws Exception {
InputStream stream = TestUtils.getResourceStream(this.getClass(), "properties-attributes.xml");
String input = IOUtils.toString(stream, Charset.forName("UTF-8"));
// System.out.println( "INPUT=" + input );
UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1");
UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/xml");
UrlRewriteFilterBufferDescriptor bufferconfig = contentConfig.addBuffer("/properties/property");
UrlRewriteFilterApplyDescriptor applyConfig = bufferconfig.addApply("@value", "test-rule-2");
// UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
XmlFilterReader filter = new TestXmlFilterReader(new StringReader(input), contentConfig);
String output = IOUtils.toString(filter);
// System.out.println( "OUTPUT=" + output );
assertThat(the(output), hasXPath("/properties/property[1]/@name", equalTo("test-name-1")));
assertThat(the(output), hasXPath("/properties/property[1]/@value", equalTo("attr:test-rule-2{test-value-1}")));
assertThat(the(output), hasXPath("/properties/property[2]/@name", equalTo("test-name-2")));
assertThat(the(output), hasXPath("/properties/property[2]/@value", equalTo("attr:test-rule-2{test-value-2}")));
assertThat(the(output), hasXPath("/properties/property[3]/@name", equalTo("test-name-3")));
assertThat(the(output), hasXPath("/properties/property[3]/@value", equalTo("attr:test-rule-2{test-value-3}")));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor in project knox by apache.
the class XmlFilterReaderTest method testBufferedDetectApplyForElements.
@Test
public void testBufferedDetectApplyForElements() throws Exception {
InputStream stream = TestUtils.getResourceStream(this.getClass(), "properties-elements.xml");
String input = IOUtils.toString(stream, Charset.forName("UTF-8"));
// System.out.println( "INPUT=" + input );
UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1");
UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/xml");
UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer("/properties/property");
UrlRewriteFilterDetectDescriptor detectConfig = bufferConfig.addDetect("name", "test-name-2");
UrlRewriteFilterApplyDescriptor applyConfig = detectConfig.addApply("value", "test-rule-2");
// UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
XmlFilterReader filter = new TestXmlFilterReader(new StringReader(input), contentConfig);
String output = IOUtils.toString(filter);
// System.out.println( "OUTPUT=" + output );
assertThat(the(output), hasXPath("/properties/property[1]/name/text()", equalTo("test-name-1")));
assertThat(the(output), hasXPath("/properties/property[1]/value/text()", equalTo("test-value-1")));
assertThat(the(output), hasXPath("/properties/property[2]/name/text()", equalTo("test-name-2")));
assertThat(the(output), hasXPath("/properties/property[2]/value/text()", equalTo("text:test-rule-2{test-value-2}")));
assertThat(the(output), hasXPath("/properties/property[3]/name/text()", equalTo("test-name-3")));
assertThat(the(output), hasXPath("/properties/property[3]/value/text()", equalTo("test-value-3")));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor in project knox by apache.
the class JsonFilterReader method filterStreamValue.
protected String filterStreamValue(Level node) {
String value;
if (node.isArray()) {
value = node.node.get(0).asText();
} else {
value = node.node.get(node.field).asText();
}
String rule = null;
UrlRewriteFilterGroupDescriptor scope = node.scopeConfig;
// TODO: Scan the top level apply rules for the first match.
if (scope != null) {
for (UrlRewriteFilterPathDescriptor selector : scope.getSelectors()) {
JsonPath.Expression path = (JsonPath.Expression) selector.compiledPath(JPATH_COMPILER);
List<JsonPath.Match> matches = path.evaluate(node.scopeNode);
if (matches != null && !matches.isEmpty()) {
JsonPath.Match match = matches.get(0);
if (match.getNode().isTextual()) {
if (selector instanceof UrlRewriteFilterApplyDescriptor) {
UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
rule = apply.rule();
break;
}
}
}
}
}
try {
value = filterValueString(node.field, value, rule);
if (node.isArray()) {
((ArrayNode) node.node).set(0, new TextNode(value));
} else {
((ObjectNode) node.node).put(node.field, value);
}
} catch (Exception e) {
LOG.failedToFilterValue(value, rule, e);
}
return value;
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor 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.UrlRewriteFilterApplyDescriptor 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