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