CRecordFileStoreRequestFile

Request a file from a FileStore. 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.",
    "isService": "true",
    "namespaces": "SYSTEM",
    "slots": [
        {
          "key": "path",
          "name": "PATH",
          "direction": "REQUEST",
          "mandatory": "false",
          "type": "STRING",
          "description": "The relative path of the file.\nCan be null, if hash is provided."
        },
        {
          "key": "hash",
          "name": "HASH",
          "direction": "REQUEST",
          "mandatory": "false",
          "type": "STRING",
          "description": "MD5 hash in base32 (use CMd5.toBase32()).\nCan be null, if path is provided."
        },
        {
          "key": "how",
          "name": "DELIVERY_TYPE",
          "direction": "REQUEST",
          "mandatory": "false",
          "type": "BYTE",
          "description": "1 (default) as byte array, 2 as java.util.File."
        },
        {
          "key": "rec",
          "name": "FILE_RECORD",
          "direction": "ANSWER",
          "mandatory": "false",
          "type": "RECORD",
          "description": "A record of type CRecordFileStoreFile."
        },
        {
          "key": "file",
          "name": "FILE",
          "direction": "ANSWER",
          "mandatory": "false",
          "type": "OBJECT",
          "description": "A java.util.File."
        },
        {
          "key": "ba",
          "name": "BYTES",
          "direction": "ANSWER",
          "mandatory": "false",
          "type": "BYTE_ARRAY",
          "description": "The bytes of the file."
        }
    ]
}

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;
    }
}