Search in sources :

Example 1 with UrlRewriteFilterBufferDescriptor

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;
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) JsonPath(org.apache.knox.gateway.util.JsonPath) UrlRewriteFilterGroupDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 2 with UrlRewriteFilterBufferDescriptor

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);
        }
    }
}
Also used : UrlRewriteFilterBufferDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor) EndElement(javax.xml.stream.events.EndElement) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) UrlRewriteFilterScopeDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterScopeDescriptor) UrlRewriteFilterPathDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor)

Example 3 with UrlRewriteFilterBufferDescriptor

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"));
}
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 4 with UrlRewriteFilterBufferDescriptor

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>"));
}
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 5 with UrlRewriteFilterBufferDescriptor

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}"));
}
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)

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