Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.axeda.drm.sdk.Context
- import com.axeda.drm.sdk.scripto.Request
- import com.axeda.drm.sdk.audit.AuditCategory
- import com.axeda.drm.sdk.audit.AuditMessage
- import com.axeda.drm.sdk.contact.Location
- import com.axeda.drm.sdk.contact.Organization
- import com.axeda.drm.sdk.data.UploadedFile
- import com.axeda.drm.sdk.device.Device
- import com.axeda.drm.sdk.rules.ActionContext
- import com.axeda.drm.util.Emailer
- import com.axeda.platform.sdk.v1.services.extobject.ExtendedObject
- import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectSearchCriteria
- import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectService
- import com.axeda.platform.sdk.v1.services.extobject.ExtendedObjectType
- import com.axeda.platform.sdk.v1.services.extobject.Property
- import com.axeda.platform.sdk.v1.services.extobject.PropertyDataType
- import com.axeda.platform.sdk.v1.services.extobject.PropertyType
- import com.axeda.platform.sdk.v1.services.factories.SpringExtendedServiceFactory
- import com.jcraft.jsch.Channel
- import com.jcraft.jsch.ChannelSftp
- import com.jcraft.jsch.JSch
- import com.jcraft.jsch.Session
- import javax.mail.internet.InternetAddress
- import static com.axeda.sdk.v2.dsl.Bridges.*
- /**
- * OutboundSFTP.groovy
- *
- * Transfers file to SFTP server
- *
- * @param server - (REQ):Str server address.
- *
- * @param port - (REQ):Str server port.
- * @param user - (REQ):Str FTP username.
- * @param pass - (REQ):Str FTP password.
- * @param workingdir - (REQ):Str FTP working directory.
- * @param fileId - (REQ):Str Id of the FileInfo.
- *
- * @author sara streeter <sstreeter@axeda.com>
- *
- */
- /**
- * initialize our global variables
- * json = the contents of our response
- * infoString = a stringBuilder used to collect debug information during the script
- * contentType = the content type we will return
- * scriptname = The name of this Script, used in multiple places
- */
- def json = new groovy.json.JsonBuilder()
- def infoString = new StringBuilder()
- def contentType = "application/json"
- def scriptName = "OutboundSFTP.groovy"
- def root = ["result":["items":[]]]
- try {
- def server = Request.parameters.server
- // you could also use external credentials for these
- def username = Request.parameters.user
- def password = Request.parameters.pass
- // this is the folder to send to
- def workingdir = Request.parameters.workingdir
- def fileInfoId = Request.parameters.fileId
- int port = Integer.valueOf(Request.parameters['port'])
- logger.info(fileInfoId)
- InputStream inputStream = fileInfoBridge.getFileData(fileInfoId)
- logger.info(inputStream.available())
- def fileinfo = fileInfoBridge.findById(fileInfoId)
- logger.info(fileinfo?.filesize)
- def (result, failureMessage) = sendFile(server, port, username, password, workingdir, inputStream, fileinfo.label);
- if (result){
- logger.info "Send successful."
- }
- else {
- logger.info "Send failed."
- }
- }
- catch (Exception e){
- logger.error e.getMessage()
- }
- public sendFile(String address, int port, String userName,
- String password, String workingdir, InputStream inputStream,
- String fileNameToStore) {
- int TENSECONDS = 10*1000
- int THIRTYSECONDS = 30*1000
- Session session = null;
- Channel channel = null;
- ChannelSftp sftpChannel = null;
- boolean error = false;
- String failureMessage = null;
- try {
- JSch jsch = new JSch();
- session = jsch.getSession(userName, address, port);
- session.setPassword(password)
- session.setConfig("StrictHostKeyChecking", "no");
- session.setTimeout(THIRTYSECONDS)
- session.connect();
- channel = session.openChannel("sftp");
- channel.connect();
- sftpChannel = (ChannelSftp)channel;
- sftpChannel.cd(workingdir);
- if (!sftpChannel.cd(workingdir)){
- throw new Exception( "Directory $workingdir does not exist.")
- }
- // Store the file
- sftpChannel.put(inputStream, fileNameToStore);
- }
- catch (Exception e){
- error = true;
- failureMessage = e.getMessage();
- errorStr = e.getMessage();
- logger.error "Could not send file to SFTP: " + errorStr
- }
- finally{
- sftpChannel?.exit();
- channel?.disconnect();
- session?.disconnect();
- }
- return [!error, failureMessage] ;
- }
Add Comment
Please, Sign In to add comment