@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>")); }
@Test public void testValueNumberWithBuffering() throws Exception { String input = "{ \"apps\" : {\"app\":[{\"id\":\"one\", \"progress\":100.0, \"startedTime\":1399975176760}]} }"; UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create(); UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1"); UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/json"); UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer("$.apps.app[*]"); UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$.id", "test-rule"); JsonFilterReader filter = new JsonFilterReader(new StringReader(input), contentConfig); String output = IOUtils.toString(filter); assertThat(output, containsString("\"startedTime\":1399975176760}")); }
private void processText(Segment segment) { String inputValue = segment.toString(); String outputValue = inputValue; try { if (stack.isEmpty()) { // This can happen for whitespace outside of the root element. // outputValue = filterText( null, inputValue ); } else { String tagType = stack.peek().getTag().getAttributeValue("type"); String tagName = stack.peek().getTag().getName(); if (SCRIPTTAG.equals(tagName) && JSTYPES.contains(tagType) && config != null && !config.getSelectors().isEmpty()) { // embedded javascript content outputValue = UrlRewriteUtil.filterJavaScript(inputValue, config, this, REGEX_COMPILER); } else { outputValue = filterText(stack.peek().getQName(), inputValue, null); } } if (outputValue == null) { outputValue = inputValue; } } catch (Exception e) { LOG.failedToFilterValue(inputValue, null, e); } writer.write(outputValue); }
@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>")); }
@Test public void testBufferedApply() 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>"); UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$.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>")); }
@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"); // UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) ); 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")); } }