Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   JSch Object channel.put from rawdata (http://www.mirthcorp.com/community/forums/showthread.php?t=215493)

tmcdevitt 01-26-2016 01:13 PM

JSch Object channel.put from rawdata
 
Hi, I am using the JS Writer to put files onto a SFTP server. I have it working fine from a file that is on disk. I would just like to update my code to use the connectorMessage.getRawData() and the $('originalFilename').


var f = new File('D:\FileToTestUpload.txt');

var isr = new InputStreamReader(connectorMessage.getRawData());
//InputStream is = new ByteArrayInputStream(connectorMessage.getRawData() );
//channel.put(is, $('originalFilename'));
channel.put(new FileInputStream(f), $('originalFilename'));

narupley 01-27-2016 09:09 AM

To convert a String to an InputStream, use a ByteArrayInputStream and pass in the byte array. Call getBytes(charset) on the String passing in the correct charset to get the byte array.

tmcdevitt 01-27-2016 09:15 AM

When I tried using this code I am getting an error "null" at Javascript missing ;

InputStream stream = new ByteArrayInputStream(connectorMessage.getRawData() .getBytes(StandardCharsets.UTF_8));

narupley 01-27-2016 09:16 AM

Post the entire error you're getting.

tmcdevitt 01-27-2016 09:22 AM

Do I need to import a new package?



importPackage(com.jcraft.jsch);
importPackage(java.io);
importPackage(java.lang);




Error in connector "null" at Javascript:
Error on line 35: missing ; before statement (d3ce8be6-3d05-4033-a6ce-788b0ee9069c#35).

narupley 01-27-2016 09:23 AM

Is that just a syntax error? If so what code are you using?

tmcdevitt 01-27-2016 09:33 AM

Yes its a seems like an syntax error. Below is the code

importPackage(com.jcraft.jsch);
importPackage(java.io);
importPackage(java.lang);



//create object.
var jsch = new JSch();

//add identity
//jsch.addIdentity("/home/.ssh/id_pubkey", "test"); //note: this is a Linux path not Windows path - hyphens are reversed.

jsch.setConfig('StrictHostKeyChecking','no');

var session = jsch.getSession('Username','IPAddress',22);
session.setPassword('PASSWORD');
session.setTimeout(20000);

session.connect();

var channel = session.openChannel('sftp');
channel.connect();
logger.info('Connected to SFTP: ');
logger.info('Home Directory: ' + channel.getHome());

channel.cd('/Users/lab/Public/Labs_Import'); //Folder Name Where File Will Be Dropped

//var f = new File('D:\key.txt');
//InputStream in = new ByteArrayInputStream(connectorMessage.getRawData() );
//DataInputStream stream = new DataInputStream(new ByteArrayInputStream(test));

//Error in Editor missing ; before statement
InputStream stream = new ByteArrayInputStream(connectorMessage.getRawData() .getBytes(StandardCharsets.UTF_8));

channel.put(stream, $('originalFilename'));

narupley 01-27-2016 09:35 AM

Makes sense. It's because you're not using correct JavaScript syntax. You should be declaring that "stream" variable with "var".

tmcdevitt 01-27-2016 09:40 AM

I am getting this StandardCharsets when I use the var.

New code
var stream = new ByteArrayInputStream(connectorMessage.getRawData() .getBytes(StandardCharsets.UTF_8));

Destination 1SCRIPT SOURCE:
JavaScript WriterSOURCE CODE:
72: 73: //var f = new File('D:\key.txt');74: //InputStream in = new ByteArrayInputStream(connectorMessage.getRawData() );75: //DataInputStream stream = new DataInputStream(new ByteArrayInputStream(test));76: 77: var stream = new ByteArrayInputStream(connectorMessage.getRawData() .getBytes(StandardCharsets.UTF_8));78: 79: channel.put(stream, $('originalFilename'));80: //var isr = new InputStreamReader(connectorMessage.getRawData());8 1: LINE NUMBER:
77DETAILS:
ReferenceError: "StandardCharsets" is not defined.
at b15ad656-93d6-4f53-b20b-bf0c8222c11e:77 (doScript)

narupley 01-27-2016 09:46 AM

Yeah, you need to either import the package or use the fully-qualified class name there.


All times are GMT -8. The time now is 08:10 AM.

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