Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.github.skjolber.jsonfilter.jackson;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.github.skjolber.jsonfilter.JsonFilterMetrics;
Expand Down Expand Up @@ -45,6 +47,19 @@ public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayO
return false;
}
}

public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
try (JsonParser parser = jsonFactory.createParser(chars, offset, length)) {
if(parse(parser)) {
output.ensureCapacity(output.length() + length);
output.append(new String(chars, offset, length, StandardCharsets.UTF_8));
return true;
}
return false;
} catch(final Exception e) {
return false;
}
}

protected boolean parse(JsonParser parser) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public boolean process(byte[] bytes, int offset, int length, StringBuilder outpu
}

if(maxSize >= length) {
output.append(new String(bytes, offset, length));
return true;
return super.process(bytes, offset, length, output, metrics);
}
output.ensureCapacity(output.length() + length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public JacksonMaxSizeMaxStringLengthJsonFilter(int maxStringLength, int maxSize,
super(maxStringLength, maxSize, pruneMessage, anonymizeMessage, truncateMessage, jsonFactory);
}

@Override
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
if(maxSize >= length) {
return super.process(chars, offset, length, output, metrics);
Expand All @@ -46,6 +47,7 @@ public boolean process(char[] chars, int offset, int length, StringBuilder outpu
}
}

@Override
public boolean process(byte[] bytes, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
if(maxSize >= length) {
return super.process(bytes, offset, length, output, metrics);
Expand All @@ -62,6 +64,7 @@ public boolean process(byte[] bytes, int offset, int length, StringBuilder outpu
}
}

@Override
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics metrics) {
if(maxSize >= length) {
return super.process(bytes, offset, length, output, metrics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,6 @@ public static void writeMaxStringLength(final JsonParser parser, JsonGenerator g
}
}

public static void skipMaxStringLength(final JsonParser parser, JsonGenerator generator, int maxStringLength, StringBuilder builder, JsonFilterMetrics metrics, char[] truncateStringValue) throws IOException {
int level = 1;

while(level > 0) {
JsonToken nextToken = parser.nextToken();
if(nextToken == null) {
break;
}

switch(nextToken) {
case START_OBJECT:
case START_ARRAY:
level++;
break;
case END_OBJECT:
case END_ARRAY:
level--;
break;
case VALUE_STRING:
if(parser.getTextLength() > maxStringLength) {
writeMaxStringLength(parser, generator, builder, maxStringLength, truncateStringValue);

if(metrics != null) {
metrics.onMaxStringLength(1);
}

continue;
}
}

generator.copyCurrentEvent(parser);
}
}

protected final JsonFactory jsonFactory;

public JacksonMaxStringLengthJsonFilter(int maxStringLength) {
Expand Down Expand Up @@ -132,6 +98,7 @@ public boolean process(char[] chars, int offset, int length, StringBuilder outpu
}
}

@Override
public boolean process(byte[] bytes, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
if(bytes.length < offset + length) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected JacksonPathMaxSizeMaxStringLengthJsonFilter(int maxStringLength, int m

public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics metrics) {
if(!mustConstrainMaxSize(length)) {
return super.process(chars, offset, length, output);
return super.process(chars, offset, length, output, metrics);
}
output.ensureCapacity(output.length() + length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.IOException;

import com.github.skjolber.jsonfilter.base.DefaultJsonFilterMetrics;
import com.github.skjolber.jsonfilter.test.DefaultJsonFilterTest;

public abstract class AbstractDefaultJacksonJsonFilterTest extends DefaultJsonFilterTest {
Expand All @@ -24,6 +25,9 @@ public void testConvenienceMethods(JacksonJsonFilter successFilter, JacksonJsonF
assertTrue(successFilter.process(jsonBytes, 0, 2, new StringBuilder()));
assertTrue(successFilter.process(jsonChars, 0, 2, new StringBuilder()));

assertTrue(successFilter.process(jsonBytes, new StringBuilder()));
assertTrue(successFilter.process(jsonBytes, new StringBuilder(), new DefaultJsonFilterMetrics()));

assertFalse(failureFilter.process(jsonBytes, 0, 2, new StringBuilder()));
assertFalse(failureFilter.process(jsonChars, 0, 2, new StringBuilder()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonFactory;
import com.github.skjolber.jsonfilter.ResizableByteArrayOutputStream;

public class DefaultJacksonJsonFilterTest {

@Test
Expand All @@ -14,5 +19,19 @@ public void testException() throws Exception {

assertNull(filter.process(new byte[] {}, 0, 100));
assertFalse(filter.process(new char[] {}, 0, 100, new StringBuilder()));

String illegalJson = "{abcdef}";
assertFalse(filter.process(illegalJson.toCharArray(), 0, 3, new StringBuilder()));
assertFalse(filter.process(illegalJson.getBytes(StandardCharsets.UTF_8), 0, 3, new ResizableByteArrayOutputStream(1024)));
}

@Test
public void testExceptionConstructor() throws Exception {

JsonFactory factory = new JsonFactory();
DefaultJacksonJsonFilter filter = new DefaultJacksonJsonFilter(factory);

assertNull(filter.process(new byte[] {}, 0, 100));
assertFalse(filter.process(new char[] {}, 0, 100, new StringBuilder()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.LongSupplier;

import org.apache.commons.io.output.StringBuilderWriter;
import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.github.skjolber.jsonfilter.JsonFilterMetrics;
import com.github.skjolber.jsonfilter.ResizableByteArrayOutputStream;

public class JacksonMaxSizeJsonFilterTest extends AbstractDefaultJacksonJsonFilterTest {

public JacksonMaxSizeJsonFilterTest() throws Exception {
Expand Down Expand Up @@ -41,4 +53,94 @@ public void maxSize() throws Exception {
assertThat(new JacksonMaxSizeJsonFilter(DEFAULT_MAX_SIZE)).hasMaxSize(DEFAULT_MAX_SIZE);
}

@Test
public void testConvenienceMethods() throws IOException {
JsonFactory jsonFactory = mock(JsonFactory.class);
when(jsonFactory.createGenerator(any(StringBuilderWriter.class))).thenThrow(new RuntimeException());
when(jsonFactory.createGenerator(any(ByteArrayOutputStream.class))).thenThrow(new RuntimeException());

testConvenienceMethods(
new JacksonMaxSizeJsonFilter(1) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return true;
}
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
return true;
}
},
new JacksonMaxSizeJsonFilter(1) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return false;
}
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
return false;
}
},
new JacksonMaxSizeJsonFilter(1, jsonFactory) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
}
@Override
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
throw new RuntimeException();
}
}
);

testConvenienceMethods(
new JacksonMaxSizeJsonFilter(1024) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return true;
}
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
return true;
}

@Override
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
return true;
}

public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
return true;
}
},
new JacksonMaxSizeJsonFilter(1024) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return false;
}
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
return false;
}

@Override
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
return false;
}

public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
return false;
}
},
new JacksonMaxSizeJsonFilter(1024, jsonFactory) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
}

