Search in sources :

Example 11 with UrlRewriteFilterApplyDescriptor

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

the class HtmlFilterReaderBase method getRuleName.

private String getRuleName(String inputValue) {
    if (config != null && !config.getSelectors().isEmpty()) {
        for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
            if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                Matcher matcher = apply.compiledPath(REGEX_COMPILER).matcher(inputValue);
                if (matcher.matches()) {
                    return apply.rule();
                }
            }
        }
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 12 with UrlRewriteFilterApplyDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor 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 13 with UrlRewriteFilterApplyDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor 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)

Example 14 with UrlRewriteFilterApplyDescriptor

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

the class JsonFilterReaderTest method testUnscopedStreaming.

@Test
public void testUnscopedStreaming() 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");
    UrlRewriteFilterApplyDescriptor applyConfig = contentConfig.addApply("$['test-str']", "test-rule");
    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:null<text>"));
}
Also used : 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 15 with UrlRewriteFilterApplyDescriptor

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

the class XmlFilterReaderTest method testStreamedApplyForElementsConfigShortcut.

@Test
public void testStreamedApplyForElementsConfigShortcut() 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");
    UrlRewriteFilterApplyDescriptor applyConfig = contentConfig.addApply("/properties/property/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("text:test-rule-2{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("text:test-rule-2{test-value-3}")));
}
Also used : 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

UrlRewriteFilterApplyDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor)21 StringReader (java.io.StringReader)12 UrlRewriteFilterContentDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor)12 UrlRewriteFilterDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)12 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Test (org.junit.Test)12 InputStream (java.io.InputStream)11 UrlRewriteFilterBufferDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor)8 UrlRewriteFilterPathDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)8 UrlRewriteFilterDetectDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor)5 JsonPath (org.apache.knox.gateway.util.JsonPath)3 Matcher (java.util.regex.Matcher)2 Attr (org.w3c.dom.Attr)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 TextNode (com.fasterxml.jackson.databind.node.TextNode)1 IOException (java.io.IOException)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1