in rest/src/main/java/org/apache/unomi/rest/deserializers/ContextRequestDeserializer.java [55:130]
public ContextRequest deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
// Validate schema on it
if (!schemaService.isValid(node.toString(), "https://unomi.apache.org/schemas/json/rest/requestIds/1-0-0")) {
throw new InvalidRequestException("Invalid Context request object", "Invalid received data");
}
ContextRequest cr = new ContextRequest();
if (node.get("source") != null) {
cr.setSource(jsonParser.getCodec().treeToValue(node.get("source"), Item.class));
}
if (node.get("requireSegments") != null) {
cr.setRequireSegments(node.get("requireSegments").booleanValue());
}
final JsonNode requiredProfileProperties = node.get("requiredProfileProperties");
if (requiredProfileProperties instanceof ArrayNode) {
List<String> profileProperties = new ArrayList<>();
requiredProfileProperties.elements().forEachRemaining(el -> profileProperties.add(el.textValue()));
cr.setRequiredProfileProperties(profileProperties);
}
final JsonNode requiredSessionPropertiesNode = node.get("requiredSessionProperties");
if (requiredSessionPropertiesNode instanceof ArrayNode) {
List<String> requiredSessionProperties = new ArrayList<>();
requiredSessionPropertiesNode.elements().forEachRemaining(el -> requiredSessionProperties.add(el.textValue()));
cr.setRequiredSessionProperties(requiredSessionProperties);
}
if (node.get("requireScores") != null) {
cr.setRequireScores(node.get("requireScores").booleanValue());
}
final JsonNode eventsNode = node.get("events");
if (eventsNode instanceof ArrayNode) {
cr.setEvents(HttpUtils.filterValidEvents((ArrayNode) eventsNode, schemaService, jsonParser));
}
final JsonNode filtersNode = node.get("filters");
if (filtersNode instanceof ArrayNode) {
ArrayNode filters = (ArrayNode) filtersNode;
List<PersonalizationService.PersonalizedContent> f = new ArrayList<>();
filters.elements().forEachRemaining(el -> {
try {
f.add(jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizedContent.class));
} catch (JsonProcessingException e) {
// Unable to deserialize, ignore the entry
}
});
cr.setFilters(f);
}
final JsonNode personalizationsNode = node.get("personalizations");
if (personalizationsNode instanceof ArrayNode) {
ArrayNode personalizations = (ArrayNode) personalizationsNode;
List<PersonalizationService.PersonalizationRequest> p = new ArrayList<>();
personalizations.elements().forEachRemaining(el -> {
try {
p.add(jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizationRequest.class));
} catch (JsonProcessingException e) {
// Unable to deserialize, ignore the entry
}
});
cr.setPersonalizations(p);
}
if (node.get("profileOverrides") != null) {
cr.setProfileOverrides(jsonParser.getCodec().treeToValue(node.get("profileOverrides"), Profile.class));
}
if (node.get("sessionPropertiesOverrides") != null) {
cr.setSessionPropertiesOverrides(jsonParser.getCodec().treeToValue(node.get("sessionPropertiesOverrides"), Map.class));
}
if (node.get("sessionId") != null) {
cr.setSessionId(node.get("sessionId").textValue());
}
if (node.get("profileId") != null) {
cr.setProfileId(node.get("profileId").textValue());
}
if (node.get("clientId") != null) {
cr.setClientId(node.get("clientId").textValue());
}
return cr;
}