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) {
GenericFileOperationFailedException failed;
if (e instanceof GenericFileOperationFailedException) {
failed = (GenericFileOperationFailedException) e;
} else {
failed = new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
if (log.isTraceEnabled()) {
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) {
// ignore
}
}
}
}
// 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) {
if (log.isTraceEnabled()) {
log.trace("Attempting to login user: " + username + " using password: " + configuration.getPassword());
}
login = client.login(username, configuration.getPassword());
} else {
log.trace("Attempting to login anonymous");
login = client.login("anonymous", null);
}
if (log.isTraceEnabled()) {
log.trace("User " + (username != null ? username : "anonymous") + " logged in: " + 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);
}
return true;
}