|
| 1 | +/* |
| 2 | + * Copyright Cedar Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.cedarpolicy.serializer; |
| 18 | + |
| 19 | +import com.cedarpolicy.model.exception.InvalidValueDeserializationException; |
| 20 | +import com.cedarpolicy.value.Value; |
| 21 | +import com.cedarpolicy.value.EntityUID; |
| 22 | +import com.cedarpolicy.model.entity.Entity; |
| 23 | + |
| 24 | +import com.fasterxml.jackson.core.JsonParser; |
| 25 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 26 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 27 | +import com.fasterxml.jackson.databind.JsonNode; |
| 28 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.Spliterator; |
| 35 | +import java.util.Spliterators; |
| 36 | +import java.util.stream.Collectors; |
| 37 | +import java.util.stream.StreamSupport; |
| 38 | + |
| 39 | +/** |
| 40 | + * Deserialize Json to Entity. |
| 41 | + */ |
| 42 | +public class EntityDeserializer extends JsonDeserializer<Entity> { |
| 43 | + |
| 44 | + /** |
| 45 | + * Deserializes a JSON input into an Entity object. |
| 46 | + * |
| 47 | + * @param parser The JsonParser providing the JSON input to deserialize |
| 48 | + * @param context The deserialization context |
| 49 | + * |
| 50 | + * @return An Entity object constructed from the JSON input |
| 51 | + * @throws IOException If there is an error reading from the JsonParser |
| 52 | + * @throws InvalidValueDeserializationException If the JSON input is invalid or missing required fields |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public Entity deserialize(JsonParser parser, DeserializationContext context) |
| 56 | + throws IOException, InvalidValueDeserializationException { |
| 57 | + final JsonNode node = parser.getCodec().readTree(parser); |
| 58 | + final ObjectMapper mapper = (ObjectMapper) parser.getCodec(); |
| 59 | + |
| 60 | + EntityUID euid; |
| 61 | + if (node.has("uid")) { |
| 62 | + JsonNode uidNode = node.get("uid"); |
| 63 | + euid = parseEntityUID(parser, uidNode); |
| 64 | + } else { |
| 65 | + String msg = "\"uid\" not found"; |
| 66 | + throw new InvalidValueDeserializationException(parser, msg, node.asToken(), Entity.class); |
| 67 | + } |
| 68 | + |
| 69 | + Map<String, Value> attrs; |
| 70 | + if (node.has("attrs")) { |
| 71 | + JsonNode attrsNode = node.get("attrs"); |
| 72 | + if (attrsNode.isObject()) { |
| 73 | + attrs = parseValueMap(mapper, attrsNode); |
| 74 | + } else { |
| 75 | + String msg = "\"attrs\" must be a JSON object"; |
| 76 | + throw new InvalidValueDeserializationException(parser, msg, attrsNode.asToken(), Entity.class); |
| 77 | + } |
| 78 | + } else { |
| 79 | + String msg = "\"attrs\" not found"; |
| 80 | + throw new InvalidValueDeserializationException(parser, msg, node.asToken(), Entity.class); |
| 81 | + } |
| 82 | + |
| 83 | + Set<EntityUID> parentEUIDs; |
| 84 | + if (node.has("parents")) { |
| 85 | + JsonNode parentsNode = node.get("parents"); |
| 86 | + if (parentsNode.isArray()) { |
| 87 | + parentEUIDs = StreamSupport.stream(parentsNode.spliterator(), false).map(parentNode -> { |
| 88 | + try { |
| 89 | + return parseEntityUID(parser, parentNode); |
| 90 | + } catch (InvalidValueDeserializationException e) { |
| 91 | + throw new RuntimeException(e); |
| 92 | + } |
| 93 | + }).collect(Collectors.toSet()); |
| 94 | + } else { |
| 95 | + String msg = "\"parents\" field must be a JSON array"; |
| 96 | + throw new InvalidValueDeserializationException(parser, msg, parentsNode.asToken(), Entity.class); |
| 97 | + } |
| 98 | + } else { |
| 99 | + String msg = "\"parents\" not found"; |
| 100 | + throw new InvalidValueDeserializationException(parser, msg, node.asToken(), Entity.class); |
| 101 | + } |
| 102 | + |
| 103 | + Map<String, Value> tags = new HashMap<>(); |
| 104 | + if (node.has("tags")) { |
| 105 | + JsonNode tagsNode = node.get("tags"); |
| 106 | + if (tagsNode.isObject()) { |
| 107 | + tags.putAll(parseValueMap(mapper, tagsNode)); |
| 108 | + } else { |
| 109 | + String msg = "\"tags\" must be a JSON object"; |
| 110 | + throw new InvalidValueDeserializationException(parser, msg, tagsNode.asToken(), Entity.class); |
| 111 | + } |
| 112 | + } |
| 113 | + return new Entity(euid, attrs, parentEUIDs, tags); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Parses a JSON node into an EntityUID object. |
| 118 | + * |
| 119 | + * @param parser The JsonParser used for error reporting |
| 120 | + * @param entityUIDJson The JsonNode containing the entity UID data to parse. Must have "type" and "id" fields. |
| 121 | + * |
| 122 | + * @return An EntityUID object constructed from the JSON data |
| 123 | + * @throws InvalidValueDeserializationException if the required fields are missing or invalid |
| 124 | + */ |
| 125 | + private EntityUID parseEntityUID(JsonParser parser, JsonNode entityUIDJson) |
| 126 | + throws InvalidValueDeserializationException { |
| 127 | + if (entityUIDJson.has("type") && entityUIDJson.has("id")) { |
| 128 | + JsonEUID jsonEuid = new JsonEUID(entityUIDJson.get("type").asText(), entityUIDJson.get("id").asText()); |
| 129 | + return EntityUID.parseFromJson(jsonEuid).get(); |
| 130 | + } else { |
| 131 | + String msg = "\"type\" or \"id\" not found"; |
| 132 | + throw new InvalidValueDeserializationException(parser, msg, entityUIDJson.asToken(), Entity.class); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Parses a JSON node containing key-value pairs into a Map of String to Value objects. |
| 138 | + * |
| 139 | + * @param mapper The ObjectMapper used to convert JSON nodes to Value objects |
| 140 | + * @param valueMapJson The JsonNode containing the key-value pairs to parse |
| 141 | + * |
| 142 | + * @return A Map where keys are Strings and values are Value objects |
| 143 | + * @throws RuntimeException if there is an error converting a JSON node to a Value |
| 144 | + */ |
| 145 | + private Map<String, Value> parseValueMap(ObjectMapper mapper, JsonNode valueMapJson) { |
| 146 | + Map<String, Value> valueMap = StreamSupport |
| 147 | + .stream(Spliterators.spliteratorUnknownSize(valueMapJson.fields(), Spliterator.ORDERED), false) |
| 148 | + .collect(Collectors.toMap(Map.Entry::getKey, entry -> { |
| 149 | + try { |
| 150 | + return mapper.treeToValue(entry.getValue(), Value.class); |
| 151 | + } catch (IOException e) { |
| 152 | + throw new RuntimeException(e); |
| 153 | + } |
| 154 | + })); |
| 155 | + return valueMap; |
| 156 | + } |
| 157 | +} |
0 commit comments