Search in sources :

Example 6 with UrlRewriteFilterBufferDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.

the class XmlFilterReaderTest method testSimpleAttributeBuffered.

@Test
public void testSimpleAttributeBuffered() throws IOException, ParserConfigurationException, XMLStreamException {
    UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1");
    UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/xml");
    UrlRewriteFilterBufferDescriptor scopeConfig = contentConfig.addBuffer("/root");
    String inputXml = "<root name='value'/>";
    StringReader inputReader = new StringReader(inputXml);
    XmlFilterReader filterReader = new NoopXmlFilterReader(inputReader, contentConfig);
    String outputHtml = new String(IOUtils.toCharArray(filterReader));
    // System.out.println( outputHtml );
    assertThat(the(outputHtml), hasXPath("/root/@name", equalTo("value")));
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) StringReader(java.io.StringReader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 7 with UrlRewriteFilterBufferDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.

the class XmlFilterReaderTest method testBufferedDetectApplyForAttributes.

@Test
public void testBufferedDetectApplyForAttributes() 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");
    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", equalTo("test-name-1")));
    assertThat(the(output), hasXPath("/properties/property[1]/@value", equalTo("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("test-value-3")));
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) InputStream(java.io.InputStream) StringReader(java.io.StringReader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) UrlRewriteFilterDetectDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor) Test(org.junit.Test)

Example 8 with UrlRewriteFilterBufferDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.

the class UrlRewriteFilterContentDescriptorImpl method addBuffer.

@Override
public UrlRewriteFilterBufferDescriptor addBuffer(String path) {
    UrlRewriteFilterBufferDescriptor descriptor = new UrlRewriteFilterBufferDescriptorImpl();
    descriptor.path(path);
    addSelector(descriptor);
    return descriptor;
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor)

Example 9 with UrlRewriteFilterBufferDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.

the class JsonFilterReaderTest method testBuffered.

// @Test
// public void testJsonPathObject() throws IOException {
// InputStream stream = TestUtils.getResourceStream( this.getClass(), "complex.json" );
// String input = IOUtils.toString( stream, Charset.forName( "UTF-8" ) );
// 
// Object o;
// 
// o = JsonPath.read( "$", input, JsonNode.class );
// assertThat( o, instanceOf( ObjectNode.class ) );
// assertThat( o.toString(), startsWith( "{" ) );
// System.out.println( o.getClass() + "=" + o );
// 
// o = JsonPath.read( "$['test-str']", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
// 
// o = JsonPath.read( "$['test-obj-multi']", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
// 
// o = JsonPath.read( "$['val']", (JsonNode)o, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
// 
// JsonPath p = JsonPath.compile( "$['test-obj-multi']['val']" );
// o = JsonPath.read( "$['test-obj-multi']['val']", input, JsonNode.class );
// JsonNode pp = ((JsonNode)o).findParent("val");
// System.out.println( "$['test-obj-multi']['val']=" + o.getClass() + "=" + o );
// 
// }
// 
// @Test
// public void testJsonPathArray() throws IOException {
// InputStream stream = TestUtils.getResourceStream( this.getClass(), "array.json" );
// String input = IOUtils.toString( stream, Charset.forName( "UTF-8" ) );
// 
// Object o;
// 
// o = JsonPath.read( "$", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
// 
// o = JsonPath.read( "$[0]", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
// 
// o = JsonPath.read( "$[*]", input, JsonNode.class );
// System.out.println( "$[*]=" + o.getClass() + "=" + o );
// 
// o = JsonPath.read( "$['obj1-fld1']", (JsonNode)o, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
// 
// o = JsonPath.read( "$[0]['obj1-fld1']", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
// 
// }
@Test
public void testBuffered() throws IOException {
    InputStream stream = TestUtils.getResourceStream(this.getClass(), "simple-values.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("$");
    UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$['name<test-str>']", "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<test-str>", is("value:test-rule<text>"));
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) InputStream(java.io.InputStream) StringReader(java.io.StringReader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 10 with UrlRewriteFilterBufferDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor in project knox by apache.

the class JsonFilterReaderTest method testBufferedMultiApply.

@Test
public void testBufferedMultiApply() 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>");
    UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$.*.name<property>.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>"));
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) InputStream(java.io.InputStream) StringReader(java.io.StringReader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Aggregations

UrlRewriteFilterBufferDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor)14 StringReader (java.io.StringReader)11 UrlRewriteFilterContentDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor)11 UrlRewriteFilterDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)11 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Test (org.junit.Test)11 UrlRewriteFilterApplyDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor)8 InputStream (java.io.InputStream)7 UrlRewriteFilterDetectDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor)3 UrlRewriteFilterPathDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)2 EndElement (javax.xml.stream.events.EndElement)1 StartElement (javax.xml.stream.events.StartElement)1 UrlRewriteFilterGroupDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor)1 UrlRewriteFilterScopeDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterScopeDescriptor)1 JsonPath (org.apache.knox.gateway.util.JsonPath)1 Element (org.w3c.dom.Element)1