use of org.apache.commons.net.ftp.FTPClient in project ant by apache.
the class FTP method execute.
/**
* Runs the task.
*
* @throws BuildException if the task fails or is not configured
* correctly.
*/
@Override
public void execute() throws BuildException {
checkAttributes();
FTPClient ftp = null;
try {
log("Opening FTP connection to " + server, Project.MSG_VERBOSE);
ftp = new FTPClient();
if (this.isConfigurationSet) {
ftp = FTPConfigurator.configure(ftp, this);
}
ftp.setRemoteVerificationEnabled(enableRemoteVerification);
ftp.connect(server, port);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("FTP connection failed: %s", ftp.getReplyString());
}
log("connected", Project.MSG_VERBOSE);
log("logging in to FTP server", Project.MSG_VERBOSE);
if ((this.account != null && !ftp.login(userid, password, account)) || (this.account == null && !ftp.login(userid, password))) {
throw new BuildException("Could not login to FTP server");
}
log("login succeeded", Project.MSG_VERBOSE);
if (binary) {
ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not set transfer type: %s", ftp.getReplyString());
}
} else {
ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not set transfer type: %s", ftp.getReplyString());
}
}
if (passive) {
log("entering passive mode", Project.MSG_VERBOSE);
ftp.enterLocalPassiveMode();
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not enter into passive mode: %s", ftp.getReplyString());
}
}
// a legacy file system.
if (this.initialSiteCommand != null) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> doSiteCommand(lftp, FTP.this.initialSiteCommand), "initial site command: " + this.initialSiteCommand);
}
if (umask != null) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> doSiteCommand(lftp, "umask " + umask), "umask " + umask);
}
if (action == MK_DIR) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> makeRemoteDir(lftp, remotedir), remotedir);
} else if (action == SITE_CMD) {
final FTPClient lftp = ftp;
executeRetryable(new RetryHandler(this.retriesAllowed, this), () -> doSiteCommand(lftp, FTP.this.siteCommand), "Site Command: " + this.siteCommand);
} else {
if (remotedir != null) {
log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE);
ftp.changeWorkingDirectory(remotedir);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new BuildException("could not change remote directory: %s", ftp.getReplyString());
}
}
if (newerOnly && timeDiffAuto) {
// in this case we want to find how much time span there is between local
// and remote
timeDiffMillis = getTimeDiff(ftp);
}
log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]);
transferFiles(ftp);
}
} catch (IOException ex) {
throw new BuildException("error during FTP transfer: " + ex, ex);
} finally {
if (ftp != null && ftp.isConnected()) {
try {
log("disconnecting", Project.MSG_VERBOSE);
ftp.logout();
ftp.disconnect();
} catch (IOException ex) {
// ignore it
}
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project teiid by teiid.
the class FtpManagedConnectionFactory method createClient.
protected FTPClient createClient() throws IOException, ResourceException {
FTPClient client = createClientInstance();
beforeConnectProcessing(client);
client.connect(this.host, this.port);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
// $NON-NLS-1$
throw new ResourceException(UTIL.getString("ftp_connect_failed", this.host, this.port));
}
if (!client.login(this.username, this.password)) {
// $NON-NLS-1$
throw new IllegalStateException(UTIL.getString("ftp_login_failed", client.getReplyString()));
}
afterConnectProcessing(client);
return client;
}
use of org.apache.commons.net.ftp.FTPClient in project teiid by teiid.
the class TestFtpFileSystem method writeFile.
private void writeFile(String path) throws ResourceException, IOException {
VirtualFile file = VFS.getChild(path);
FtpManagedConnectionFactory mcf = new FtpManagedConnectionFactory();
// $NON-NLS-1$
mcf.setParentDirectory("/home/kylin/vsftpd");
// $NON-NLS-1$
mcf.setHost("10.66.192.120");
mcf.setPort(21);
// $NON-NLS-1$
mcf.setUsername("kylin");
// $NON-NLS-1$
mcf.setPassword("redhat");
FTPClient client = mcf.createClient();
client.storeFile(file.getName(), file.openStream());
client.disconnect();
}
use of org.apache.commons.net.ftp.FTPClient in project spring-integration by spring-projects.
the class SessionFactoryTests method testTimeouts.
@Test
public void testTimeouts() throws Exception {
final FTPClient client = mock(FTPClient.class);
DefaultFtpSessionFactory sessionFactory = new DefaultFtpSessionFactory() {
@Override
protected FTPClient createClientInstance() {
return client;
}
};
sessionFactory.setUsername("foo");
sessionFactory.setConnectTimeout(123);
sessionFactory.setDefaultTimeout(456);
sessionFactory.setDataTimeout(789);
doReturn(200).when(client).getReplyCode();
doReturn(true).when(client).login("foo", null);
sessionFactory.getSession();
verify(client).setConnectTimeout(123);
verify(client).setDefaultTimeout(456);
verify(client).setDataTimeout(789);
}
use of org.apache.commons.net.ftp.FTPClient in project pwm by pwm-project.
the class FtpTelemetrySender method ftpPut.
private void ftpPut(final TelemetryPublishBean telemetryPublishBean) throws PwmUnrecoverableException {
final FTPClient ftpClient;
switch(settings.getFtpMode()) {
case ftp:
ftpClient = new FTPClient();
break;
case ftps:
ftpClient = new FTPSClient();
break;
default:
JavaHelper.unhandledSwitchStatement(settings.getFtpMode());
throw new UnsupportedOperationException();
}
// connect
try {
LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL, "establishing " + settings.getFtpMode() + " connection to " + settings.getHost());
ftpClient.connect(settings.getHost());
final int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnectFtpClient(ftpClient);
final String msg = "error " + reply + " connecting to " + settings.getHost();
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL, "connected to " + settings.getHost());
} catch (IOException e) {
disconnectFtpClient(ftpClient);
final String msg = "unable to connect to " + settings.getHost() + ", error: " + e.getMessage();
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
// set modes
try {
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
final int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnectFtpClient(ftpClient);
final String msg = "error setting file type mode to binary, error=" + reply;
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
} catch (IOException e) {
disconnectFtpClient(ftpClient);
final String msg = "unable to connect to " + settings.getHost() + ", error: " + e.getMessage();
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
// authenticate
try {
ftpClient.login(settings.getUsername(), settings.getPassword());
final int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnectFtpClient(ftpClient);
final String msg = "error authenticating as " + settings.getUsername() + " to " + settings.getHost() + ", error=" + reply;
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL, "authenticated to " + settings.getHost() + " as " + settings.getUsername());
} catch (IOException e) {
disconnectFtpClient(ftpClient);
final String msg = "error authenticating as " + settings.getUsername() + " to " + settings.getHost() + ", error: " + e.getMessage();
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
// upload
try {
final String filePath = settings.getPath() + "/" + telemetryPublishBean.getId() + ".zip";
final byte[] fileBytes = dataToJsonZipFile(telemetryPublishBean);
final ByteArrayInputStream fileStream = new ByteArrayInputStream(fileBytes);
LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL, "preparing to transfer " + fileBytes.length + " bytes to file path " + filePath);
final Instant startTime = Instant.now();
ftpClient.storeFile(filePath, fileStream);
final int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
disconnectFtpClient(ftpClient);
final String msg = "error uploading file to " + settings.getHost() + ", error=" + reply;
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
LOGGER.trace(SessionLabel.TELEMETRY_SESSION_LABEL, "completed transfer of " + fileBytes.length + " in " + TimeDuration.compactFromCurrent(startTime));
} catch (IOException e) {
disconnectFtpClient(ftpClient);
final String msg = "error uploading file to " + settings.getHost() + ", error: " + e.getMessage();
throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_TELEMETRY_SEND_ERROR, msg));
}
}
Aggregations