use of org.apache.commons.net.ftp.FTPClient in project jmeter by apache.
the class FTPSampler method sample.
@Override
public SampleResult sample(Entry e) {
SampleResult res = new SampleResult();
// Assume failure
res.setSuccessful(false);
String remote = getRemoteFilename();
String local = getLocalFilename();
boolean binaryTransfer = isBinaryMode();
res.setSampleLabel(getName());
final String label = getLabel();
res.setSamplerData(label);
try {
res.setURL(new URL(label));
} catch (MalformedURLException e1) {
log.warn("Cannot set URL: " + e1.getLocalizedMessage());
}
InputStream input = null;
FileInputStream fileIS = null;
res.sampleStart();
FTPClient ftp = new FTPClient();
try {
savedClient = ftp;
final int port = getPortAsInt();
if (port > 0) {
ftp.connect(getServer(), port);
} else {
ftp.connect(getServer());
}
res.latencyEnd();
int reply = ftp.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
if (ftp.login(getUsername(), getPassword())) {
if (binaryTransfer) {
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
// should probably come from the setup dialog
ftp.enterLocalPassiveMode();
boolean ftpOK = false;
if (isUpload()) {
String contents = getLocalFileContents();
if (contents.length() > 0) {
// TODO - charset?
byte[] bytes = contents.getBytes();
input = new ByteArrayInputStream(bytes);
res.setBytes((long) bytes.length);
} else {
File infile = new File(local);
res.setBytes(infile.length());
// NOSONAR False positive, fileIS is closed in finally and not overwritten
fileIS = new FileInputStream(infile);
input = new BufferedInputStream(fileIS);
}
ftpOK = ftp.storeFile(remote, input);
} else {
final boolean saveResponse = isSaveResponse();
// No need to close this
ByteArrayOutputStream baos = null;
OutputStream target = null;
OutputStream output = null;
try {
if (saveResponse) {
baos = new ByteArrayOutputStream();
target = baos;
}
if (local.length() > 0) {
// NOSONAR False positive, the output is closed in finally and not overwritten
output = new FileOutputStream(local);
if (target == null) {
target = output;
} else {
target = new TeeOutputStream(output, baos);
}
}
if (target == null) {
target = new NullOutputStream();
}
input = ftp.retrieveFileStream(remote);
if (input == null) {
// Could not access file or other error
res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
} else {
long bytes = IOUtils.copy(input, target);
ftpOK = bytes > 0;
if (saveResponse && baos != null) {
res.setResponseData(baos.toByteArray());
if (!binaryTransfer) {
res.setDataType(SampleResult.TEXT);
}
} else {
res.setBytes(bytes);
}
}
} finally {
IOUtils.closeQuietly(target);
IOUtils.closeQuietly(output);
}
}
if (ftpOK) {
res.setResponseCodeOK();
res.setResponseMessageOK();
res.setSuccessful(true);
} else {
res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
}
} else {
res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
}
} else {
// TODO
res.setResponseCode("501");
res.setResponseMessage("Could not connect");
//res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
}
} catch (IOException ex) {
// TODO
res.setResponseCode("000");
res.setResponseMessage(ex.toString());
} finally {
savedClient = null;
if (ftp.isConnected()) {
try {
ftp.logout();
} catch (IOException ignored) {
}
try {
ftp.disconnect();
} catch (IOException ignored) {
}
}
IOUtils.closeQuietly(input);
IOUtils.closeQuietly(fileIS);
}
res.sampleEnd();
return res;
}
use of org.apache.commons.net.ftp.FTPClient in project ddf by codice.
the class TestFtp method createInsecureClient.
private FTPClient createInsecureClient() throws Exception {
FTPClient ftp = new FTPClient();
int attempts = 0;
while (true) {
try {
ftp.connect(FTP_SERVER, Integer.parseInt(FTP_PORT.getPort()));
break;
} catch (SocketException e) {
//a socket exception can be thrown if the ftp server is still in the process of coming up or down
Thread.sleep(1000);
if (attempts++ > 30) {
throw e;
}
}
}
showServerReply(ftp);
int connectionReply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(connectionReply)) {
fail("FTP server refused connection: " + connectionReply);
}
boolean success = ftp.login(USERNAME, PASSWORD);
showServerReply(ftp);
if (!success) {
fail("Could not log in to the FTP server.");
}
ftp.enterLocalPassiveMode();
ftp.setControlKeepAliveTimeout(300);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
return ftp;
}
Aggregations