web stats
JSch Object channel.put from rawdata - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 01-26-2016, 01:13 PM
tmcdevitt tmcdevitt is offline
OBX.3 Kenobi
 
Join Date: Oct 2014
Posts: 125
tmcdevitt is on a distinguished road
Smile 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'));
Reply With Quote
  #2  
Old 01-27-2016, 09:09 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,123
narupley is on a distinguished road
Default

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.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #3  
Old 01-27-2016, 09:15 AM
tmcdevitt tmcdevitt is offline
OBX.3 Kenobi
 
Join Date: Oct 2014
Posts: 125
tmcdevitt is on a distinguished road
Default

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));
Reply With Quote
  #4  
Old 01-27-2016, 09:16 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,123
narupley is on a distinguished road
Default

Post the entire error you're getting.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #5  
Old 01-27-2016, 09:22 AM
tmcdevitt tmcdevitt is offline
OBX.3 Kenobi
 
Join Date: Oct 2014
Posts: 125
tmcdevitt is on a distinguished road
Default

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).
Reply With Quote
  #6  
Old 01-27-2016, 09:23 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,123
narupley is on a distinguished road
Default

Is that just a syntax error? If so what code are you using?
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #7  
Old 01-27-2016, 09:33 AM
tmcdevitt tmcdevitt is offline
OBX.3 Kenobi
 
Join Date: Oct 2014
Posts: 125
tmcdevitt is on a distinguished road
Default

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'));
Reply With Quote
  #8  
Old 01-27-2016, 09:35 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,123
narupley is on a distinguished road
Default

Makes sense. It's because you're not using correct JavaScript syntax. You should be declaring that "stream" variable with "var".
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #9  
Old 01-27-2016, 09:40 AM
tmcdevitt tmcdevitt is offline
OBX.3 Kenobi
 
Join Date: Oct 2014
Posts: 125
tmcdevitt is on a distinguished road
Default

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)
Reply With Quote
  #10  
Old 01-27-2016, 09:46 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,123
narupley is on a distinguished road
Default

Yeah, you need to either import the package or use the fully-qualified class name there.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

Nicholas Rupley
Work: 949-237-6069
Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
Reply

Tags
getrawdata(), inputstreamreader, jsch, sftp, sftp connection

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 05:56 AM.


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