Announcement

Collapse
No announcement yet.

JSch Object channel.put from rawdata

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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'));

  • #2
    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.

    Comment


    • #3
      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));

      Comment


      • #4
        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.

        Comment


        • #5
          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).

          Comment


          • #6
            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.

            Comment


            • #7
              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'));

              Comment


              • #8
                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.

                Comment


                • #9
                  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)

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      will I just need to add importPackage(java.nio.charset.StandardCharsets); or is there something else I need? This is the error I am getting .


                      Function importPackage must be called with a package; had "[JavaClass java.nio.charset.StandardCharsets]" instead.

                      Comment


                      • #12
                        When you call importPackage you need to use a package name, not the class name. So just get rid of the ".StandardCharsets" and it should work.
                        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.

                        Comment


                        • #13
                          I added the code importPackage(java.nio.charset.StandardCharsets); but its getting and error Function importPackage must be called with a package; had "[JavaClass java.nio.charset.StandardCharsets]" instead. How do i fix this?

                          Comment


                          • #14
                            When I login to the sftp server all the files are 0KB in size, am i using the correct StandardCharsets?


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

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

                            Comment


                            • #15
                              Java JSch library push 0KB file

                              I am using the Java JSch library to connect to an SFTP server and can see that the files are getting created but the content is not in the file. The files are 0 KB is size and they are going into the correct folder on the sftp server. Can it be a way I am converting the string to a stream array.


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

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

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

                              Comment

                              Working...
                              X