CRecordFileStoreRequestFile: Request a File from a FileStore
Request a file from a The File Store API. This request should always be sent to the local FileStore, as it will download the file from another FileStore if necessary and then return it in the response.
This message belongs to the FileStore API.
The message is a nanoservice of the SYSTEM namespace.
{
"id": "0b85e177-500e-47dc-b794-4675e0985d94",
"name": "FILE_STORE_REQUEST_FILE",
"description": "Request a file from the local file store.",
"isNanoService": "true",
"type": "REQUEST",
"namespaces": "SYSTEM",
"slots": [
{
"key": "path",
"name": "PATH",
"direction": "REQUEST",
"presenceConstraint": "CONDITIONAL_EXCLUSIVE",
"type": "STRING",
"description": "The relative path of the file.\nCan be null, if hash is provided."
},
{
"key": "hash",
"name": "HASH",
"direction": "REQUEST",
"presenceConstraint": "CONDITIONAL_EXCLUSIVE",
"type": "STRING",
"description": "MD5 hash in base32 (use CMd5.toBase32()).\nCan be null, if path is provided."
},
{
"key": "how",
"name": "DELIVERY_TYPE",
"direction": "REQUEST",
"presenceConstraint": "OPTIONAL",
"type": "BYTE",
"description": "1 (default) as byte array, 2 as java.util.File."
},
{
"key": "rec",
"name": "FILE_RECORD",
"direction": "RESPONSE",
"presenceConstraint": "MANDATORY",
"type": "RECORD",
"description": "A record of type CRecordFileStoreFile."
},
{
"key": "file",
"name": "FILE",
"direction": "RESPONSE",
"presenceConstraint": "CONDITIONAL_EXCLUSIVE",
"type": "OBJECT",
"description": "A java.util.File."
},
{
"key": "ba",
"name": "BYTES",
"direction": "RESPONSE",
"presenceConstraint": "CONDITIONAL_EXCLUSIVE",
"type": "BYTE_ARRAY",
"description": "The bytes of the file."
}
]
}
Slot Description
| Key | Name | Direction | Presence Constraint | Type | Description |
|---|---|---|---|---|---|
| path | PATH | REQUEST | CONDITIONAL_EXCLUSIVE | STRING |
The relative path of the file. Can be null, if hash is provided. |
| hash | HASH | REQUEST | CONDITIONAL_EXCLUSIVE | STRING |
MD5 hash in base32 (use CMd5.toBase32()). Can be null, if path is provided. |
| how | DELIVERY_TYPE | REQUEST | OPTIONAL | BYTE |
1 (default) as a byte array 2 as java.util.File object. |
| rec | FILE_RECORD | RESPONSE | MANDATORY | RECORD | A record of type CRecordFileStoreFile. |
| file | FILE | RESPONSE | CONDITIONAL_EXCLUSIVE | OBJECT | If a java.util.File object was requested, the object for a file in the local FileStore is delivered here if successful. |
| ba | BYTES | RESPONSE | CONDITIONAL_EXCLUSIVE | BYTE_ARRAY | If the data in the file was requested, the byte array of the file contents is delivered here if successful. |
The request requires specifying the relative path of the file. Optionally, a hash can be included if a specific version of the file is needed. Furthermore, you can specify whether the file should be delivered as a byte array (default) or as a local file.
The response contains a record of type CRecordFileStoreFile with a detailed description of the file. Additionally, it includes the file object or the file's bytes.
Usage
Request
final CEnvelope env = CEnvelope.forLocalNanoService(CWellKnownNID.SYSTEM); final CRecord record = CRecordFileStoreRequestFile.create(); CRecordFileStoreRequestFile.setPath(record, "images/myIcon.gif"); CRecordFileStoreRequestFile.setDeliveryType(record, EFileDeliveryType.BYTES.getType()); sendRequest(env, record);
Catch the response
In the constructor:
addMessageHandler(CRecordFileStoreRequestFile.ID,
this::asyncGetFile);
The message handler:
private boolean asyncGetFile(@NotNull final CEnvelope aEnvelope, @NotNull final CRecord aRecord) { if (aEnvelope.isAnswer()) { final CResult result = aEnvelope.getResult(); if (result.isSuccess()) { final byte[] bytes = CRecordFileStoreRequestFile.getBytes(aRecord, null); if (bytes != null) { final ImageIcon image = new ImageIcon(bytes, path); ... } // Optional, if File has been requested: final File file = (File) CRecordFileStoreRequestFile.getFile(aRecord, null); // if needed: final CRecord rec = CRecordFileStoreRequestFile.getFileRecord(aRecord, null); if (rec != null) { final String hash = CRecordFileStoreFile.getHash(rec, null); ... } } return true; } else { return false; } }
Record Usage
In
One or more descriptions of these Records are stored in the JSON or XML format
as record.json or record.xml within a directory.
The Record Generator, an included Swing tool, generates helper classes from these files.
These classes can then be used to type-safely write or read messages.