Refactored FTP Upload and Download behaviours.

Made FTP behaviours to use Passive mode instead of active.
[artf448994]
This commit is contained in:
Ziver Koc 2014-11-20 17:40:17 +01:00
parent 5ca06373bb
commit e4936d9f33
84 changed files with 341 additions and 105 deletions

View file

@ -0,0 +1,68 @@
package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.UeBehaviour;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import java.io.IOException;
/**
* This behaviour simulates downloading a file from FTP
*
* Created by ezivkoc on 2014-07-15.
*/
public abstract class UeBehaviourFtp extends UeBehaviour {
private static final Logger log = Logger.getLogger(UeBehaviourFtp.class);
final protected void execute() throws IOException {
FTPClient ftp = new FTPClient();
try {
// Connect
ftp.setConnectTimeout(5000);
ftp.setDataTimeout(3000);
if (getHost().contains(":")) {
String tmpHost = getHost().substring(0, getHost().indexOf(':'));
int tmpPort = Integer.parseInt(getHost().substring(getHost().indexOf(':')));
ftp.connect(tmpHost, tmpPort);
} else ftp.connect(getHost());
if(getUsername() != null && !getUsername().isEmpty())
ftp.login(getUsername(), getPassword());
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
executeFtp(ftp);
// Disconnect
ftp.logout();
} finally {
if(ftp.isConnected()) {
ftp.disconnect();
}
}
}
protected String getFtpUrl(String filePath) {
String url = "ftp://";
if(getUsername() != null && !getUsername().isEmpty()) {
url += getUsername();
if(getPassword() !=null && !getPassword().isEmpty())
url += ":" + getPassword();
url += "@";
}
url += getHost();
if(filePath != null && !filePath.isEmpty()){
if(!filePath.startsWith("/"))
filePath = "/"+filePath;
url += filePath;
}
return url;
}
abstract protected void executeFtp(FTPClient client) throws IOException;
abstract String getHost();
abstract String getUsername();
abstract String getPassword();
}

View file

