use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.
the class JsonFilterReader method startBuffering.
protected boolean startBuffering(Level node) {
boolean buffered = false;
UrlRewriteFilterGroupDescriptor scope = node.scopeConfig;
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()) {
if (selector instanceof UrlRewriteFilterBufferDescriptor) {
bufferingLevel = node;
bufferingConfig = (UrlRewriteFilterBufferDescriptor) selector;
buffered = true;
}
break;
}
}
}
return buffered;
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.
the class XmlFilterReader method processStartElement.
private void processStartElement(StartElement event) throws XPathExpressionException {
// System.out.println( "SE=" + event );
// Create a new "empty" element and add it to the document.
Element element = bufferElement(event);
Level parent = stack.peek();
parent.node.appendChild(element);
// Note: Don't currently support nested buffer or scope descriptors.
if (currentlyBuffering()) {
pushLevel(parent, event, element, parent.scopeNode, parent.scopeConfig);
bufferAttributes(event, element);
// Else not currently buffering
} else {
// See if there is a matching path descriptor in the current scope.
UrlRewriteFilterPathDescriptor descriptor = pickFirstMatchingPath(parent);
if (descriptor != null) {
// If this is a buffer descriptor then switch to buffering and buffer the attributes.
if (descriptor instanceof UrlRewriteFilterBufferDescriptor) {
pushLevel(parent, event, element, element, (UrlRewriteFilterBufferDescriptor) descriptor);
bufferAttributes(event, element);
// Otherwise if this is a scope descriptor then change the scope and stream the attributes.
} else if (descriptor instanceof UrlRewriteFilterScopeDescriptor) {
pushLevel(parent, event, element, element, (UrlRewriteFilterScopeDescriptor) descriptor);
streamElement(event, element);
// Else found an unexpected matching path.
} else {
// This is likely because there is an <apply> targeted at the text of an element.
// That "convenience" config will be taken care of in the streamElement() processing.
pushLevel(parent, event, element, parent.scopeNode, parent.scopeConfig);
streamElement(event, element);
}
// If there is no matching path descriptor then continue streaming.
} else {
pushLevel(parent, event, element, parent.scopeNode, parent.scopeConfig);
streamElement(event, element);
}
}
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.
the class JsonFilterReaderTest method testBufferedDetectApply.
@Test
public void testBufferedDetectApply() throws IOException {
InputStream stream = TestUtils.getResourceStream(this.getClass(), "properties.json");
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/json");
UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer("$.name<properties>.*.name<property>");
UrlRewriteFilterDetectDescriptor detectConfig = bufferConfig.addDetect("$.name<property-name>", "test-name-2");
UrlRewriteFilterApplyDescriptor applyConfig = detectConfig.addApply("$.name<property-value>", "test-rule-2");
// UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig);
String output = IOUtils.toString(filter);
// System.out.println( "OUTPUT=" + output );
JsonAssert.with(output).assertThat("name<properties>[0].name<property>.name<property-name>", is("test-name-1"));
JsonAssert.with(output).assertThat("name<properties>[0].name<property>.name<property-value>", is("test-value-1"));
JsonAssert.with(output).assertThat("name<properties>[1].name<property>.name<property-name>", is("test-name-2"));
JsonAssert.with(output).assertThat("name<properties>[1].name<property>.name<property-value>", is("value:test-rule-2<test-value-2>"));
JsonAssert.with(output).assertThat("name<properties>[2].name<property>.name<property-name>", is("test-name-3"));
JsonAssert.with(output).assertThat("name<properties>[2].name<property>.name<property-value>", is("test-value-3"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.
the class JsonFilterReaderTest method testBufferedApply.
@Test
public void testBufferedApply() throws IOException {
InputStream stream = TestUtils.getResourceStream(this.getClass(), "properties.json");
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/json");
UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer("$.name<properties>.*.name<property>");
UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$.name<property-value>", "test-rule");
// UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );
JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig);
String output = IOUtils.toString(filter);
// System.out.println( "OUTPUT=" + output );
JsonAssert.with(output).assertThat("name<properties>[0].name<property>.name<property-name>", is("test-name-1"));
JsonAssert.with(output).assertThat("name<properties>[0].name<property>.name<property-value>", is("value:test-rule<test-value-1>"));
JsonAssert.with(output).assertThat("name<properties>[1].name<property>.name<property-name>", is("test-name-2"));
JsonAssert.with(output).assertThat("name<properties>[1].name<property>.name<property-value>", is("value:test-rule<test-value-2>"));
JsonAssert.with(output).assertThat("name<properties>[2].name<property>.name<property-name>", is("test-name-3"));
JsonAssert.with(output).assertThat("name<properties>[2].name<property>.name<property-value>", is("value:test-rule<test-value-3>"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.
the class JsonFilterReaderTest method testValueNumberWithBuffering.
@Test
public void testValueNumberWithBuffering() throws Exception {
String input = "{ \"apps\" : {\"app\":[{\"id\":\"one\", \"progress\":100.0, \"startedTime\":1399975176760}]} }";
UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1");
UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/json");
UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer("$.apps.app[*]");
UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$.id", "test-rule");
JsonFilterReader filter = new JsonFilterReader(new StringReader(input), contentConfig);
String output = IOUtils.toString(filter);
assertThat(output, containsString("\"startedTime\":1399975176760}"));
}
Aggregations