@Override
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
throw new RuntimeException();
}


}
);
}

@Test
public void testConstructor() {
new JacksonMaxSizeJsonFilter(1024, "XXX", "YYY", "ZZZ");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.function.LongSupplier;

import org.apache.commons.io.output.StringBuilderWriter;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -41,24 +42,59 @@ public void maxStringLength() throws Exception {

@Test
public void testConvenienceMethods() throws IOException {

JsonFactory jsonFactory = mock(JsonFactory.class);
when(jsonFactory.createGenerator(any(StringBuilderWriter.class))).thenThrow(new RuntimeException());
when(jsonFactory.createGenerator(any(ByteArrayOutputStream.class))).thenThrow(new RuntimeException());

testConvenienceMethods(
new JacksonMaxStringLengthJsonFilter(-1) {
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return true;
}
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
return true;
}
},
new JacksonMaxStringLengthJsonFilter(-1) {
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return false;
}
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
return false;
}
},
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
}
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
}
}
);

testConvenienceMethods(
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return true;
}
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
return true;
}
},
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
return false;
}
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
return false;
}
},
new JacksonMaxStringLengthJsonFilter(-1, jsonFactory) {
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
}
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public boolean process(final JsonParser parser, JsonGenerator generator, JsonFil
},
new JacksonMaxStringLengthJsonFilter(-1) {
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
throw new RuntimeException();
return false;
}
},
new JacksonMaxStringLengthJsonFilter(-1, jsonFactory) {
Expand Down
Loading