Skip to content

Commit 145d139

Browse files
committed
Improve jackson module
1 parent 14ce16e commit 145d139

9 files changed

+174
-22
lines changed

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/DefaultJacksonJsonFilter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
package com.github.skjolber.jsonfilter.jackson;
2+
import java.nio.charset.StandardCharsets;
3+
24
import com.fasterxml.jackson.core.JsonFactory;
35
import com.fasterxml.jackson.core.JsonParser;
46
import com.github.skjolber.jsonfilter.JsonFilterMetrics;
@@ -45,6 +47,19 @@ public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayO
4547
return false;
4648
}
4749
}
50+
51+
public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
52+
try (JsonParser parser = jsonFactory.createParser(chars, offset, length)) {
53+
if(parse(parser)) {
54+
output.ensureCapacity(output.length() + length);
55+
output.append(new String(chars, offset, length, StandardCharsets.UTF_8));
56+
return true;
57+
}
58+
return false;
59+
} catch(final Exception e) {
60+
return false;
61+
}
62+
}
4863

4964
protected boolean parse(JsonParser parser) {
5065
try {

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeJsonFilter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public boolean process(byte[] bytes, int offset, int length, StringBuilder outpu
7272
}
7373

7474
if(maxSize >= length) {
75-
output.append(new String(bytes, offset, length));
76-
return true;
75+
return super.process(bytes, offset, length, output, metrics);
7776
}
7877
output.ensureCapacity(output.length() + length);
7978

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeMaxStringLengthJsonFilter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public JacksonMaxSizeMaxStringLengthJsonFilter(int maxStringLength, int maxSize,
3030
super(maxStringLength, maxSize, pruneMessage, anonymizeMessage, truncateMessage, jsonFactory);
3131
}
3232

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

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

67+
@Override
6568
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics metrics) {
6669
if(maxSize >= length) {
6770
return super.process(bytes, offset, length, output, metrics);

frameworks/jackson/src/main/java/com/github/skjolber/jsonfilter/jackson/JacksonPathMaxSizeMaxStringLengthJsonFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected JacksonPathMaxSizeMaxStringLengthJsonFilter(int maxStringLength, int m
3232

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

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/AbstractDefaultJacksonJsonFilterTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.io.IOException;
1010

11+
import com.github.skjolber.jsonfilter.base.DefaultJsonFilterMetrics;
1112
import com.github.skjolber.jsonfilter.test.DefaultJsonFilterTest;
1213

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

28+
assertTrue(successFilter.process(jsonBytes, new StringBuilder()));
29+
assertTrue(successFilter.process(jsonBytes, new StringBuilder(), new DefaultJsonFilterMetrics()));
30+
2731
assertFalse(failureFilter.process(jsonBytes, 0, 2, new StringBuilder()));
2832
assertFalse(failureFilter.process(jsonChars, 0, 2, new StringBuilder()));
2933

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeJsonFilterTest.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.fasterxml.jackson.core.JsonGenerator;
1919
import com.fasterxml.jackson.core.JsonParser;
2020
import com.github.skjolber.jsonfilter.JsonFilterMetrics;
21+
import com.github.skjolber.jsonfilter.ResizableByteArrayOutputStream;
2122

2223
public class JacksonMaxSizeJsonFilterTest extends AbstractDefaultJacksonJsonFilterTest {
2324

@@ -61,20 +62,85 @@ public void testConvenienceMethods() throws IOException {
6162
testConvenienceMethods(
6263
new JacksonMaxSizeJsonFilter(1) {
6364
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
64-
return true;
65+
return true;
66+
}
67+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
68+
return true;
6569
}
6670
},
6771
new JacksonMaxSizeJsonFilter(1) {
6872
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
6973
return false;
7074
}
75+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
76+
return false;
77+
}
7178
},
7279
new JacksonMaxSizeJsonFilter(1, jsonFactory) {
7380
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
7481
throw new RuntimeException();
7582
}
83+
@Override
84+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
85+
throw new RuntimeException();
86+
}
7687
}
7788
);
89+
90+
testConvenienceMethods(
91+
new JacksonMaxSizeJsonFilter(1024) {
92+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
93+
return true;
94+
}
95+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
96+
return true;
97+
}
98+
99+
@Override
100+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
101+
return true;
102+
}
103+
104+
public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
105+
return true;
106+
}
107+
},
108+
new JacksonMaxSizeJsonFilter(1024) {
109+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
110+
return false;
111+
}
112+
public boolean process(char[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
113+
return false;
114+
}
115+
116+
@Override
117+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
118+
return false;
119+
}
120+
121+
public boolean process(byte[] chars, int offset, int length, StringBuilder output, JsonFilterMetrics filterMetrics) {
122+
return false;
123+
}
124+
},
125+
new JacksonMaxSizeJsonFilter(1024, jsonFactory) {
126+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
127+
throw new RuntimeException();
128+
}
129+
130+
@Override
131+
public boolean process(byte[] bytes, int offset, int length, ResizableByteArrayOutputStream output, JsonFilterMetrics filterMetrics) {
132+
throw new RuntimeException();
133+
}
134+
135+
136+
}
137+
);
78138
}
79139