@ -2,15 +2,19 @@ package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import java.io.*;
/**
* This behaviour simulates downloading a file from FTP
*
* Created by ezivkoc on 2014-07-15.
*/
public class UeBehaviourFtpDownload extends UeBehaviourFileDownload {
public class UeBehaviourFtpDownload extends UeBehaviourFtp {
private static final Logger log = Logger.getLogger(UeBehaviourFtpDownload.class);
private static final int BUFFER_SIZE = 512;
@Configurable("Host")
private String host = "ftp.otenet.gr";
@ -21,23 +25,46 @@ public class UeBehaviourFtpDownload extends UeBehaviourFileDownload {
@Configurable("Password")
private String password = "speedtest";
private transient long estimatedDataLength = -1;
@Override
protected String getFileUrl() {
String url = "ftp://";
if(username != null && !username.isEmpty()) {
url += username;
if(password !=null && !password.isEmpty())
url += ":"+password;
url += "@";
protected void executeFtp(FTPClient ftp) throws IOException {
// Download data
InputStream inStream = ftp.retrieveFileStream(filePath);
if (inStream != null) {
InputStream in = new BufferedInputStream(inStream);
long progress = 0;
long read = 0;
byte[] data = new byte[BUFFER_SIZE];
while((read = in.read(data)) != -1 && !stopExecution()){
progress += read;
super.setProgress((float)progress/estimatedDataLength);
}
estimatedDataLength = progress;
in.close();
if(!ftp.completePendingCommand()) {
throw new IOException("FTP reply: "+ftp.getReplyString());
}
} else {
throw new IOException("FTP reply: "+ftp.getReplyString());
}
url += host;
if(filePath != null && !filePath.isEmpty()){
if(!filePath.startsWith("/"))
filePath = "/"+filePath;
url += filePath;
}
return url;
}
@Override
public String getHost() {
return host;
}
@Override
public String getUsername() {
return username;
}
@Override
public String getPassword() {
return password;
}
@ -46,4 +73,8 @@ public class UeBehaviourFtpDownload extends UeBehaviourFileDownload {
return "FTP Download";
}
@Override
public String toString() {
return "Will download " + getFtpUrl(filePath);
}
}

View file

@ -21,7 +21,7 @@ import java.net.URLConnection;
*
* Created by ezivkoc on 2014-07-15.
*/
public class UeBehaviourFtpUpload extends UeBehaviour {
public class UeBehaviourFtpUpload extends UeBehaviourFtp {
private static final int BUFFER_SIZE = 512;
private static final Logger log = Logger.getLogger(UeBehaviourFtpUpload.class);
@ -38,85 +38,42 @@ public class UeBehaviourFtpUpload extends UeBehaviour {
@Override
protected void execute() throws IOException {
FTPClient ftp = new FTPClient();
try {
// Connect
ftp.setConnectTimeout(3000);
ftp.setDataTimeout(200);
if (host.contains(":")) {
String tmpHost = host.substring(0, host.indexOf(':'));
int tmpPort = Integer.parseInt(host.substring(host.indexOf(':')));
ftp.connect(tmpHost, tmpPort);
} else ftp.connect(host);
ftp.login(username, password);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
protected void executeFtp(FTPClient ftp) throws IOException {
// Upload data
OutputStream outStream = ftp.storeFileStream(filePath);
if (outStream != null) {
OutputStream out = new BufferedOutputStream(outStream);
int total = 0;
byte[] data = new byte[BUFFER_SIZE];
while (total < size && !stopExecution()) {
int writeLength = (total + data.length < size ? data.length : size - total);
out.write(data, 0, writeLength);
// Upload data
OutputStream outStream = ftp.storeFileStream(filePath);
if (outStream != null) {
OutputStream out = new BufferedOutputStream(outStream);
int total = 0;
byte[] data = new byte[BUFFER_SIZE];
while (total < size && !stopExecution()) {
int writeLength = (total + data.length < size ? data.length : size - total);
out.write(data, 0, writeLength);
total += writeLength;
super.setProgress((float) total / size);
}
out.close();
if(!ftp.completePendingCommand()) {
throw new IOException("FTP reply: "+ftp.getReplyString());
}
} else {
total += writeLength;
super.setProgress((float) total / size);
}
out.close();
if(!ftp.completePendingCommand()) {
throw new IOException("FTP reply: "+ftp.getReplyString());
}
// Disconnect
ftp.logout();
} finally {
if(ftp.isConnected()) {
ftp.disconnect();
}
} else {
throw new IOException("FTP reply: "+ftp.getReplyString());
}
}
/* @Override
protected void execute() throws IOException {
URL url = new URL(getFileUrl());
log.debug("Uploading file: " + url);
byte[] data = new byte[BUFFER_SIZE];
URLConnection connection = url.openConnection();
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
int total = 0;
while(total < size && !stopExecution()){
int writeLength = (total+data.length < size ? data.length : total+data.length-size);
out.write(data, 0, writeLength);
total += writeLength;
super.setProgress((float)total/size);
}
out.close();
}
*/
protected String getFileUrl() {
String url = "ftp://";
if(username != null && !username.isEmpty()) {
url += username;
if(password !=null && !password.isEmpty())
url += ":"+password;
url += "@";
}
url += host;
if(filePath != null && !filePath.isEmpty()){
if(!filePath.startsWith("/"))
filePath = "/"+filePath;
url += filePath;
}
return url;
@Override
public String getHost() {
return host;
}
@Override
public String getUsername() {
return username;
}
@Override
public String getPassword() {
return password;
}

View file

@ -4,9 +4,12 @@ import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import com.ericsson.uecontrol.gui.MainActivity;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
@ -14,15 +17,24 @@ import java.net.URLConnection;
* Created by ezivkoc on 2014-09-18.
*/
public class UrlUtil {
private static final Logger log = Logger.getLogger(UrlUtil.class);
public static URLConnection getURLConnection(URL url) throws IOException {
if(!isNetworkAvailable())
throw new IOException("No Data Network Available");
URLConnection connection = url.openConnection();
final URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(3000);
connection.setUseCaches(false);
/*new Thread(new Runnable() {
public void run() {
try{Thread.sleep(5000);}catch(Exception e){};
log.error("Force disconnect URLConnection");
connection.
}
}).start();*/
connection.connect();
return connection;

View file

@ -0,0 +1,47 @@
package com.ericsson.uecontrol.gui.util;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import com.ericsson.uecontrol.gui.MainActivity;
import org.apache.log4j.Logger;
import java.util.List;
/**
* Created by ezivkoc on 2014-11-05.
*/
public class WifiScanner {
private static final Logger log = Logger.getLogger(WifiScanner.class);
private WifiManager mainWifi;
private WifiReceiver receiverWifi;
public WifiScanner() {
mainWifi = (WifiManager) MainActivity.getContext().getSystemService(MainActivity.getContext().WIFI_SERVICE);
receiverWifi = new WifiReceiver();
}
protected void start() {
MainActivity.getContext().registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mainWifi.startScan();
}
protected void stop() {
MainActivity.getContext().unregisterReceiver(receiverWifi);
}
class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> scanList = mainWifi.getScanResults();
for(ScanResult item : scanList){
log.debug("WiFi scan found SSID: "+item);
}
}
}
}