* @see smilehouse.opensyncro.user.pipes.Destination#take(java.lang.String,
* smilehouse.opensyncro.user.pipes.DestinationInfo, smilehouse.opensyncro.user.pipes.log.MessageLogger)
*/
public void take(String data, DestinationInfo info, MessageLogger logger) throws FailTransferException, AbortTransferException {
FTPClient ftp = new FTPClient();
try {
// -----------------
// Try to connect...
// -----------------
String host = this.data.getAttribute(HOST_ATTR);
int port = -1;
String portStr = this.data.getAttribute(PORT_ATTR);
if(portStr != null && portStr.length() > 0) {
try {
port = Integer.parseInt(portStr);
} catch(NumberFormatException nfe) {
logger.logMessage(
"Invalid value '" + portStr + "' for port.",
this,
MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
}
int reply;
try {
if(port != -1) {
ftp.connect(host, port);
} else {
ftp.connect(host);
}
} catch(SocketException e) {
logger.logMessage("SocketException while connecting to host " + host + ", aborting", this, MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
} catch(IOException e) {
logger.logMessage("IOException while connecting to host " + host + ", aborting", this, MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
try {
ftp.disconnect();
} catch (IOException e) {
/**
* ftp.disconnect() is called only as additional clean-up here, so we choose to
* ignore possible exceptions
*/
}
logger.logMessage(
"Couldn't connect to the FTP server: " + ftp.getReplyString(),
this,
MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
// -----------
// Then log in
// -----------
try {
if(!ftp.login(this.data.getAttribute(USER_ATTR), this.data.getAttribute(PASSWORD_ATTR))) {
logger.logMessage(
"Could not log in, check your username and password settings.",
this,
MessageLogger.ERROR);
ftp.logout();
PipeComponentUtils.failTransfer();
}
} catch(IOException e) {
logger.logMessage("IOException while logging in to FTP server", this, MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
// Use passive mode
ftp.enterLocalPassiveMode();
// -----------------
// ASCII or binary ?
// -----------------
boolean fileTypeSetOk = false;
try {
switch(getFileType()) {
case FILE_TYPE_ASCII:
fileTypeSetOk = ftp.setFileType(FTP.ASCII_FILE_TYPE);
break;
case FILE_TYPE_BINARY:
fileTypeSetOk = ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
if(!fileTypeSetOk) {
logger.logMessage(
"Could not set file type: " + ftp.getReplyString(),
this,
MessageLogger.WARNING);
}
} catch(IOException e) {
logger.logMessage("IOException while setting file transfer type parameter", this, MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
// -------------
// Send the data
// -------------
String fileName = getFileName();
logger.logMessage("Storing file: " + fileName, this, MessageLogger.DEBUG);
String charSet=this.data.getAttribute(CHARSET_ATTR);
if(charSet == null || charSet.length() == 0)
charSet = DEFAULT_CHARSET;
InputStream dataStream=null;
try {
dataStream = new ByteArrayInputStream(data.getBytes(charSet));
} catch (UnsupportedEncodingException e1) {
logger.logMessage(charSet+" charset not supported", this, MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
try {
if(!ftp.storeFile(fileName, dataStream)) {
logger.logMessage("Could not store file '" + fileName + "': "
+ ftp.getReplyString(), this, MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
} catch (IOException e) {
logger.logMessage("IOException while uploading the file to FTP server", this, MessageLogger.ERROR);
PipeComponentUtils.failTransfer();
}
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException f) {
// do nothing
}
}
}