Search in sources :

Example 61 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project carbon-apimgt by wso2.

the class JSONAnalyzer method analyze.

/**
 * @param payload json payload
 * @throws APIMThreatAnalyzerException if defined limits for json payload exceeds
 */
@Override
public void analyze(String payload, String apiContext) throws APIMThreatAnalyzerException {
    try (JsonParser parser = factory.createParser(new StringReader(payload))) {
        int currentDepth = 0;
        int currentFieldCount = 0;
        JsonToken token;
        while ((token = parser.nextToken()) != null) {
            switch(token) {
                case START_OBJECT:
                    currentDepth += 1;
                    try {
                        analyzeDepth(maxJsonDepth, currentDepth, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case END_OBJECT:
                    currentDepth -= 1;
                    break;
                case FIELD_NAME:
                    currentFieldCount += 1;
                    String name = parser.getCurrentName();
                    try {
                        analyzeField(name, maxFieldCount, currentFieldCount, maxFieldLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case VALUE_STRING:
                    String value = parser.getText();
                    try {
                        analyzeString(value, maxStringLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
                case START_ARRAY:
                    try {
                        analyzeArray(parser, maxArrayElementCount, maxStringLength, apiContext);
                    } catch (APIMThreatAnalyzerException e) {
                        throw e;
                    }
                    break;
            }
        }
    } catch (JsonParseException e) {
        logger.error(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload parsing failed", e);
        throw new APIMThreatAnalyzerException(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload parsing failed", e);
    } catch (IOException e) {
        logger.error(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload build failed", e);
        throw new APIMThreatAnalyzerException(JSON_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Payload build failed", e);
    }
}
Also used : StringReader(java.io.StringReader) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) APIMThreatAnalyzerException(org.wso2.carbon.apimgt.ballerina.threatprotection.APIMThreatAnalyzerException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 62 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project atlasmap by atlasmap.

the class ActionsJsonDeserializer method processFormatJsonToken.

protected Format processFormatJsonToken(JsonParser jsonToken) throws IOException {
    Format action = new Format();
    if (JsonToken.END_ARRAY.equals(jsonToken.currentToken()) || JsonToken.END_OBJECT.equals(jsonToken.currentToken())) {
        return action;
    }
    JsonToken nextToken = null;
    do {
        if (JsonToken.START_OBJECT.equals(jsonToken.currentToken())) {
            jsonToken.nextToken();
        }
        switch(jsonToken.getCurrentName()) {
            case ActionsJsonSerializer.TEMPLATE:
                jsonToken.nextToken();
                action.setTemplate(jsonToken.getValueAsString());
                break;
            default:
                break;
        }
        nextToken = jsonToken.nextToken();
    } while (!JsonToken.END_ARRAY.equals(nextToken) && !JsonToken.END_OBJECT.equals(nextToken));
    return action;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 63 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project atlasmap by atlasmap.

the class ActionsJsonDeserializer method deserialize.

@Override
public Actions deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    Actions actions = null;
    if (jp != null && jp.isExpectedStartArrayToken()) {
        actions = new Actions();
        jp.nextToken();
        while (jp.nextToken() != JsonToken.END_ARRAY) {
            JsonToken jsonToken = jp.nextToken();
            if (jsonToken == JsonToken.END_ARRAY) {
                break;
            }
            Action action = processActionJsonToken(jp);
            if (action != null) {
                actions.getActions().add(action);
            }
        }
    } else {
        throw new IOException("Invalid JSON where array expected: " + (jp != null ? jp.getCurrentToken().asString() : null));
    }
    return actions;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException)

Example 64 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project atlasmap by atlasmap.

the class ActionsJsonDeserializer method processEndsWithJsonToken.

protected EndsWith processEndsWithJsonToken(JsonParser jsonToken) throws IOException {
    EndsWith action = new EndsWith();
    if (JsonToken.END_ARRAY.equals(jsonToken.currentToken()) || JsonToken.END_OBJECT.equals(jsonToken.currentToken())) {
        return action;
    }
    JsonToken nextToken = null;
    do {
        if (JsonToken.START_OBJECT.equals(jsonToken.currentToken())) {
            jsonToken.nextToken();
        }
        switch(jsonToken.getCurrentName()) {
            case ActionsJsonSerializer.STRING:
                jsonToken.nextToken();
                action.setString(jsonToken.getValueAsString());
                break;
            default:
                break;
        }
        nextToken = jsonToken.nextToken();
    } while (!JsonToken.END_ARRAY.equals(nextToken) && !JsonToken.END_OBJECT.equals(nextToken));
    return action;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Example 65 with JsonToken

use of com.fasterxml.jackson.core.JsonToken in project atlasmap by atlasmap.

the class ActionsJsonDeserializer method processPadStringRightJsonToken.

protected PadStringRight processPadStringRightJsonToken(JsonParser jsonToken) throws IOException {
    PadStringRight action = new PadStringRight();
    if (JsonToken.END_ARRAY.equals(jsonToken.currentToken()) || JsonToken.END_OBJECT.equals(jsonToken.currentToken())) {
        return action;
    }
    JsonToken nextToken = null;
    do {
        if (JsonToken.START_OBJECT.equals(jsonToken.currentToken())) {
            jsonToken.nextToken();
        }
        switch(jsonToken.getCurrentName()) {
            case ActionsJsonSerializer.PAD_CHARACTER:
                jsonToken.nextToken();
                action.setPadCharacter(jsonToken.getValueAsString());
                break;
            case ActionsJsonSerializer.PAD_COUNT:
                jsonToken.nextToken();
                action.setPadCount(jsonToken.getIntValue());
                break;
            default:
                break;
        }
        nextToken = jsonToken.nextToken();
    } while (!JsonToken.END_ARRAY.equals(nextToken) && !JsonToken.END_OBJECT.equals(nextToken));
    return action;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken)

Aggregations

JsonToken (com.fasterxml.jackson.core.JsonToken)72 JsonParser (com.fasterxml.jackson.core.JsonParser)14 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)8 SqlNullable (com.facebook.presto.spi.function.SqlNullable)7 SqlType (com.facebook.presto.spi.function.SqlType)7 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)7 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 JsonParserHelper.assertExpectedJsonToken (com.alibaba.json.test.performance.JacksonPageModelParser.JsonParserHelper.assertExpectedJsonToken)5 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)2 RpcHint (com.navercorp.pinpoint.web.filter.RpcHint)2 InputStream (java.io.InputStream)2 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)2 Company (com.alibaba.json.test.entity.Company)1 Department (com.alibaba.json.test.entity.Department)1 Employee (com.alibaba.json.test.entity.Employee)1 Group (com.alibaba.json.test.entity.Group)1 LayoutInstance (com.alibaba.json.test.entity.pagemodel.LayoutInstance)1 PageInstance (com.alibaba.json.test.entity.pagemodel.PageInstance)1