How to store Binary Data as an attribute in your Ontology?
Intro
It is possible to store small binary files (<100kB, such as avatars from social media users) inside an ontology.
To do so, you have to define a property in the schema whose type is 'file'.
From the control panel, go to create/edit the ontology page. Then, create a property and select type 'file':
If you select this type, the schema is going to generate the following property:
Properties needed:
- data: it is the binary string of the file (could be a reference, URL for example).
- media: it is the context data of the file, for later processing.
- media.name: the file's name. Ex: "Avatar.png".
- media.storageArea: where the file is stored: Serialized if the data is the string representation of the file (base 64 for example). Database for database reference. URL if the data contains the URL where the resource is stores.
- media.type: the data's encoding.
- media.mime: the file's mime or content type.
Java support
We have support to make operations with binary files and ontologies, inside the comms protocol library.Â
Either compile the jar or add the following dependency:
<dependency>
<groupId>com.minsait.onesait.platform</groupId>
<artifactId>onesaitplatform-comms-protocol</artifactId>
<version>${onesaitplatform.version}</version>
</dependency>
There is a class called BinarySerialized inside package com.minsait.onesait.platform.comms.protocol.util
This class has three methods:
- getJsonBinary(String fieldName, File file, Mime mime): which returns a JsonNode object with the structure of a binary property, encrypting the file in base64.
- public byte[] binaryJsonToFile(JsonNode binaryNode): which reads from a binary property Node, and decrypts File data into bytes.
- public void binaryJsonToFile(JsonNode binaryNode, String path): performs the same decryption process, but instead of returning the array of bytes, it creates the File in the output "path" specified.
Example in Java
An example of Main class is provided, to illustrate how it works. This example uses RestClient form our Java API.
Then, we serialize the data of the image, by creating BinarySerializer object and calling getJsonbinary().
Then, we add this "image" property to the ontology instance node, and perform the POST action via REST.
Lastly, we perform GET action to retrieve all ontology instances, and in case they have binary data, generate the image locally.
 Â