use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project carbon-apimgt by wso2.
the class JSONAnalyzer method analyze.
/**
* Analyze the JSON payload against limitations.
* @param in input stream of the request payload.
* @param apiContext request api context.
* @throws APIMThreatAnalyzerException if defined limits for json payload exceeds
*/
@Override
public void analyze(InputStream in, String apiContext) throws APIMThreatAnalyzerException {
try (JsonParser parser = factory.createParser(new InputStreamReader(in))) {
int currentDepth = 0;
int currentFieldCount = 0;
JsonToken token;
while ((token = parser.nextToken()) != null) {
switch(token) {
case START_OBJECT:
currentDepth += 1;
analyzeDepth(maxJsonDepth, currentDepth, apiContext);
break;
case END_OBJECT:
currentDepth -= 1;
break;
case FIELD_NAME:
currentFieldCount += 1;
String name = parser.getCurrentName();
analyzeField(name, maxFieldCount, currentFieldCount, maxFieldLength);
break;
case VALUE_STRING:
String value = parser.getText();
analyzeString(value, maxStringLength);
break;
case START_ARRAY:
analyzeArray(parser, maxArrayElementCount, maxStringLength);
}
}
} catch (JsonParseException e) {
throw new APIMThreatAnalyzerException("Error occurred while parsing the JSON payload", e);
} catch (IOException e) {
throw new APIMThreatAnalyzerException("Error occurred while reading the JSON payload.", e);
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project carbon-apimgt by wso2.
the class JSONAnalyzer method analyzeArray.
/**
* Analyzes json arrays using defined limits
*
* @param parser JsonParser instance (Current token should be at JsonToken.START_ARRAY state)
* @param maxArrayElementCount maximum array element count allowed
* @param maxStringLength maximum string length allowed
* @throws APIMThreatAnalyzerException if array/string length is greater than maximum values provided
*/
private void analyzeArray(JsonParser parser, int maxArrayElementCount, int maxStringLength) throws APIMThreatAnalyzerException {
JsonToken token;
try {
int arrayElementCount = 0;
while ((token = parser.nextToken()) != JsonToken.END_ARRAY) {
// analyzing string values inside the array
if (token == JsonToken.VALUE_STRING) {
String value = parser.getText();
analyzeString(value, maxStringLength);
}
arrayElementCount += 1;
if (arrayElementCount > maxArrayElementCount) {
throw new APIMThreatAnalyzerException(" Max Array Length [" + maxArrayElementCount + "] Reached");
}
}
} catch (IOException e) {
throw new APIMThreatAnalyzerException("Array Parsing Error", e);
}
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project knox by apache.
the class JsonFilterReader method read.
@Override
public int read(char[] destBuffer, int destOffset, int destCount) throws IOException {
int count = 0;
int available = buffer.length() - offset;
if (available == 0) {
JsonToken token = parser.nextToken();
if (token == null) {
count = -1;
} else {
processCurrentToken();
available = buffer.length() - offset;
}
}
if (available > 0) {
count = Math.min(destCount, available);
buffer.getChars(offset, offset + count, destBuffer, destOffset);
offset += count;
if (offset == buffer.length()) {
offset = 0;
buffer.setLength(0);
}
}
return count;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project cyclops by aol.
the class IterableXDeserializer method deserialize.
@Override
public IterableX<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (!p.isExpectedStartArrayToken()) {
return (IterableX) ctxt.handleUnexpectedToken(handledType(), p);
}
List multi = new ArrayList();
JsonToken t;
while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
} else if (typeDeserializerForValue == null) {
value = valueDeserializer.deserialize(p, ctxt);
} else {
value = valueDeserializer.deserializeWithType(p, ctxt, typeDeserializerForValue);
}
multi.add(value);
}
if (Vector.class.isAssignableFrom(elementType))
return Vector.fromIterable(multi);
if (Seq.class.isAssignableFrom(elementType))
return Seq.fromIterable(multi);
if (LazySeq.class.isAssignableFrom(elementType))
return LazySeq.fromIterable(multi);
if (LazyString.class.isAssignableFrom(elementType))
return LazyString.fromLazySeq((LazySeq) LazySeq.fromIterable(multi));
if (IntMap.class.isAssignableFrom(elementType))
return IntMap.fromIterable(multi);
if (ReactiveSeq.class.isAssignableFrom(elementType))
return ReactiveSeq.fromIterable(multi);
if (Streamable.class.isAssignableFrom(elementType))
return Streamable.fromIterable(multi);
if (BankersQueue.class.isAssignableFrom(elementType))
return BankersQueue.fromIterable(multi);
if (Bag.class.isAssignableFrom(elementType))
return Bag.fromIterable(multi);
if (cyclops.data.HashSet.class.isAssignableFrom(elementType))
return HashSet.fromIterable(multi);
if (cyclops.data.TrieSet.class.isAssignableFrom(elementType))
return TrieSet.fromIterable(multi);
if (cyclops.data.TreeSet.class.isAssignableFrom(elementType))
return TreeSet.fromIterable(multi, (Comparator) Comparator.naturalOrder());
Optional<Method> m = streamMethod.computeIfAbsent(elementType, c -> Stream.of(c.getMethods()).filter(method -> "fromIterable".equals(method.getName())).filter(method -> method.getParameterCount() == 1).findFirst().map(m2 -> {
m2.setAccessible(true);
return m2;
}));
IterableX x = m.map(mt -> (IterableX) new Invoker().executeMethod(multi, mt, itX)).orElse(null);
return x;
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonToken in project spf4j by zolyfarkas.
the class SampleNode method parseD3Json.
// not that weak here...
@SuppressFBWarnings("WEM_WEAK_EXCEPTION_MESSAGING")
private static void parseD3Json(final JsonParser jsonP, final BiConsumer<Method, SampleNode> consumer) throws IOException {
String methodName = null;
SampleNode sn = new SampleNode(-1);
while (true) {
JsonToken nextToken = jsonP.nextToken();
if (nextToken == JsonToken.FIELD_NAME) {
String fieldName = jsonP.getCurrentName();
switch(fieldName) {
case "name":
consume(jsonP, JsonToken.VALUE_STRING);
methodName = jsonP.getText();
break;
case "value":
consume(jsonP, JsonToken.VALUE_NUMBER_INT);
sn.sampleCount = jsonP.getIntValue();
break;
case "children":
consume(jsonP, JsonToken.START_ARRAY);
while (jsonP.nextToken() != JsonToken.END_ARRAY) {
parseD3Json(jsonP, sn::put);
}
break;
default:
throw new JsonParseException(jsonP, "Unexpected field name : " + fieldName);
}
} else if (nextToken == JsonToken.END_OBJECT) {
if (methodName == null) {
throw new JsonParseException(jsonP, "name field not found");
}
if (sn.sampleCount < 0) {
throw new JsonParseException(jsonP, "value field not found");
}
consumer.accept(Methods.from(methodName), sn);
return;
} else {
throw new JsonParseException(jsonP, "Unexpected " + nextToken);
}
}
}
Aggregations