Guest User

Untitled

a guest
Jan 9th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. import com.axeda.drm.sdk.Context
  2. import com.axeda.drm.sdk.scripto.Request
  3. import com.axeda.drm.sdk.audit.AuditCategory
  4. import com.axeda.drm.sdk.audit.AuditMessage
  5. import com.axeda.drm.sdk.contact.Location
  6. import com.axeda.drm.sdk.contact.Organization
  7. import com.axeda.drm.sdk.data.UploadedFile
  8. import com.axeda.drm.sdk.device.Device
  9. import com.axeda.drm.sdk.rules.ActionContext
  10. import com.axeda.drm.util.Emailer
  11. import com.axeda.platform.sdk.v1.services.extobject.ExtendedObject
  12. import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectSearchCriteria
  13. import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectService
  14. import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectType
  15. import com.axeda.platform.sdk.v1.services.extobject.Property
  16. import com.axeda.platform.sdk.v1.services.extobject.PropertyDataType
  17. import com.axeda.platform.sdk.v1.services.extobject.PropertyType
  18. import com.axeda.platform.sdk.v1.services.factories.SpringExtendedServiceFactory
  19. import com.jcraft.jsch.Channel
  20. import com.jcraft.jsch.ChannelSftp
  21. import com.jcraft.jsch.JSch
  22. import com.jcraft.jsch.Session
  23. import javax.mail.internet.InternetAddress
  24. import static com.axeda.sdk.v2.dsl.Bridges.*
  25.  
  26. /**
  27. * OutboundSFTP.groovy
  28. *
  29. * Transfers file to SFTP server
  30. *
  31. * @param server - (REQ):Str server address.
  32. *
  33. * @param port - (REQ):Str server port.
  34. * @param user - (REQ):Str FTP username.
  35. * @param pass - (REQ):Str FTP password.
  36. * @param workingdir - (REQ):Str FTP working directory.
  37. * @param fileId - (REQ):Str Id of the FileInfo.
  38. *
  39. * @author sara streeter <sstreeter@axeda.com>
  40. *
  41. */
  42.  
  43. /**
  44. * initialize our global variables
  45. * json = the contents of our response
  46. * infoString = a stringBuilder used to collect debug information during the script
  47. * contentType = the content type we will return
  48. * scriptname = The name of this Script, used in multiple places
  49. */
  50. def json = new groovy.json.JsonBuilder()
  51. def infoString = new StringBuilder()
  52. def contentType = "application/json"
  53. def scriptName = "OutboundSFTP.groovy"
  54. def root = ["result":["items":[]]]
  55.  
  56. try {
  57.  
  58. def server = Request.parameters.server
  59. // you could also use external credentials for these
  60. def username = Request.parameters.user
  61. def password = Request.parameters.pass
  62. // this is the folder to send to
  63. def workingdir = Request.parameters.workingdir
  64. def fileInfoId = Request.parameters.fileId
  65. int port = Integer.valueOf(Request.parameters['port'])
  66. logger.info(fileInfoId)
  67. InputStream inputStream = fileInfoBridge.getFileData(fileInfoId)
  68. logger.info(inputStream.available())
  69. def fileinfo = fileInfoBridge.findById(fileInfoId)
  70. logger.info(fileinfo?.filesize)
  71. def (result, failureMessage) = sendFile(server, port, username, password, workingdir, inputStream, fileinfo.label);
  72.  
  73. if (result){
  74. logger.info "Send successful."
  75.  
  76. }
  77. else {
  78. logger.info "Send failed."
  79. }
  80. }
  81. catch (Exception e){
  82. logger.error e.getMessage()
  83. }
  84.  
  85. public sendFile(String address, int port, String userName,
  86. String password, String workingdir, InputStream inputStream,
  87. String fileNameToStore) {
  88. int TENSECONDS = 10*1000
  89. int THIRTYSECONDS = 30*1000
  90. Session session = null;
  91. Channel channel = null;
  92. ChannelSftp sftpChannel = null;
  93.  
  94. boolean error = false;
  95. String failureMessage = null;
  96.  
  97. try {
  98. JSch jsch = new JSch();
  99. session = jsch.getSession(userName, address, port);
  100. session.setPassword(password)
  101. session.setConfig("StrictHostKeyChecking", "no");
  102. session.setTimeout(THIRTYSECONDS)
  103. session.connect();
  104.  
  105. channel = session.openChannel("sftp");
  106. channel.connect();
  107. sftpChannel = (ChannelSftp)channel;
  108.  
  109.  
  110. sftpChannel.cd(workingdir);
  111.  
  112. if (!sftpChannel.cd(workingdir)){
  113. throw new Exception( "Directory $workingdir does not exist.")
  114. }
  115.  
  116. // Store the file
  117. sftpChannel.put(inputStream, fileNameToStore);
  118. }
  119. catch (Exception e){
  120. error = true;
  121. failureMessage = e.getMessage();
  122. errorStr = e.getMessage();
  123. logger.error "Could not send file to SFTP: " + errorStr
  124. }
  125. finally{
  126.  
  127. sftpChannel?.exit();
  128. channel?.disconnect();
  129. session?.disconnect();
  130. }
  131. return [!error, failureMessage] ;
  132. }
Add Comment
Please, Sign In to add comment