web stats
SMB in JavaScript Reader - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 04-18-2018, 11:31 AM
mhaase mhaase is offline
OBX.2 Kenobi
 
Join Date: Dec 2015
Posts: 60
mhaase is on a distinguished road
Default SMB in JavaScript Reader

Has anyone delved into coding a successful SMB connection in the JavaScript Reader? If so, can you share some examples please?

Thanks!
Matt
Reply With Quote
  #2  
Old 04-19-2018, 07:45 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 967
agermano is on a distinguished road
Default

mirth uses jcifs for smb. You can probably find some samples on stack overflow using the library.
Reply With Quote
  #3  
Old 06-13-2018, 02:09 PM
mhaase mhaase is offline
OBX.2 Kenobi
 
Join Date: Dec 2015
Posts: 60
mhaase is on a distinguished road
Default

I have scrapped using SMB all together in Mirth in favor of another routine, accomplishing the same thing with very minimal overhead.
Reply With Quote
  #4  
Old 06-20-2018, 12:51 AM
AlexNeiva AlexNeiva is offline
Mirth Guru
 
Join Date: Oct 2013
Location: Portugal
Posts: 277
AlexNeiva is on a distinguished road
Default

Hello,


below is some code i made.
It basically get a file from a share and copy to the local machine.


Code:
var auth = new Packages.jcifs.smb.NtlmPasswordAuthentication(null, "username", "password");

try
{
    var remoteFile  = new Packages.jcifs.smb.SmbFile( new java.lang.String("smb://ip/xx/xx/xx.pdf"), auth);

    if(remoteFile.exists())
    {
        var destination = "C:/xx/xx.pdf";
    
        var localFile = java.nio.file.Paths.get(destination);
        var inputStr=remoteFile.getInputStream();
        java.nio.file.Files.copy(inputStr, localFile,java.nio.file.StandardCopyOption.REPLACE_EXISTING);
        
        inputStr.close();    
    }
    else
        logger.error("File " + FilePath + " does not exist!");
    
}
catch(err)
{

}
__________________
Best Regards,
Alex Neiva
Reply With Quote
  #5  
Old 06-20-2018, 03:43 AM
robrud robrud is offline
Mirth Newb
 
Join Date: Oct 2015
Posts: 12
robrud is on a distinguished road
Default SmbFileConnection

We use the functions below, for example to read PDFs linked in HL7 ORU / MDM files into the channel-attachment.

For large files, we have an implementation with inputStream, just moving the file and not storing it in mirth.

