Search in sources :

Example 1 with UrlRewriteFilterContentDescriptor

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

the class UrlRewriteResponse method streamResponse.

@Override
public void streamResponse(InputStream input, OutputStream output) throws IOException {
    InputStream inStream;
    OutputStream outStream;
    boolean isGzip = false;
    BufferedInputStream inBuffer = new BufferedInputStream(input);
    try {
        // Use this way to check whether the input stream is gzip compressed, in case
        // the content encoding header is unknown, as it could be unset in inbound response
        inBuffer.mark(STREAM_BUFFER_SIZE);
        inStream = new GZIPInputStream(inBuffer);
        isGzip = true;
    } catch (ZipException e) {
        inBuffer.reset();
        inStream = inBuffer;
    } catch (IOException e) {
        inBuffer.reset();
        inStream = inBuffer;
    }
    MimeType mimeType = getMimeType();
    UrlRewriteFilterContentDescriptor filterContentConfig = getRewriteFilterConfig(rewriter.getConfig(), bodyFilterName, mimeType);
    if (filterContentConfig != null) {
        String asType = filterContentConfig.asType();
        if (asType != null && asType.trim().length() > 0) {
            mimeType = MimeTypes.create(asType, getCharacterEncoding());
        }
    }
    InputStream filteredInput = UrlRewriteStreamFilterFactory.create(mimeType, null, inStream, rewriter, this, UrlRewriter.Direction.OUT, filterContentConfig);
    outStream = (isGzip) ? new GZIPOutputStream(output) : output;
    IOUtils.copyLarge(filteredInput, outStream, new byte[STREAM_BUFFER_SIZE]);
    // KNOX-685: outStream.flush();
    outStream.close();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) MimeType(javax.activation.MimeType) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor)

Example 2 with UrlRewriteFilterContentDescriptor

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

the class JavaScriptFilterReaderTest method testSimpleMultipleLines.

@Test
public void testSimpleMultipleLines() throws IOException {
    String inputJs = "var url = '/webhdfs/v1' + abs_path + '?op=GET_BLOCK_LOCATIONS';\n" + "$.ajax({\"url\": url, \"crossDomain\": true}).done(function(data) {}).error(network_error_handler(url));\n";
    StringReader inputReader = new StringReader(inputJs);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    config.addApply("/webhdfs/v1", "test-rule");
    JavaScriptFilterReader filterReader = new NoopJsFilterReader(inputReader, config);
    String outputJs = new String(IOUtils.toCharArray(filterReader));
    assertThat(outputJs, is(inputJs));
}
Also used : StringReader(java.io.StringReader) UrlRewriteFilterContentDescriptorImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFilterContentDescriptorImpl) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 3 with UrlRewriteFilterContentDescriptor

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

the class JavaScriptFilterReaderTest method testMatchedJsContent.

@Test
public void testMatchedJsContent() throws IOException {
    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");
    map.put("/webhdfs/v1", "https://knoxhost:8443/webhdfs/v1");
    rules.put("test-rule", map);
    String inputJs = "var url = '/webhdfs/v1' + abs_path + '?op=GET_BLOCK_LOCATIONS';\n" + "$.ajax({\"url\": url, \"crossDomain\": true}).done(function(data) {\n" + "  var url = http://testhost:8088/cluster/app/application_1436831599487_0001;\n" + "}).error(network_error_handler(url));\n";
    StringReader inputReader = new StringReader(inputJs);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    config.addApply("(https?://[^/':,]+:[\\d]+)?/cluster/app", "test-rule");
    config.addApply("/webhdfs/v1", "test-rule");
    JavaScriptFilterReader filterReader = new MatchRuleJsFilterReader(inputReader, rules, config);
    String outputJs = new String(IOUtils.toCharArray(filterReader));
    String expectedOutputJs = "var url = 'https://knoxhost:8443/webhdfs/v1' + abs_path + '?op=GET_BLOCK_LOCATIONS';\n" + "$.ajax({\"url\": url, \"crossDomain\": true}).done(function(data) {\n" + "  var url = https://knoxhost:8443/cluster/app/application_1436831599487_0001;\n" + "}).error(network_error_handler(url));\n";
    assertThat(outputJs, is(expectedOutputJs));
}
Also used : HashMap(java.util.HashMap) StringReader(java.io.StringReader) Map(java.util.Map) HashMap(java.util.HashMap) UrlRewriteFilterContentDescriptorImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFilterContentDescriptorImpl) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 4 with UrlRewriteFilterContentDescriptor

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

the class JavaScriptFilterReaderTest method testSimple.

@Test
public void testSimple() throws IOException {
    String inputJs = "function load_page() {}\n";
    StringReader inputReader = new StringReader(inputJs);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    JavaScriptFilterReader filterReader = new NoopJsFilterReader(inputReader, config);
    String outputJs = new String(IOUtils.toCharArray(filterReader));
    assertThat(outputJs, is(inputJs));
}
Also used : StringReader(java.io.StringReader) UrlRewriteFilterContentDescriptorImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFilterContentDescriptorImpl) UrlRewriteFilterContentDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor) Test(org.junit.Test)

Example 5 with UrlRewriteFilterContentDescriptor

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

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