|
| 1 | +var AttachmentSftpUtils = Class.create(); |
| 2 | +AttachmentSftpUtils.prototype = { |
| 3 | + initialize: function () { |
| 4 | + // Set up the Packages references |
| 5 | + |
| 6 | + this.File = Packages.java.io.File; |
| 7 | + this.FileOutputStream = Packages.java.io.FileOutputStream; |
| 8 | + |
| 9 | + |
| 10 | + //get the parameters |
| 11 | + |
| 12 | + this.relativeUrl = probe.getParameter("relativeUrl"); |
| 13 | + this.domain = probe.getParameter("httpDomain"); |
| 14 | + this.midlogs = probe.getParameter("MidLogs"); |
| 15 | + this.filename = probe.getParameter("filename"); |
| 16 | + this.midserverfilepath = probe.getParameter("midserverpath"); |
| 17 | + this.midserverfilename = probe.getParameter("filename"); |
| 18 | + //this.deleteAfterUpload = probe.getParameter("deleteAfterUpload"); |
| 19 | + this.SnowMidUserName = probe.getParameter("SnowMidUsername"); |
| 20 | + this.SnowMidPassword = probe.getParameter("SnowMidPassword"); |
| 21 | + |
| 22 | + //sftp |
| 23 | + |
| 24 | + this.targetServer = probe.getParameter('sftpTargetServer'); |
| 25 | + this.targetUsername = probe.getParameter('sftpTargetUsername'); |
| 26 | + this.targetPassword = probe.getParameter('sftpTargetPassword'); |
| 27 | + this.targetPort = probe.getParameter('sftpTargetPort'); |
| 28 | + this.sftpFilePath = probe.getParameter('sftpFilePath'); |
| 29 | + }, |
| 30 | + |
| 31 | + processFileTransfer: function() { |
| 32 | + |
| 33 | + this.log("ShammaSalhotra"); |
| 34 | + |
| 35 | + |
| 36 | + var localFileName = this.midserverfilepath + "/" + this.midserverfilename; |
| 37 | + |
| 38 | + try { |
| 39 | + var fn = this.saveToFile(localFileName); |
| 40 | + this.log("File is moved to MIDserver or not "+ fn); |
| 41 | + |
| 42 | + if(fn){ |
| 43 | + this.log("Initiated file transfer from MID Server: " + localFileName); |
| 44 | + |
| 45 | + var fnc = this.sftpFile(this.targetServer, this.targetUsername, this.targetPassword, localFileName, this.sftpFilePath + this.midserverfilename); |
| 46 | + if(fnc!=""){ |
| 47 | + |
| 48 | + return fnc; |
| 49 | + |
| 50 | + } |
| 51 | + return "file is moved to mid server"; |
| 52 | + } |
| 53 | + } catch (e) { |
| 54 | + this.log("ShammaSalhotra1"); |
| 55 | + this.log("Error in writing file to SFTP server: " + e); |
| 56 | + |
| 57 | + return "Error in writing file to SFTP server: " + e; |
| 58 | + } |
| 59 | + |
| 60 | + }, |
| 61 | + saveToFile: function(targetPath) { |
| 62 | + var tmpLoc; |
| 63 | + var result = true; |
| 64 | + |
| 65 | + //var DPassword = new Packages.com.glide.util.Encrypter().decrypt(this.SnowMidPassword); // decrypting the password |
| 66 | + var DPassword = this.SnowMidPassword; |
| 67 | + //Initiating the webservice to get the attachment from servicenow |
| 68 | + |
| 69 | + this.HttpClients = Packages.com.glide.communications.HTTPRequest; // GlideHTTPRequest |
| 70 | + |
| 71 | + var munewurl = this.relativeUrl; |
| 72 | + |
| 73 | + |
| 74 | + var request = new this.HttpClients(munewurl); |
| 75 | + request.setBasicAuth(this.SnowMidUserName,DPassword); |
| 76 | + request.addHeader('Accept','application/json'); |
| 77 | + var response = request.get(); |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + var status = response.getStatusCode(); |
| 82 | + |
| 83 | + this.log("Response " + status); |
| 84 | + tmpLoc = targetPath;//this.midserverfilepath + this.midserverfilename; |
| 85 | + |
| 86 | + if( status == "200" ){ |
| 87 | + // Save the attachment in midserver |
| 88 | + |
| 89 | + try { |
| 90 | + |
| 91 | + this.log("Saving the attachment to " + this.filename +"::" + targetPath); |
| 92 | + var f = new this.File(targetPath); |
| 93 | + var inputStream = response.getBodyAsStream(); |
| 94 | + var out = new this.FileOutputStream(f); |
| 95 | + var buf = Packages.java.lang.reflect.Array.newInstance(Packages.java.lang.Byte.TYPE, 1024); |
| 96 | + while ((len = inputStream.read(buf)) > 0) { |
| 97 | + |
| 98 | + out.write(buf, 0, len); |
| 99 | + } |
| 100 | + out.close(); |
| 101 | + |
| 102 | + this.log("File saved to: " + f); |
| 103 | + inputStream.close(); |
| 104 | + |
| 105 | + }catch (e) { |
| 106 | + |
| 107 | + result = false; |
| 108 | + } |
| 109 | + } |
| 110 | + else{ |
| 111 | + |
| 112 | + result = false; |
| 113 | + } |
| 114 | + return result; |
| 115 | + }, |
| 116 | + |
| 117 | + |
| 118 | + sftpFile : function(hostName, userName, password, localFileName, remoteFileName) { |
| 119 | + // Initiate the file transfer from midserver to sftp server |
| 120 | + this.log("Shamma"); |
| 121 | + var result = ""; |
| 122 | + this.log('sftpFile(): attempting to connect to ' + hostName); |
| 123 | + var ssh = new Packages.com.sshtools.j2ssh.SshClient(); |
| 124 | + var ignoreHost = new Packages.com.sshtools.j2ssh.transport.IgnoreHostKeyVerification(); |
| 125 | + if (!this.targetPort){ |
| 126 | + this.targetPort = 22; |
| 127 | + } |
| 128 | + this.log('sftpFile(): attempting to connect to ' + hostName + " on port " + this.targetPort); |
| 129 | + ssh.connect(hostName, this.targetPort, ignoreHost); |
| 130 | + |
| 131 | + var pwd = new Packages.com.sshtools.j2ssh.authentication.PasswordAuthenticationClient(); |
| 132 | + //var authPassword = new Packages.com.glide.util.Encrypter().decrypt(password); |
| 133 | + //var tarpassword = this.targetPassword; |
| 134 | + pwd.setUsername(userName); |
| 135 | + pwd.setPassword(password); |
| 136 | + |
| 137 | + |
| 138 | + this.log('sftpFile(): attempting to copy ' + localFileName + ' to ' + remoteFileName); |
| 139 | + if(ssh.authenticate(pwd) == new Packages.com.sshtools.j2ssh.authentication.AuthenticationProtocolState().COMPLETE) { |
| 140 | + sftp = ssh.openSftpClient(); |
| 141 | + |
| 142 | + try { |
| 143 | + sftp.put(localFileName, remoteFileName); |
| 144 | + this.log("File successfully uploaded to sftp " + remoteFileName); |
| 145 | + |
| 146 | + result = "File successfully uploaded to sftp"; |
| 147 | + |
| 148 | + if (this.deleteAfterUpload == "true") { |
| 149 | + this.log("deleteAfterUpload -> " + this.deleteAfterUpload + ", deleting local file..."); |
| 150 | + new this.File(localFileName)["delete"](); |
| 151 | + } |
| 152 | + } catch(e) { |
| 153 | + this.log('FILE NOT FOUND ' + remoteFileName + ' or error: ' + e); |
| 154 | + result = 'FILE NOT FOUND ' + remoteFileName + ' or error: ' + e; |
| 155 | + } |
| 156 | + sftp.quit(); |
| 157 | + try{ |
| 158 | + // kill connection |
| 159 | + ssh.disconnect(); |
| 160 | + } |
| 161 | + catch(e){ |
| 162 | + this.log('Manual connection kill not successful with error: ' + e); |
| 163 | + result = 'Manual connection kill not successful with error: ' + e; |
| 164 | + } |
| 165 | + } |
| 166 | + else { |
| 167 | + result = 'Error ' + new Packages.com.sshtools.j2ssh.authentication.AuthenticationProtocolState().COMPLETE; |
| 168 | + } |
| 169 | + |
| 170 | + return result; |
| 171 | + |
| 172 | + }, |
| 173 | + |
| 174 | + log: function(data) { |
| 175 | + if (this.midlogs == "true") { |
| 176 | + ms.log(data); |
| 177 | + } |
| 178 | + }, |
| 179 | + |
| 180 | + type: AttachmentSftpUtils |
| 181 | +}; |
0 commit comments