Search code examples
dropboxdropbox-apimirth

Read Files From Dropbox through Mirth


I have a requirement where I need to read the HL7v2 files stored in sites like dropbox. One way to do this is to download the file from that site and access it via SFTP through Mirth.

But that process is tedious, imagine I will have 1000's of file generated at a point of time and It will be very hectic and time consuming to manually download all those data and fetch it from SFTP. Does Mirth 3.4.2 community version has this ability to fetch the data from cloud storage? or possible to read through JavaScript reader?

Please let me know if it is achievable via Mirth.


Solution

  • I'm not sure if Dropbox has any kind of native FTP or SFTP access layer, but it does have an API and SDK: enter link description here

    Here's the example code snippet they show:

    import com.dropbox.core.DbxException;
    import com.dropbox.core.DbxRequestConfig;
    import com.dropbox.core.v2.DbxClientV2;
    import com.dropbox.core.v2.files.FileMetadata;
    import com.dropbox.core.v2.files.ListFolderResult;
    import com.dropbox.core.v2.files.Metadata;
    import com.dropbox.core.v2.users.FullAccount;
    
    import java.util.List;
    
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.io.IOException;
    
    public class Main {
        private static final String ACCESS_TOKEN = "<ACCESS TOKEN>";
    
        public static void main(String args[]) throws DbxException, IOException {
            // Create Dropbox client
            DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial", "en_US");
            DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
    
            // Get current account info
            FullAccount account = client.users().getCurrentAccount();
            System.out.println(account.getName().getDisplayName());
    
            // Get files and folder metadata from Dropbox root directory
            ListFolderResult result = client.files().listFolder("");
            while (true) {
                for (Metadata metadata : result.getEntries()) {
                    System.out.println(metadata.getPathLower());
                }
    
                if (!result.getHasMore()) {
                    break;
                }
    
                result = client.files().listFolderContinue(result.getCursor());
            }
    
            // Upload "test.txt" to Dropbox
            try (InputStream in = new FileInputStream("test.txt")) {
                FileMetadata metadata = client.files().uploadBuilder("/test.txt")
                    .uploadAndFinish(in);
            }
        }
    }
    

    That translates easily into a JavaScript Reader script:

    var accessToken = '<ACCESS TOKEN>';
    
    // Create Dropbox client
    var config = new com.dropbox.core.DbxRequestConfig("dropbox/java-tutorial", "en_US");
    var client = new com.dropbox.core.v2.DbxClientV2(config, accessToken);
    
    // Get files and folder metadata from Dropbox root directory
    var request = client.files();
    var listResult = request.listFolder('');
    var messages = new java.util.ArrayList();
    
    while (true) {
        for each (metadata in listResult.getEntries().toArray()) {
            if (metadata instanceof com.dropbox.core.v2.files.FileMetadata) {
                try {
                    var sourceMap = new java.util.HashMap();
                    sourceMap.put('originalFilename', metadata.getName());
                    sourceMap.put('fileDirectory', org.apache.commons.lang3.StringUtils.removeEndIgnoreCase(metadata.getPathLower(), metadata.getName()));
                    sourceMap.put('fileSize', metadata.getSize());
                    sourceMap.put('fileLastModified', metadata.getServerModified());
                    sourceMap.put('dropboxId', metadata.getId());
    
                    var baos = new java.io.ByteArrayOutputStream();
                    var result = request.download(metadata.getId()).download(baos);
    
                    if (result && result.getId() == metadata.getId()) {
                        messages.add(new RawMessage(baos.toByteArray(), null, sourceMap));
                    }
                } catch (e) {
                    logger.error('Error downloading file: ' + metadata.getPathLower(), e.javaException);
                }
            }
        }
    
        if (!listResult.getHasMore()) {
            break;
        }
    
        listResult = request.listFolderContinue(listResult.getCursor());
    }
    
    return messages;