Skip to content
Closed
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
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 @@ -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,23 @@

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;

public class JacksonMaxSizeJsonFilterTest extends AbstractDefaultJacksonJsonFilterTest {

public JacksonMaxSizeJsonFilterTest() throws Exception {
Expand Down Expand Up @@ -41,4 +52,29 @@ 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;
}
},
new JacksonMaxSizeJsonFilter(1) {
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
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();
}
}
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ 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());
Expand All @@ -54,7 +53,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
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
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public boolean process(final JsonParser parser, JsonGenerator generator, JsonFil
},
new JacksonPathMaxStringLengthJsonFilter(-1, null, null) {
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
return false;
}
},
new JacksonPathMaxStringLengthJsonFilter(-1, null, null, jsonFactory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.skjolber.jsonfilter.jackson;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -88,7 +90,7 @@ public boolean process(final JsonParser parser, JsonGenerator generator, JsonFil
},
new JacksonPathMaxStringLengthJsonFilter(-1, null, null) {
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
throw new RuntimeException();
return false;
}
},
new JacksonPathMaxStringLengthJsonFilter(-1, null, null, jsonFactory) {
Expand All @@ -99,4 +101,13 @@ public boolean process(final JsonParser parser, JsonGenerator generator, JsonFil
);
}

@Test
public void byteInputStringBuilderOutput() throws Exception {
JacksonPathMaxStringLengthJsonFilter filter = new JacksonPathMaxStringLengthJsonFilter(-1, null, null);

StringBuilder stringBuilder = new StringBuilder();
assertTrue(filter.process("{}", stringBuilder));
assertFalse(filter.process("{abcdef}", stringBuilder));
}

}