Guest User

Untitled

a guest
Apr 23rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.61 KB | None | 0 0
  1. import com.jcraft.jsch.JSch
  2. import com.jcraft.jsch.Session
  3. import com.jcraft.jsch.Channel
  4. import com.jcraft.jsch.ChannelSftp
  5. import com.axeda.drm.sdk.data.UploadedFile
  6. import com.jcraft.jsch.JSchException
  7.  
  8. final String SFTP_ADDRESS = "bogusIP";
  9. final int SFTP_PORT = 22;
  10.  
  11. final String userName1 = "axeda1";
  12. final String password1 = "aX3D@1";
  13.  
  14. //final String userName2 = "axeda2"
  15. //final String password2 = "aX3D@2"
  16.  
  17. final String modelName = parameters.model;
  18. final String deviceName = parameters.serial;
  19.  
  20. UploadedFile[] files = compressedFile.getFiles();
  21. File file = files[0]?.extractFile();
  22. boolean result = sendFile(SFTP_ADDRESS, SFTP_PORT, userName1, password1, modelName, deviceName, file);
  23.  
  24. if (!result){
  25.   // store what is needed for retry
  26. }
  27.  
  28. public boolean sendFile(String address,
  29.                          int port,
  30.                          String userName,
  31.                          String password,
  32.                          String modelName,
  33.                          String deviceName,
  34.                          File file) {
  35.  
  36.   JSch jsch = null;
  37.   Session session = null;
  38.   Channel channel = null;
  39.   ChannelSftp sftpChannel = null;
  40.   InputStream is = null;
  41.  
  42.   boolean error = false;
  43.  
  44.   try {
  45.     jsch = new JSch()
  46.     session = jsch.getSession(userName, address, port);
  47.     session.setPassword(password)
  48.     session.setConfig("StrictHostKeyChecking", "no");
  49.     session.connect();
  50.  
  51.     channel = session.openChannel("sftp");
  52.     channel.connect();
  53.     sftpChannel = (ChannelSftp)channel;
  54.  
  55.     String fileName = file.getName();
  56.     String shortFileName = fileName.substring(0,fileName.indexOf("["));
  57.     String fileExt = fileName.substring(fileName.indexOf("]")+1);
  58.     String fileNameToStore = shortFileName + fileExt;
  59.  
  60.     InputStream inputStream = new FileInputStream(file);
  61.  
  62.     try{
  63.       sftpChannel.cd(modelName);
  64.     }
  65.     catch (Exception){
  66.       logger.debug "Asset model directory does not exist.  Creating now."
  67.       sftpChannel.mkdir(modelName);
  68.       sftpChannel.cd(modelName);
  69.     }
  70.  
  71.     try{
  72.       sftpChannel.cd(deviceName);
  73.     }
  74.     catch (Exception){
  75.       logger.debug "Asset device directory does not exist.  Creating now."
  76.       sftpChannel.mkdir(deviceName);
  77.       sftpChannel.cd(deviceName);
  78.     }
  79.     sftpChannel.put(inputStream, fileNameToStore);
  80.   }
  81.   catch (Exception e){
  82.     error = true;
  83.     logger.info "Could not send file to SFTP"
  84.     logger.error e.getMessage()
  85.   }
  86.   finally{
  87.     is?.close();
  88.     sftpChannel?.exit();
  89.     channel?.disconnect();
  90.     session?.disconnect();
  91.   }
  92.  
  93.   logger.info "Return error: " + error
  94.   return !error;
  95. }
Add Comment
Please, Sign In to add comment