if (FTPReply.isPositiveCompletion(reply)) {
// yes we could connect
connected = true;
} else {
// throw an exception to force the retry logic in the catch exception block
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), "Server refused connection");
}
} catch (Exception e) {
// check if we are interrupted so we can break out
if (Thread.currentThread().isInterrupted()) {
throw new GenericFileOperationFailedException("Interrupted during connecting", new InterruptedException("Interrupted during connecting"));
}
GenericFileOperationFailedException failed;
if (e instanceof GenericFileOperationFailedException) {
failed = (GenericFileOperationFailedException) e;
} else {
failed = new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
log.trace("Cannot connect due: {}", failed.getMessage());
attempt++;
if (attempt > endpoint.getMaximumReconnectAttempts()) {
throw failed;
}
if (endpoint.getReconnectDelay() > 0) {
try {
Thread.sleep(endpoint.getReconnectDelay());
} catch (InterruptedException ie) {
// we could potentially also be interrupted during sleep
Thread.currentThread().interrupt();
throw new GenericFileOperationFailedException("Interrupted during sleeping", ie);
}
}
}
}
// must enter passive mode directly after connect
if (configuration.isPassiveMode()) {
log.trace("Using passive mode connections");
client.enterLocalPassiveMode();
}
// must set soTimeout after connect
if (endpoint instanceof FtpEndpoint) {
FtpEndpoint ftpEndpoint = (FtpEndpoint) endpoint;
if (ftpEndpoint.getSoTimeout() > 0) {
log.trace("Using SoTimeout=" + ftpEndpoint.getSoTimeout());
try {
client.setSoTimeout(ftpEndpoint.getSoTimeout());
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
}
}
try {
boolean login;
if (username != null) {
log.trace("Attempting to login user: {} using password: {}", username, configuration.getPassword());
login = client.login(username, configuration.getPassword());
} else {
log.trace("Attempting to login anonymous");
login = client.login("anonymous", "");
}
log.trace("User {} logged in: {}", username != null ? username : "anonymous", login);
if (!login) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString());
}
client.setFileType(configuration.isBinary() ? FTPClient.BINARY_FILE_TYPE : FTPClient.ASCII_FILE_TYPE);
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
// site commands
if (endpoint.getConfiguration().getSiteCommand() != null) {
// commands can be separated using new line
Iterator it = ObjectHelper.createIterator(endpoint.getConfiguration().getSiteCommand(), "\n");
while (it.hasNext()) {
Object next = it.next();
String command = endpoint.getCamelContext().getTypeConverter().convertTo(String.class, next);
log.trace("Site command to send: {}", command);
if (command != null) {
boolean result = sendSiteCommand(command);
if (!result) {
throw new GenericFileOperationFailedException("Site command: " + command + " returned false");
}
}
}
}