140+
@Test
141+
public void testConstructor() {
142+
new JacksonMaxSizeJsonFilter(1024, "XXX", "YYY", "ZZZ");
143+
}
144+
145+
80146
}

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/JacksonMaxSizeMaxStringLengthJsonFilterTest.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.ByteArrayOutputStream;
88
import java.io.IOException;
9+
import java.util.function.LongSupplier;
910

1011
import org.apache.commons.io.output.StringBuilderWriter;
1112
import org.junit.jupiter.api.Test;
@@ -46,18 +47,54 @@ public void testConvenienceMethods() throws IOException {
4647
when(jsonFactory.createGenerator(any(ByteArrayOutputStream.class))).thenThrow(new RuntimeException());
4748

4849
testConvenienceMethods(
49-
new JacksonMaxStringLengthJsonFilter(-1) {
50-
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
50+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
51+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
52+
return true;
53+
}
54+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
55+
return true;
56+
}
57+
},
58+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
59+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
60+
return false;
61+
}
62+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
63+
return false;
64+
}
65+
},
66+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1) {
67+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
68+
throw new RuntimeException();
69+
}
70+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
71+
throw new RuntimeException();
72+
}
73+
}
74+
);
75+
76+
testConvenienceMethods(
77+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
78+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
79+
return true;
80+
}
81+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
5182
return true;
5283
}
5384
},
54-
new JacksonMaxStringLengthJsonFilter(-1) {
55-
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
85+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
86+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
87+
return false;
88+
}
89+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
5690
return false;
5791
}
5892
},
59-
new JacksonMaxStringLengthJsonFilter(-1, jsonFactory) {
60-
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) {
93+
new JacksonMaxSizeMaxStringLengthJsonFilter(512, 1024) {
94+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
95+
throw new RuntimeException();
96+
}
97+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
6198
throw new RuntimeException();
6299
}
63100
}

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/JacksonPathMaxSizeMaxStringLengthJsonFilterTest.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.ByteArrayOutputStream;
88
import java.io.IOException;
9+
import java.util.function.LongSupplier;
910

1011
import org.apache.commons.io.output.StringBuilderWriter;
1112
import org.junit.jupiter.api.Test;
@@ -137,19 +138,55 @@ public void testConvenienceMethods() throws IOException {
137138
when(jsonFactory.createGenerator(any(ByteArrayOutputStream.class))).thenThrow(new RuntimeException());
138139

139140
testConvenienceMethods(
140-
new JacksonPathMaxStringLengthJsonFilter(-1, null, null) {
141+
new JacksonPathMaxSizeMaxStringLengthJsonFilter(-1, 1024, null, null) {
141142
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
142143
return true;
143144
}
145+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
146+
return true;
147+
}
144148
},
145-
new JacksonPathMaxStringLengthJsonFilter(-1, null, null) {
149+
new JacksonPathMaxSizeMaxStringLengthJsonFilter(-1, 1024, null, null) {
146150
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
147151
return false;
148152
}
153+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
154+
return false;
155+
}
149156
},
150-
new JacksonPathMaxStringLengthJsonFilter(-1, null, null, jsonFactory) {
157+
new JacksonPathMaxSizeMaxStringLengthJsonFilter(-1, 1024, null, null) {
151158
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
152159
throw new RuntimeException();
160+
}
161+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
162+
throw new RuntimeException();
163+
}
164+
}
165+
);
166+
167+
testConvenienceMethods(
168+
new JacksonPathMaxSizeMaxStringLengthJsonFilter(-1, 1, null, null) {
169+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
170+
return true;
171+
}
172+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
173+
return true;
174+
}
175+
},
176+
new JacksonPathMaxSizeMaxStringLengthJsonFilter(-1, 1, null, null) {
177+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
178+
return false;
179+
}
180+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
181+
return false;
182+
}
183+
},
184+
new JacksonPathMaxSizeMaxStringLengthJsonFilter(-1, 1, null, null) {
185+
public boolean process(final JsonParser parser, JsonGenerator generator, JsonFilterMetrics metrics) throws IOException {
186+
throw new RuntimeException();
187+
}
188+
public boolean process(final JsonParser parser, JsonGenerator generator, LongSupplier offsetSupplier, LongSupplier outputSizeSupplier, JsonFilterMetrics metrics) throws IOException {
189+
throw new RuntimeException();
153190
}
154191
}
155192
);

frameworks/jackson/src/test/java/com/github/skjolber/jsonfilter/jackson/JacksonPathMaxStringLengthJsonFilterTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,4 @@ public boolean process(final JsonParser parser, JsonGenerator generator, JsonFil
101101
);
102102
}
103103

104-
@Test
105-
public void byteInputStringBuilderOutput() throws Exception {
106-
JacksonPathMaxStringLengthJsonFilter filter = new JacksonPathMaxStringLengthJsonFilter(-1, null, null);
107-
108-
StringBuilder stringBuilder = new StringBuilder();
109-
assertTrue(filter.process("{}", stringBuilder));
110-
assertFalse(filter.process("{abcdef}", stringBuilder));
111-
}
112-
113104
}

0 commit comments

Comments
 (0)