Search in sources :

Example 31 with JsonToken

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) APIMThreatAnalyzerException(org.wso2.carbon.apimgt.gateway.threatprotection.APIMThreatAnalyzerException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 32 with JsonToken

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);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) APIMThreatAnalyzerException(org.wso2.carbon.apimgt.gateway.threatprotection.APIMThreatAnalyzerException)

Example 33 with JsonToken

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;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 34 with JsonToken

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;
}
Also used : MethodHandle(java.lang.invoke.MethodHandle) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) ArrayList(java.util.ArrayList) StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) Map(java.util.Map) JsonToken(com.fasterxml.jackson.core.JsonToken) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) Method(java.lang.reflect.Method) ConstantCallSite(java.lang.invoke.ConstantCallSite) CollectionLikeType(com.fasterxml.jackson.databind.type.CollectionLikeType) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) ContextualDeserializer(com.fasterxml.jackson.databind.deser.ContextualDeserializer) JsonParser(com.fasterxml.jackson.core.JsonParser) AsArrayTypeDeserializer(com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer) cyclops.data(cyclops.data) BeanProperty(com.fasterxml.jackson.databind.BeanProperty) MethodHandles(java.lang.invoke.MethodHandles) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ReactiveSeq(cyclops.reactive.ReactiveSeq) List(java.util.List) Stream(java.util.stream.Stream) CallSite(java.lang.invoke.CallSite) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) Optional(java.util.Optional) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectArrayDeserializer(com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer) Comparator(java.util.Comparator) Streamable(cyclops.companion.Streamable) cyclops.data(cyclops.data) IterableX(com.oath.cyclops.types.traversable.IterableX) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) JsonToken(com.fasterxml.jackson.core.JsonToken) Method(java.lang.reflect.Method) Comparator(java.util.Comparator)

Example 35 with JsonToken

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);
        }
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) JsonParseException(com.fasterxml.jackson.core.JsonParseException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)251 IOException (java.io.IOException)65 JsonParser (com.fasterxml.jackson.core.JsonParser)44 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 JsonFactory (com.fasterxml.jackson.core.JsonFactory)18 JsonParseException (com.fasterxml.jackson.core.JsonParseException)15 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 SqlNullable (com.facebook.presto.spi.function.SqlNullable)6 SqlType (com.facebook.presto.spi.function.SqlType)6 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)6 JsonParserHelper.assertExpectedJsonToken (com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken)5 InputStream (java.io.InputStream)5 LinkedHashMap (java.util.LinkedHashMap)5 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ByteString (com.google.protobuf.ByteString)4 HashSet (java.util.HashSet)4 MappingJsonFactory (com.fasterxml.jackson.databind.MappingJsonFactory)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3