Method from org.apache.jmeter.protocol.ftp.sampler.FTPSampler Detail: |
public String getLabel() {
StrBuilder sb = new StrBuilder();
sb.setNullText("null");// $NON-NLS-1$
sb.append("ftp://");// $NON-NLS-1$
sb.append(getServer());
String port = getPort();
if (port.length() > 0){
sb.append(':');
sb.append(port);
}
sb.append("/");// $NON-NLS-1$
sb.append(getRemoteFilename());
sb.append(isBinaryMode() ? " (Binary) " : " (Ascii) ");// $NON-NLS-1$ $NON-NLS-2$
sb.append(isUpload() ? " < - " : " - > "); // $NON-NLS-1$ $NON-NLS-2$
sb.append(getLocalFilename());
return sb.toString();
}
Returns a formatted string label describing this sampler Example output:
ftp://ftp.nowhere.com/pub/README.txt |
public String getLocalFilename() {
return getPropertyAsString(LOCAL_FILENAME);
}
|
public String getPassword() {
return getPropertyAsString(ConfigTestElement.PASSWORD);
}
|
public String getPort() {
return getPropertyAsString(PORT, ""); // $NON-NLS-1$
}
|
public int getPortAsInt() {
return getPropertyAsInt(PORT, 0);
}
|
public String getRemoteFilename() {
return getPropertyAsString(REMOTE_FILENAME);
}
|
public String getServer() {
return getPropertyAsString(SERVER);
}
|
public String getUsername() {
return getPropertyAsString(ConfigTestElement.USERNAME);
}
|
public boolean interrupt() {
FTPClient client = savedClient;
if (client != null) {
savedClient = null;
try {
client.abort();
client.disconnect();
} catch (IOException ignored) {
}
}
return client != null;
}
|
public boolean isBinaryMode() {
return getPropertyAsBoolean(BINARY_MODE,false);
}
|
public boolean isSaveResponse() {
return getPropertyAsBoolean(SAVE_RESPONSE,false);
}
|
public boolean isUpload() {
return getPropertyAsBoolean(UPLOAD_FILE,false);
}
|
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;
OutputStream output = 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){
byte bytes[] = contents.getBytes();// TODO this assumes local encoding
input = new ByteArrayInputStream(bytes);
res.setBytes(bytes.length);
} else {
File infile = new File(local);
res.setBytes((int)infile.length());
input = new FileInputStream(infile);
}
ftpOK = ftp.storeFile(remote, input);
} else {
final boolean saveResponse = isSaveResponse();
ByteArrayOutputStream baos=null; // No need to close this
OutputStream target=null; // No need to close this
if (saveResponse){
baos = new ByteArrayOutputStream();
target=baos;
}
if (local.length() >0){
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((int) bytes);
}
}
}
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"); // TODO
res.setResponseMessage("Could not connect");
//res.setResponseCode(Integer.toString(ftp.getReplyCode()));
res.setResponseMessage(ftp.getReplyString());
}
} catch (IOException ex) {
res.setResponseCode("000"); // TODO
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(output);
}
res.sampleEnd();
return res;
}
|
public void setPort(String newPort) {
this.setProperty(PORT, newPort, ""); // $NON-NLS-1$
}
|
public void setServer(String newServer) {
this.setProperty(SERVER, newServer);
}
|