Search in sources :

Example 1 with UrlRewriteFilterApplyDescriptor

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

the class JsonFilterReader method filterBufferedValues.

private void filterBufferedValues(Level node, List<UrlRewriteFilterPathDescriptor> selectors) {
    for (UrlRewriteFilterPathDescriptor selector : selectors) {
        JsonPath.Expression path = (JsonPath.Expression) selector.compiledPath(JPATH_COMPILER);
        List<JsonPath.Match> matches = path.evaluate(node.node);
        for (JsonPath.Match match : matches) {
            if (match.getNode().isTextual()) {
                if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                    filterBufferedValue(match, (UrlRewriteFilterApplyDescriptor) selector);
                }
            }
        }
    }
}
Also used : JsonPath(org.apache.knox.gateway.util.JsonPath) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 2 with UrlRewriteFilterApplyDescriptor

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

the class XmlFilterReader method streamAttribute.

private void streamAttribute(Element element, Attribute attribute) throws XPathExpressionException {
    Attr node;
    QName name = attribute.getName();
    String prefix = name.getPrefix();
    String uri = name.getNamespaceURI();
    if (uri == null || uri.isEmpty()) {
        node = document.createAttribute(name.getLocalPart());
        element.setAttributeNode(node);
    } else {
        node = document.createAttributeNS(uri, name.getLocalPart());
        if (prefix != null && !prefix.isEmpty()) {
            node.setPrefix(prefix);
        }
        element.setAttributeNodeNS(node);
    }
    String value = attribute.getValue();
    Level level = stack.peek();
    if ((level.scopeConfig) == null || (level.scopeConfig.getSelectors().isEmpty())) {
        value = filterAttribute(null, attribute.getName(), value, null);
        node.setValue(value);
    } else {
        UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath(level);
        if (path instanceof UrlRewriteFilterApplyDescriptor) {
            String rule = ((UrlRewriteFilterApplyDescriptor) path).rule();
            value = filterAttribute(null, attribute.getName(), value, rule);
            node.setValue(value);
        }
    }
    if (prefix == null || prefix.isEmpty()) {
        writer.write(" ");
        writer.write(name.getLocalPart());
    } else {
        writer.write(" ");
        writer.write(prefix);
        writer.write(":");
        writer.write(name.getLocalPart());
    }
    writer.write("=\"");
    writer.write(value);
    writer.write("\"");
    element.removeAttributeNode(node);
}
Also used : QName(javax.xml.namespace.QName) Attr(org.w3c.dom.Attr) UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 3 with UrlRewriteFilterApplyDescriptor

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

the class UrlRewriteFilterGroupDescriptorBase method addApply.

@Override
public UrlRewriteFilterApplyDescriptor addApply(String path, String rule) {
    UrlRewriteFilterApplyDescriptor apply = new UrlRewriteFilterApplyDescriptorImpl();
    apply.path(path);
    apply.rule(rule);
    addSelector(apply);
    return apply;
}
Also used : UrlRewriteFilterApplyDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor)

Example 4 with UrlRewriteFilterApplyDescriptor

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

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

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