Search in sources :

Example 16 with UrlRewriteFilterContentDescriptor

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

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

the class HtmlFilterReaderBaseTest method testMatchedJavaScriptText.

@Test
public void testMatchedJavaScriptText() throws IOException, ParserConfigurationException {
    Map<String, Map<String, String>> rules = new HashMap<>();
    Map<String, String> map = new HashMap<>();
    map.put("(https?://[^/':,]+:[\\d]+)?/cluster/app", "https://knoxhost:8443/cluster/app");
    rules.put("test-rule", map);
    String inputXml = "<root>\n" + "  <script type=\"text/javascript\">\n" + "    var appsTableData=[\n" + "      [\"<a href='/cluster/app/application_1436831599487_0008'>application_1436831599487_0008</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='http://testhost:8088/cluster/app/application_1436831599487_0008'>History</a>\"],\n" + "      [\"<a href='/cluster/app/application_1436831599487_0006'>application_1436831599487_0006</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='http://testhost:8088/cluster/app/application_1436831599487_0006'>History</a>\"],\n" + "      [\"<a href='/cluster/app/application_1436831599487_0007'>application_1436831599487_0007</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='http://testhost:8088/cluster/app/application_1436831599487_0007'>History</a>\"]\n" + "    ]\n" + "  </script>\n" + "</root>\n";
    StringReader inputReader = new StringReader(inputXml);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    config.addApply("(https?://[^/':,]+:[\\d]+)?/cluster/app", "test-rule");
    HtmlFilterReaderBase filterReader = new MatchRuleXmlFilterReader(inputReader, rules, config);
    String outputXml = new String(IOUtils.toCharArray(filterReader));
    String expectedOutput = "<root>\n" + "  <script type=\"text/javascript\">\n" + "    var appsTableData=[\n" + "      [\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0008'>application_1436831599487_0008</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0008'>History</a>\"],\n" + "      [\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0006'>application_1436831599487_0006</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0006'>History</a>\"],\n" + "      [\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0007'>application_1436831599487_0007</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0007'>History</a>\"]\n" + "    ]\n" + "  </script>\n" + "</root>\n";
    assertThat(outputXml, is(expectedOutput));
}
Also used : HashMap(java.util.HashMap) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) UrlRewriteFilterContentDescriptorImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFilterContentDescriptorImpl) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 18 with UrlRewriteFilterContentDescriptor

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

the class HtmlFilterReaderBaseTest method testMRJobHistoryUIJavaScriptText.

@Test
public void testMRJobHistoryUIJavaScriptText() throws IOException, ParserConfigurationException {
    Map<String, Map<String, String>> rules = new HashMap<>();
    Map<String, String> map = new HashMap<>();
    map.put("https?://[^/':,]+:[\\d]+", "https://knoxhost:8443/gateway/nodemanagerui/node?host=knoxhost");
    rules.put("test-rule", map);
    String inputXml = "<root>\n" + "  <script type=\"text/javascript\">\n" + "    var appsTableData=[\n" + "      [\"<a href='http://testhost:8042'>/default-rack/node</a>\",\"<a href='http://testhost:8042'>testhost:8042</a>\"],\n" + "    ]\n" + "  </script>\n" + "</root>\n";
    StringReader inputReader = new StringReader(inputXml);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    config.addApply("https?://[^/':,]+:[\\d]+", "test-rule");
    HtmlFilterReaderBase filterReader = new MatchRuleXmlFilterReader(inputReader, rules, config);
    String outputXml = new String(IOUtils.toCharArray(filterReader));
    String expectedOutput = "<root>\n" + "  <script type=\"text/javascript\">\n" + "    var appsTableData=[\n" + "      [\"<a href='https://knoxhost:8443/gateway/nodemanagerui/node?host=knoxhost'>/default-rack/node</a>\",\"<a href='https://knoxhost:8443/gateway/nodemanagerui/node?host=knoxhost'>testhost:8042</a>\"],\n" + "    ]\n" + "  </script>\n" + "</root>\n";
    assertThat(outputXml, is(expectedOutput));
}
Also used : HashMap(java.util.HashMap) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) UrlRewriteFilterContentDescriptorImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFilterContentDescriptorImpl) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 19 with UrlRewriteFilterContentDescriptor

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

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

Aggregations

UrlRewriteFilterContentDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor)28 StringReader (java.io.StringReader)24 Test (org.junit.Test)24 UrlRewriteFilterDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)20 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)20 Matchers.containsString (org.hamcrest.Matchers.containsString)19 InputStream (java.io.InputStream)14 UrlRewriteFilterApplyDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor)12 UrlRewriteFilterBufferDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor)11 UrlRewriteFilterContentDescriptorImpl (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFilterContentDescriptorImpl)5 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 UrlRewriteFilterDetectDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDetectDescriptor)3 MimeType (javax.activation.MimeType)2 BufferedInputStream (java.io.BufferedInputStream)1 OutputStream (java.io.OutputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ZipException (java.util.zip.ZipException)1