in src/protocol/ftp/src/main/java/org/apache/jmeter/protocol/ftp/sampler/FTPSampler.java [172:306]
public SampleResult sample(Entry e) {
SampleResult res = new SampleResult();
res.setSuccessful(false); // Assume failure
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);
}
ftp.enterLocalPassiveMode();// should probably come from the setup dialog
boolean ftpOK=false;
if (isUpload()) {
String contents=getLocalFileContents();
if (contents.length() > 0){
@SuppressWarnings("DefaultCharset")
byte[] bytes = contents.getBytes(); // TODO - charset?
input = new ByteArrayInputStream(bytes);
res.setSentBytes((long)bytes.length);
} else {
File infile = new File(local);
res.setSentBytes(infile.length());
fileIS = new FileInputStream(infile); // NOSONAR False positive, fileIS is closed in finally and not overwritten
input = new BufferedInputStream(fileIS);
}
ftpOK = ftp.storeFile(remote, input);
} else {
final boolean saveResponse = isSaveResponse();
ByteArrayOutputStream baos=null; // No need to close this
OutputStream target=null;
OutputStream output = null;
try {
if (saveResponse){
baos = new ByteArrayOutputStream();
target=baos;
}
if (local.length()>0){
output=new FileOutputStream(local); // NOSONAR False positive, the output is closed in finally and not overwritten
if (target==null) {
target=output;
} else {
target = new TeeOutputStream(output,baos);
}
}
if (target == null){
target = NullOutputStream.INSTANCE;
}
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) {
saveResponse(res, binaryTransfer, baos);
} else {
res.setBytes(bytes);
}
}
} finally {
IOUtils.closeQuietly(target, null);
IOUtils.closeQuietly(output, null);
}
}
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 {
res.setResponseCode("501");
String replyString = ftp.getReplyString();
if(StringUtils.isEmpty(replyString)) {
res.setResponseMessage("Could not connect");
}
else {
res.setResponseMessage(replyString);
}
}
} catch (IOException ex) {
res.setResponseCode("000");
res.setResponseMessage(ex.toString());
} finally {
savedClient = null;
if (ftp.isConnected()) {
try {
ftp.logout();
} catch (IOException ignored) {
// NOOP
}
try {
ftp.disconnect();
} catch (IOException ignored) {
// NOOP
}
}
IOUtils.closeQuietly(input, null);
IOUtils.closeQuietly(fileIS, null);
}
res.sampleEnd();
return res;
}