Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@

import com.cedarpolicy.value.EntityUID;
import com.cedarpolicy.value.Value;
import com.cedarpolicy.model.exception.InternalException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -29,6 +37,8 @@
* entities, and zero or more tags.
*/
public class Entity {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

private final EntityUID euid;

/** Key/Value attribute map. */
Expand Down Expand Up @@ -66,6 +76,45 @@ public Entity(EntityUID uid, Map<String, Value> attributes, Set<EntityUID> paren
this.tags = new HashMap<>(tags);
}

/**
* Get the Entity's JSON string value.
*
* @return the Entity's JSON string value
* @throws NullPointerException if the Entity is null
* @throws InternalException if the Entity is unable to be parsed
*/
public String toJsonString() throws NullPointerException, InternalException {
return toJsonEntityJni(this);
}

/**
* Get the Entity's JSON value.
*
* @return the Entity's JSON value
* @throws NullPointerException if the Entity is null
* @throws InternalException if the Entity is unable to be parsed
* @throws JsonProcessingException if the Entity JSON is unable to be processed
*/
public JsonNode toJsonValue() throws NullPointerException, InternalException, JsonProcessingException {
String entityJsonStr = this.toJsonString();
return OBJECT_MAPPER.readTree(entityJsonStr);
}

/**
* Dump the Entity into an entity JSON file.
*
* @param file the file to dump the Entity into
* @throws NullPointerException if the Entity is null
* @throws InternalException if the Entity is unable to be parsed
* @throws JsonProcessingException if the Entity JSON is unable to be processed
* @throws IOException if the Entity is unable to be written to the file
*/
public void writeToJson(File file) throws NullPointerException, InternalException, JsonProcessingException, IOException {
ObjectWriter writer = OBJECT_MAPPER.writer();
JsonNode entityJson = this.toJsonValue();
writer.writeValue(file, entityJson);
}

/**
* Get the value for the given attribute, or null if not present.
*
Expand Down Expand Up @@ -116,6 +165,14 @@ public EntityUID getEUID() {
return euid;
}

/**
* Get the Entity's attributes
* @return the attribute map
*/
private Map<String, Value> getAttributes() {
return attrs;
}

/**
* Get this Entity's parents
* @return the set of parent EntityUIDs
Expand All @@ -131,4 +188,6 @@ public Set<EntityUID> getParents() {
public Map<String, Value> getTags() {
return tags;
}

private static native String toJsonEntityJni(Entity entity) throws NullPointerException, InternalException;
}
Loading