Search in sources :

Example 6 with UrlRewriteFilterApplyDescriptor

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

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

the class XmlFilterReaderTest method testStreamedApplyForElements.

@Test
public void testStreamedApplyForElements() 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/text()", "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)

Example 8 with UrlRewriteFilterApplyDescriptor

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

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

the class XmlFilterReaderTest method testStreamedApplyForAttributes.

@Test
public void testStreamedApplyForAttributes() 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");
    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", 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}")));
}
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 10 with UrlRewriteFilterApplyDescriptor

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

the class UrlRewriteUtil method filterJavaScript.

public static String filterJavaScript(String inputValue, UrlRewriteFilterContentDescriptor config, UrlRewriteFilterReader filterReader, UrlRewriteFilterPathDescriptor.Compiler<Pattern> regexCompiler) {
    StringBuffer tbuff = new StringBuffer();
    StringBuffer sbuff = new StringBuffer();
    sbuff.append(inputValue);
    if (config != null && !config.getSelectors().isEmpty()) {
        for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
            if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                Matcher matcher = apply.compiledPath(regexCompiler).matcher(sbuff);
                int index = 0;
                while (matcher.find()) {
                    int start = matcher.start();
                    int end = matcher.end();
                    if (start != -1 && end != -1) {
                        tbuff.append(sbuff, index, start);
                        String value = matcher.group();
                        value = filterReader.filterValueString(null, value, apply.rule());
                        tbuff.append(value);
                        index = end;
                    }
                }
                tbuff.append(sbuff, index, sbuff.length());
                sbuff.setLength(0);
                sbuff.append(tbuff);
                tbuff.setLength(0);
            }
        }
    }
    return sbuff.toString();
}
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)

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