Code:
/**
	Lesen einer Datei via SMB-Verbindung

	@param {String} smbShare - ServerName
	@param {String} smbDirectory - FileDirectory
	@param {String} smbFile - FileName
	@param {String} smbUsername - smbDomain\\smbUserName
	@param {String} smbPassword - smbPassword
	@param {Number} smbTimeout - Timeout in milliseconds
	@param {String} smbSchemeProperties - smbSchemeProperties default null
	@return {String} File as String (nicht Base64)
*/
function ctReadFileFromSMBPlain_03(smbShare, smbDirectory, smbFile, smbUsername, smbPassword, smbTimeout, smbSchemeProperties) {
	
	// http://www.mirthcorp.com/community/forums/showthread.php?t=6738&highlight=smb+javascript - MC v2.	- SmbFileConnection(strHost, strUsername, strPassword)
	// http://www.mirthcorp.com/community/forums/showthread.php?t=15391 - MC v3.1.x - v3.2.x				- SmbFileConnection(String share, String domainAndUser, String password, int timeout)
	// MC v3.3.x																			- SmbFileConnection(String share, FileSystemConnectionOptions fileSystemOptions, int timeout)
	try {
		// connect to cifs server and get pdf file
		var smbFileSystemOptions = new com.mirth.connect.connectors.file.FileSystemConnectionOptions(smbUsername, smbPassword, smbSchemeProperties);
		var client = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare, smbFileSystemOptions, smbTimeout);

		var inputStream = client.readFile(smbFile, smbDirectory);
		//var inputByteArr = org.apache.commons.io.IOUtils.toByteArray(inputStream);
		var inputString = org.apache.commons.io.IOUtils.toString(inputStream);
		inputStream.close();

		//pdfContentBase64 = FileUtil.encode(inputByteArr);
		//return pdfContentBase64;
		return inputString;
		
	} catch (err) {
		throw(err);
	} finally {
		client.destroy();
	}
}
Code:
/**
	Lesen einer Datei via SMB-Verbindung

	@param {String} smbShare - ServerName
	@param {String} smbDirectory - FileDirectory
	@param {String} smbFile - FileName
	@param {String} smbUsername - smbDomain\\smbUserName
	@param {String} smbPassword - smbPassword
	@param {Number} smbTimeout - Timeout in milliseconds
	@param {String} smbSchemeProperties - smbSchemeProperties default null
	@return {String} File as Base64 String
*/
function ctReadFileFromSMBBase64_03(smbShare, smbDirectory, smbFile, smbUsername, smbPassword, smbTimeout, smbSchemeProperties) {
	
	// http://www.mirthcorp.com/community/forums/showthread.php?t=6738&highlight=smb+javascript - MC v2.	- SmbFileConnection(strHost, strUsername, strPassword)
	// http://www.mirthcorp.com/community/forums/showthread.php?t=15391 - MC v3.1.x - v3.2.x				- SmbFileConnection(String share, String domainAndUser, String password, int timeout)
	// MC v3.3.x																			- SmbFileConnection(String share, FileSystemConnectionOptions fileSystemOptions, int timeout)
	try {
		// connect to cifs server and get pdf file
		var smbFileSystemOptions = new com.mirth.connect.connectors.file.FileSystemConnectionOptions(smbUsername, smbPassword, smbSchemeProperties);
		var client = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare, smbFileSystemOptions, smbTimeout);

		var inputStream = client.readFile(smbFile, smbDirectory);
		var inputByteArr = org.apache.commons.io.IOUtils.toByteArray(inputStream);
		inputStream.close();

		pdfContentBase64 = FileUtil.encode(inputByteArr);
		return pdfContentBase64;
		
	} catch (err) {
		throw(err);
	} finally {
		client.destroy();
	}
}
Code:
/* Implementierungsnotizen
   Im Javascript-Transformer werden Dateien via Inputstream/Outputstream byteweise verschoben.
   Die Implementierung baut darauf, als Delimited-Nachricht die Liste der Dateien zu erhalten:
   z.B. 
   ssi0127,daten_dev$/to/,datei1.csv,ssi0127,daten_dev$/from/,Export.csv
   Die Implementierung funktioniert fuer verschiedene Server aber nur fuer SMB-Shares
*/

// SMB User and Password
smbUsername = configurationMap.get('sks_user');//'domain\user';
smbPassword = configurationMap.get('sks_user_pwd');//'**';

// Variablen
var smbSchemeProperties = null;
var smbTimeout = '120000';

var smbFileSystemOptions = new com.mirth.connect.connectors.file.FileSystemConnectionOptions(smbUsername, smbPassword, smbSchemeProperties);

// Dateien kopieren
for each (row in msg['row']) {
	try {
		// Quelle:
		var smbShare = row['srcSrv'].toString(); //'ssi0127';
		var smbFile = row['srcFile'].toString(); //'DownloadHospindex_20171112.zip';
		var smbDirectory = row['srcDir'].toString(); //'/daten_dev$/kgr/emedista/kisim/to_int/';
		var smbConn = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare, smbFileSystemOptions, smbTimeout);
		
		// Ziel:
		var smbShare2 = row['destSrv'].toString(); //'ssi0127';
		var smbFile2 = row['destFile'].toString();//DownloadHospindex.zip
		var smbDirectory2 = row['destDir'].toString(); //'/daten_dev$/kgr/emedista/kisim/test/';
		var smbConn2 = new com.mirth.connect.connectors.file.filesystems.SmbFileConnection(smbShare2, smbFileSystemOptions, smbTimeout);
	
		if (smbConn.exists(smbFile, smbDirectory)) {
			// InputStream
			var myInputStream = smbConn.readFile(smbFile, smbDirectory);		//read File
			// -> OutputStream
			smbConn2.writeFile(smbFile2 + '.tmp', smbDirectory2, false, myInputStream);	//copy File //Append false
			// Variablen schliessen
			myInputStream.close();
			
			// tmp-Datei umbenennen
			smbConn2.move(smbFile2 + '.tmp', smbDirectory2, smbFile2, smbDirectory2);
		}
	}
	catch(e) {}
	finally {
		// Variablen schliessen
		smbConn.destroy();
		smbConn2.destroy();
	}
}
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 09:53 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation