Search in sources :

Example 11 with UrlRewriteFilterDescriptor

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

the class XmlUrlRewriteRulesExporter method store.

@Override
public void store(UrlRewriteRulesDescriptor descriptor, Writer writer) throws IOException {
    try {
        Document document = XmlUtils.createDocument();
        Element root = document.createElement(ROOT);
        document.appendChild(root);
        if (!descriptor.getFunctions().isEmpty()) {
            Element functionsElement = document.createElement(FUNCTIONS);
            root.appendChild(functionsElement);
            for (UrlRewriteFunctionDescriptor function : descriptor.getFunctions()) {
                Element functionElement = createElement(document, function.name(), function);
                functionsElement.appendChild(functionElement);
            }
        }
        if (!descriptor.getRules().isEmpty()) {
            for (UrlRewriteRuleDescriptor rule : descriptor.getRules()) {
                Element ruleElement = createRule(document, rule);
                root.appendChild(ruleElement);
            }
        }
        if (!descriptor.getFilters().isEmpty()) {
            for (UrlRewriteFilterDescriptor filter : descriptor.getFilters()) {
                Element filterElement = createFilter(document, filter);
                root.appendChild(filterElement);
            }
        }
        XmlUtils.writeXml(document, writer);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (TransformerException e) {
        throw new IOException(e);
    } catch (InvocationTargetException e) {
        LOG.failedToWriteRulesDescriptor(e);
    } catch (NoSuchMethodException e) {
        LOG.failedToWriteRulesDescriptor(e);
    } catch (IntrospectionException e) {
        LOG.failedToWriteRulesDescriptor(e);
    } catch (IllegalAccessException e) {
        LOG.failedToWriteRulesDescriptor(e);
    }
}
Also used : UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) Element(org.w3c.dom.Element) IntrospectionException(java.beans.IntrospectionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) UrlRewriteFunctionDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor) TransformerException(javax.xml.transform.TransformerException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)

Example 12 with UrlRewriteFilterDescriptor

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

the class UrlRewriteRequest method getRewriteFilterConfig.

private UrlRewriteFilterContentDescriptor getRewriteFilterConfig(String filterName, MimeType mimeType) {
    UrlRewriteFilterContentDescriptor filterContentConfig = null;
    UrlRewriteRulesDescriptor rewriteConfig = rewriter.getConfig();
    if (rewriteConfig != null) {
        UrlRewriteFilterDescriptor filterConfig = rewriteConfig.getFilter(filterName);
        if (filterConfig != null) {
            filterContentConfig = filterConfig.getContent(mimeType);
        }
    }
    return filterContentConfig;
}
Also used : UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)

Example 13 with UrlRewriteFilterDescriptor

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

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

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

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