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);
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations