Last active
January 9, 2019 12:40
-
-
Save axeda/6553826 to your computer and use it in GitHub Desktop.
Sends a FileInfo out by FTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.axeda.drm.sdk.Context | |
import static com.axeda.sdk.v2.dsl.Bridges.* | |
import com.axeda.drm.sdk.scripto.Request | |
import com.axeda.drm.sdk.device.Device | |
import com.axeda.drm.sdk.device.DeviceFinder | |
import org.apache.commons.net.ftp.* | |
import groovy.json.* | |
import net.sf.json.JSONObject | |
import com.axeda.drm.sdk.device.Model | |
import com.axeda.drm.sdk.device.ModelFinder | |
/** | |
* OutboundFTP.groovy | |
* ----------------------- | |
* | |
* Sends a file out via FTP. | |
* | |
* @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 = "OutboundFTP.groovy" | |
def root = ["result":["items":[]]] | |
int TENSECONDS = 10*1000 | |
int THIRTYSECONDS = 30*1000 | |
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']) | |
InputStream input = fileInfoBridge.getFileData(fileInfoId) | |
def fileinfo = fileInfoBridge.findById(fileInfoId) | |
try { | |
FTPClient ftp = new FTPClient() | |
ftp.setConnectTimeout(TENSECONDS) | |
ftp.setDefaultTimeout(TENSECONDS) | |
ftp.connect server, port | |
//30 seconds to log on. Also 30 seconds to change to working directory. | |
ftp.setSoTimeout(THIRTYSECONDS) | |
logger.info "Connected to ftp." | |
def reply = ftp.getReplyCode() | |
if (!FTPReply.isPositiveCompletion(reply)) | |
{ | |
ftp.disconnect() | |
logger.error("FTP server refused connection."); | |
return | |
} | |
ftp.login(username, password) | |
if (!ftp.login(username, password)) | |
{ | |
throw new Exception("Unable to login to FTP server") | |
} | |
if (!ftp.changeWorkingDirectory(workingdir)) | |
{ | |
throw new Exception("Unable to change working directory on FTP server") | |
} | |
logger.info "Remote system is $ftp.systemName" | |
ftp.setFileType(FTPClient.BINARY_FILE_TYPE) | |
ftp.enterLocalPassiveMode() | |
logger.info "PUTing file ${fileinfo.label}" | |
ftp.storeFile(fileinfo.label, input); | |
input.close(); | |
logger.info "Done PUTing file ${fileinfo.label}" | |
ftp.logout() | |
ftp.disconnect() | |
logger.info "Disconnected to ftp." | |
} | |
catch (Exception e) | |
{ | |
logger.error e.message | |
logger.error "Unable to connect to ftp server at $server:$port" | |
} | |
} catch (Exception e) { | |
processException(scriptName,json,e) | |
} | |
return ['Content-Type': 'application/json', 'Content': JSONObject.fromObject(root).toString(2)] | |
/* | |
Processes the contents of an Exception and add it to the Errors collection | |
@param json The markup builder | |
*/ | |
private def processException(String scriptName, JsonBuilder json, Exception e) { | |
// catch the exception output | |
def logStringWriter = new StringWriter() | |
e.printStackTrace(new PrintWriter(logStringWriter)) | |
logger.error("Exception occurred in ${scriptName}: ${logStringWriter.toString()}") | |
/* | |
Construct the error response | |
- errorCode Will be an element from an agreed upon enum | |
- errorMessage The text of the exception | |
*/ | |
json.errors { | |
error { | |
message "[${scriptName}]: " + e.getMessage() | |
timestamp "${System.currentTimeMillis()}" | |
} | |
} | |
return json | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment