Fixed Surfing to download all linked content like images and iframes, also added FTP download
This commit is contained in:
parent
d9fac2807a
commit
21e55f12c4
9 changed files with 107 additions and 45 deletions
|
|
@ -18,6 +18,7 @@ public abstract class UeBehaviour {
|
|||
|
||||
if(execListener != null) execListener.executionStarted();
|
||||
try {
|
||||
setProgress(0);
|
||||
execute();
|
||||
} catch(Exception e){
|
||||
if(execListener != null) execListener.exception(e);
|
||||
|
|
|
|||
|
|
@ -1,12 +1,21 @@
|
|||
package com.ericsson.uecontrol.core.behaviour;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.ericsson.uecontrol.core.UeBehaviour;
|
||||
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* This behaviour simulates visiting a website
|
||||
|
|
@ -16,36 +25,88 @@ import java.net.URLConnection;
|
|||
public class UeBehaviourSurfing extends UeBehaviour {
|
||||
|
||||
@Configurable("Web Address")
|
||||
private String strUrl;
|
||||
private String rootUrl;
|
||||
|
||||
private long estimatedDataLength = -1;
|
||||
|
||||
public UeBehaviourSurfing(){
|
||||
this("http://google.com");
|
||||
}
|
||||
public UeBehaviourSurfing(String url){
|
||||
strUrl = url;
|
||||
rootUrl = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute() throws IOException {
|
||||
if(!strUrl.startsWith("http://") &&
|
||||
!strUrl.startsWith("ftp://"))
|
||||
strUrl = "http://"+strUrl;
|
||||
URL url = new URL(strUrl);
|
||||
if(!rootUrl.startsWith("http://"))
|
||||
rootUrl = "http://"+ rootUrl;
|
||||
|
||||
List<URL> urlList = new ArrayList<URL>();
|
||||
urlList.add(new URL(rootUrl));
|
||||
byte[] data = new byte[100];
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setUseCaches(false);
|
||||
connection.connect();
|
||||
InputStream in = connection.getInputStream();
|
||||
IOException retException = null;
|
||||
long totalRead = 0;
|
||||
|
||||
long total = in.available();
|
||||
long progress = 0;
|
||||
long read = 0;
|
||||
for(int i=0; i<urlList.size(); i++) {
|
||||
try {
|
||||
URL url = urlList.get(i);
|
||||
Log.v(getClass().getSimpleName(), "Downloading: " + url);
|
||||
|
||||
while((read = in.read(data)) != -1 && !stopExecution()){
|
||||
progress += read;
|
||||
super.setProgress((float)progress/total);
|
||||
super.setHandledIncomingData(read);
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setUseCaches(false);
|
||||
connection.connect();
|
||||
InputStream in = connection.getInputStream();
|
||||
|
||||
if (estimatedDataLength < 0)
|
||||
estimatedDataLength = in.available();
|
||||
int read = 0;
|
||||
|
||||
StringBuilder content = new StringBuilder();
|
||||
while ((read = in.read(data)) != -1) {
|
||||
if(stopExecution())
|
||||
return;
|
||||
totalRead += read;
|
||||
super.setProgress((float) totalRead / estimatedDataLength);
|
||||
super.setHandledIncomingData(read);
|
||||
content.append(new String(data, 0, read));
|
||||
}
|
||||
getAdditionalContent(url, content.toString(), urlList);
|
||||
}catch(IOException e){
|
||||
e.printStackTrace();
|
||||
if(retException == null) retException = e;
|
||||
}
|
||||
}
|
||||
|
||||
estimatedDataLength = totalRead;
|
||||
if(retException != null)
|
||||
throw retException;
|
||||
}
|
||||
|
||||
private static final Pattern[] CONTENT_INCLUSION_PATTERNS = new Pattern[]{
|
||||
// [\w\/\.-]
|
||||
// HTML
|
||||
Pattern.compile("<img.* src=\"(.*?)\"", Pattern.CASE_INSENSITIVE),
|
||||
Pattern.compile("<iframe.* src=\"(.*?)\"", Pattern.CASE_INSENSITIVE),
|
||||
// CSS
|
||||
Pattern.compile("<link.*rel=\"stylesheet\".*href=\"(.*?)\".*>", Pattern.CASE_INSENSITIVE),
|
||||
Pattern.compile("@import [\"']?(.*?)[\"']?", Pattern.CASE_INSENSITIVE),
|
||||
Pattern.compile("(?:import|background)[\\W:]*url\\([\"']?(?![\"']?data:)(.*?)[\"']??\\)", Pattern.CASE_INSENSITIVE),
|
||||
// Javascript
|
||||
Pattern.compile("<script.* src=\"(.*?)\"", Pattern.CASE_INSENSITIVE)
|
||||
};
|
||||
private void getAdditionalContent(URL baseUrl, String data, List<URL> urlList){
|
||||
for(Pattern pattern : CONTENT_INCLUSION_PATTERNS){
|
||||
Matcher m = pattern.matcher(data);
|
||||
while(m.find()){
|
||||
try {
|
||||
String strUrl = m.group(1);
|
||||
URL url = new URL(baseUrl, strUrl);
|
||||
if(!urlList.contains(url))
|
||||
urlList.add(url);
|
||||
}catch(MalformedURLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -55,9 +116,8 @@ public class UeBehaviourSurfing extends UeBehaviour {
|
|||
return "Surfing";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Will visit "+ strUrl;
|
||||
return "Will visit "+ rootUrl;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ public class EditActivity extends ListActivity implements AdapterView.OnItemClic
|
|||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
final UeBehaviour behaviour = executor.getBehaviourList().get(i);
|
||||
UeBehaviour behaviour = executor.getBehaviourList().get(i);
|
||||
Configurator confer = new Configurator(behaviour);
|
||||
|
||||
ConfigureDialog dialog = new ConfigureDialog();
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import android.view.MenuItem;
|
|||
|
||||
import com.ericsson.uecontrol.R;
|
||||
import com.ericsson.uecontrol.core.UeControlExecutor;
|
||||
import com.ericsson.uecontrol.core.behaviour.UeBehaviourFtp;
|
||||
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep;
|
||||
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing;
|
||||
import com.ericsson.uecontrol.gui.fragments.BehaviourListFragment;
|
||||
|
|
@ -20,8 +21,7 @@ import com.ericsson.uecontrol.gui.fragments.StatusFragment;
|
|||
import com.ericsson.uecontrol.gui.util.CSVWriter;
|
||||
|
||||
|
||||
public class MainActivity extends FragmentActivity
|
||||
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
|
||||
public class MainActivity extends FragmentActivity {
|
||||
|
||||
/**
|
||||
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
|
||||
|
|
@ -42,6 +42,8 @@ public class MainActivity extends FragmentActivity
|
|||
// getFragmentManager().findFragmentById(R.id.navigation_drawer);
|
||||
statusFragment = (StatusFragment)
|
||||
getFragmentManager().findFragmentById(R.id.status_fragment);
|
||||
getFragmentManager().beginTransaction()
|
||||
.replace(R.id.container, BehaviourListFragment.newInstance()).commit();
|
||||
|
||||
// Set up the drawer.
|
||||
//navigationDrawerFragment.setUp(
|
||||
|
|
@ -53,16 +55,8 @@ public class MainActivity extends FragmentActivity
|
|||
currentExecutor.addBehaviour(new UeBehaviourSurfing());
|
||||
currentExecutor.addBehaviour(new UeBehaviourSleep(4000));
|
||||
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
|
||||
onNavigationDrawerItemSelected(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNavigationDrawerItemSelected(int position) {
|
||||
// update the main content by replacing fragments
|
||||
FragmentManager fragmentManager = getFragmentManager();
|
||||
fragmentManager.beginTransaction()
|
||||
.replace(R.id.container, BehaviourListFragment.newInstance()).commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ public class ConfigureDialog extends DialogFragment {
|
|||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT));
|
||||
input.setText(confParam.getString(), TextView.BufferType.EDITABLE);
|
||||
input.setInputType(InputType.TYPE_NULL);
|
||||
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
|
||||
inputs.put(confParam.getName(), input);
|
||||
layout.addView(input);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import java.util.regex.Matcher;
|
|||
import java.util.regex.Pattern;
|
||||
|
||||
public class SelectBehaviourDialog extends DialogFragment {
|
||||
public static String[] behavioursClasses;
|
||||
public static String[] behaviourClasses;
|
||||
public static String[] behaviourNames;
|
||||
|
||||
private BehaviourSelectionCallback callback;
|
||||
|
|
@ -22,17 +22,18 @@ public class SelectBehaviourDialog extends DialogFragment {
|
|||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
// Generate nice behaviour names
|
||||
if(behavioursClasses == null) {
|
||||
behavioursClasses = getResources().getStringArray(R.array.behaviours);
|
||||
behaviourNames = new String[behavioursClasses.length];
|
||||
if(behaviourClasses == null) {
|
||||
behaviourClasses = getResources().getStringArray(R.array.behaviours);
|
||||
behaviourNames = new String[behaviourClasses.length];
|
||||
|
||||
Pattern p = Pattern.compile("[\\w\\.]*\\.(?:UeBehaviour|)(\\w*)");
|
||||
for (int i = 0; i < behavioursClasses.length; i++) {
|
||||
Matcher m = p.matcher(behavioursClasses[i]);
|
||||
if(m.find())
|
||||
behaviourNames[i] = m.group(1);
|
||||
else
|
||||
behaviourNames[i] = behavioursClasses[i];
|
||||
for (int i = 0; i < behaviourClasses.length; i++) {
|
||||
try {
|
||||
UeBehaviour obj = (UeBehaviour) Class.forName(behaviourClasses[i]).newInstance();
|
||||
behaviourNames[i] = obj.getName();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
behaviourNames[i] = behaviourClasses[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,7 +44,7 @@ public class SelectBehaviourDialog extends DialogFragment {
|
|||
public void onClick(DialogInterface dialog, int which) {
|
||||
try {
|
||||
if (callback != null){
|
||||
Class objClass = Class.forName(behavioursClasses[which]);
|
||||
Class objClass = Class.forName(behaviourClasses[which]);
|
||||
callback.behaviourSelected(behaviourNames[which], objClass);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
|||
|
|
@ -35,8 +35,10 @@ public class CSVWriter {
|
|||
|
||||
public CSVWriter(Context context){
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String path = prefs.getString("logging_path", "/sdcard/uecontrol/");
|
||||
if(!path.endsWith("/")) path += "/";
|
||||
file = new File(
|
||||
prefs.getString("logging_path", "/sdcard/uecontrol/"),
|
||||
path,
|
||||
"log_"+fileDateFormater.format(System.currentTimeMillis())+".log");
|
||||
|
||||
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
|
|
|||
|
|
@ -91,7 +91,10 @@ public class Configurator {
|
|||
public String getNiceName(){return niceName;}
|
||||
public ConfigType getType(){return type;}
|
||||
|
||||
public String getString(){return value.toString();}
|
||||
public String getString(){
|
||||
if(value == null)
|
||||
return null;
|
||||
return value.toString();}
|
||||
|
||||
public void setString(String v){
|
||||
switch(type){
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string-array name="behaviours" >
|
||||
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourFtp</item>
|
||||
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep</item>
|
||||
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing</item>
|
||||
</string-array>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue