Search in sources :

Example 16 with UrlRewriteFilterDescriptor

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

the class JsonFilterReaderTest method testInvalidConfigShouldThrowException.

@Test
public void testInvalidConfigShouldThrowException() throws Exception {
    String input = "{\"test-name\":\"test-value\"}";
    // System.out.println( "INPUT=" + input );
    UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1");
    UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("*/json");
    contentConfig.addApply("/root/@url", "test-rule");
    try {
        JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig);
        IOUtils.toString(filter);
        fail("Should have thrown an IllegalArgumentException.");
    } catch (IOException e) {
        fail("Should have thrown an IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("/root/@url"));
    }
}
Also used : StringReader(java.io.StringReader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 17 with UrlRewriteFilterDescriptor

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

the class JsonFilterReaderTest method testNamesWithDots.

@Test
public void testNamesWithDots() throws IOException {
    InputStream stream = TestUtils.getResourceStream(this.getClass(), "dotted-field-name.json");
    String input = IOUtils.toString(stream, Charset.forName("UTF-8"));
    // System.out.println( "INPUT=" + input );
    UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("test-filter");
    UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("application/json");
    // NOTE: The field names are rewritten first so the values rules need to match the rewritten name.
    contentConfig.addApply("$.name<testField>", "test-rule");
    contentConfig.addApply("$.name<test_field>", "test-rule");
    contentConfig.addApply("$.name<test-field>", "test-rule");
    contentConfig.addApply("$['name<test.field>']", "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<testField>']", is("value:test-rule<testField value>"));
    JsonAssert.with(output).assertThat("$['name<test_field>']", is("value:test-rule<test_field value>"));
    JsonAssert.with(output).assertThat("$['name<test-field>']", is("value:test-rule<test-field value>"));
    JsonAssert.with(output).assertThat("$['name<test.field>']", is("value:test-rule<test.field value>"));
}
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) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 18 with UrlRewriteFilterDescriptor

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

the class XmlFilterReaderTest method testSimpleBuffered.

// @Test
// public void testSimpleScoped() throws IOException, ParserConfigurationException, XMLStreamException {
// UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
// UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
// UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );
// 
// String inputXml = "<root/>";
// StringReader inputReader = new StringReader( inputXml );
// XmlStaxFilterReader filterReader = new NoopXmlFilterReader( inputReader );
// String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
// assertThat( the( outputHtml ), hasXPath( "/root" ) );
// }
@Test
public void testSimpleBuffered() 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 input = "<root/>";
    // System.out.println( "INPUT=" + input );
    StringReader inputReader = new StringReader(input);
    XmlFilterReader filterReader = new NoopXmlFilterReader(inputReader, contentConfig);
    String output = new String(IOUtils.toCharArray(filterReader));
    // System.out.println( "OUTPUT=" + output );
    assertThat(the(output), hasXPath("/root"));
}
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 19 with UrlRewriteFilterDescriptor

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

Example 20 with UrlRewriteFilterDescriptor

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

the class XmlFilterReaderTest method testBufferedApplyForAttributes.

@Test
public void testBufferedApplyForAttributes() 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");
    UrlRewriteFilterApplyDescriptor applyConfig = bufferconfig.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("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 : 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

UrlRewriteFilterDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)22 UrlRewriteFilterContentDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor)20 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)20 StringReader (java.io.StringReader)19 Matchers.containsString (org.hamcrest.Matchers.containsString)19 Test (org.junit.Test)19 InputStream (java.io.InputStream)12 UrlRewriteFilterApplyDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor)12 UrlRewriteFilterBufferDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor)11 IOException (java.io.IOException)3 UrlRewriteFilterDetectDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor)3 IntrospectionException (java.beans.IntrospectionException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 UrlRewriteFunctionDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor)1 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1