From 23c7adb9f1b9df0c604ac0305e971f618506832d Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Mon, 12 Mar 2012 16:50:17 +0100 Subject: [PATCH] Support for new style Xirvik shared servers. --- android/AndroidManifest.xml | 2 +- android/project.properties | 2 +- android/res/values/arrays.xml | 4 +- android/res/values/strings.xml | 3 + .../preferences/PreferencesXirvikServer.java | 313 +++++++++++------- .../preferences/XirvikServerType.java | 6 +- .../preferences/XirvikSettings.java | 39 +-- .../transdroid/preferences/Preferences.java | 5 +- .../daemon/Rtorrent/RtorrentAdapter.java | 2 +- .../transdroid/daemon/util/HttpHelper.java | 36 +- 10 files changed, 248 insertions(+), 164 deletions(-) diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 7c97b5c9..57ab25de 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -22,7 +22,7 @@ android:versionCode="145" android:installLocation="auto"> - + diff --git a/android/project.properties b/android/project.properties index b6221954..44fd2acb 100644 --- a/android/project.properties +++ b/android/project.properties @@ -10,6 +10,6 @@ # Indicates whether an apk should be generated for each density. split.density=false # Project target. -target=android-13 +target=android-14 apk-configurations= android.library.reference.1=../external/JakeWharton-ActionBarSherlock/library diff --git a/android/res/values/arrays.xml b/android/res/values/arrays.xml index 81daccb5..6e211da2 100644 --- a/android/res/values/arrays.xml +++ b/android/res/values/arrays.xml @@ -43,12 +43,14 @@ - Shared + Shared (Torrentflux) + Shared (rTorrent) Semi-dedicated Dedicated type_shared + type_sharedrt type_semi type_dedicated diff --git a/android/res/values/strings.xml b/android/res/values/strings.xml index 98741ac9..eb4c54d7 100644 --- a/android/res/values/strings.xml +++ b/android/res/values/strings.xml @@ -363,7 +363,10 @@ Shared, semi- or dedicated Server name Like dedi000.xirvik.com +SCGI mount +Set automatically Invalid server (use the full host name, like dedi000.xirvik.com) +Cannot retrieve the Xirvik SCGI folder setting; please try again later ro correct your server name setting SeedM8 offers unmetered GBit seedbox hosting. Transdroid provides easy setup for SeedM8 servers.\n\nRead more at www.seedm8.com Add SeedM8 server diff --git a/android/src/com/xirvik/transdroid/preferences/PreferencesXirvikServer.java b/android/src/com/xirvik/transdroid/preferences/PreferencesXirvikServer.java index e6392f16..0abd68a2 100644 --- a/android/src/com/xirvik/transdroid/preferences/PreferencesXirvikServer.java +++ b/android/src/com/xirvik/transdroid/preferences/PreferencesXirvikServer.java @@ -15,15 +15,26 @@ * along with Transdroid. If not, see . * */ - package com.xirvik.transdroid.preferences; +package com.xirvik.transdroid.preferences; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.DefaultHttpClient; import org.transdroid.R; +import org.transdroid.daemon.DaemonException; +import org.transdroid.daemon.util.HttpHelper; import org.transdroid.preferences.Preferences; import org.transdroid.preferences.TransdroidCheckBoxPreference; import org.transdroid.preferences.TransdroidEditTextPreference; import org.transdroid.preferences.TransdroidListPreference; import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.os.AsyncTask; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; @@ -39,7 +50,7 @@ import android.widget.Toast; public class PreferencesXirvikServer extends PreferenceActivity { public static final String PREFERENCES_XSERVER_KEY = "PREFERENCES_XSERVER_POSTFIX"; - /*public static final String[] validAddressStart = { "dedi", "semi" };*/ + /* public static final String[] validAddressStart = { "dedi", "semi" }; */ public static final String[] validAddressEnding = { ".xirvik.com", ".xirvik.net" }; private String serverPostfix; @@ -47,6 +58,7 @@ public class PreferencesXirvikServer extends PreferenceActivity { private TransdroidEditTextPreference name; private TransdroidListPreference type; private TransdroidEditTextPreference server; + private TransdroidEditTextPreference folder; private TransdroidEditTextPreference user; private TransdroidEditTextPreference pass; private TransdroidCheckBoxPreference alarmFinished; @@ -55,94 +67,104 @@ public class PreferencesXirvikServer extends PreferenceActivity { private String nameValue = null; private String typeValue = null; private String serverValue = null; + private String folderValue = null; private String userValue = null; - //private String passValue = null; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - // For which server? - serverPostfix = getIntent().getStringExtra(PREFERENCES_XSERVER_KEY); - // Create the preferences screen here: this takes care of saving/loading, but also contains the ListView adapter, etc. - setPreferenceScreen(getPreferenceManager().createPreferenceScreen(this)); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - - nameValue = prefs.getString(Preferences.KEY_PREF_XNAME + serverPostfix, null); - typeValue = prefs.getString(Preferences.KEY_PREF_XTYPE + serverPostfix, null); - serverValue = prefs.getString(Preferences.KEY_PREF_XSERVER + serverPostfix, null); - userValue = prefs.getString(Preferences.KEY_PREF_XUSER + serverPostfix, null); - //passValue = prefs.getString(Preferences.KEY_PREF_XPASS + serverPostfix, null); - - // Create preference objects - getPreferenceScreen().setTitle(R.string.xirvik_pref_title); - // Name - name = new TransdroidEditTextPreference(this); - name.setTitle(R.string.pref_name); - name.setKey(Preferences.KEY_PREF_XNAME + serverPostfix); - name.getEditText().setSingleLine(); - name.setDialogTitle(R.string.pref_name); - name.setOnPreferenceChangeListener(updateHandler); - getPreferenceScreen().addItemFromInflater(name); - // Type - type = new TransdroidListPreference(this); - type.setTitle(R.string.xirvik_pref_type); - type.setKey(Preferences.KEY_PREF_XTYPE + serverPostfix); - type.setEntries(R.array.pref_xirvik_types); - type.setEntryValues(R.array.pref_xirvik_values); - type.setDialogTitle(R.string.xirvik_pref_type); - type.setOnPreferenceChangeListener(updateHandler); - getPreferenceScreen().addItemFromInflater(type); - // Server - server = new TransdroidEditTextPreference(this); - server.setTitle(R.string.xirvik_pref_server); - server.setKey(Preferences.KEY_PREF_XSERVER + serverPostfix); - server.getEditText().setSingleLine(); - server.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI); - server.setDialogTitle(R.string.xirvik_pref_server); - server.setOnPreferenceChangeListener(updateHandler); - getPreferenceScreen().addItemFromInflater(server); - // User - user = new TransdroidEditTextPreference(this); - user.setTitle(R.string.pref_user); - user.setKey(Preferences.KEY_PREF_XUSER + serverPostfix); - user.getEditText().setSingleLine(); - user.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_FILTER); - user.setDialogTitle(R.string.pref_user); - user.setOnPreferenceChangeListener(updateHandler); - getPreferenceScreen().addItemFromInflater(user); - // Pass - pass = new TransdroidEditTextPreference(this); - pass.setTitle(R.string.pref_pass); - pass.setKey(Preferences.KEY_PREF_XPASS + serverPostfix); - pass.getEditText().setSingleLine(); - pass.getEditText().setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); - pass.getEditText().setTransformationMethod(new PasswordTransformationMethod()); - pass.setDialogTitle(R.string.pref_pass); - pass.setOnPreferenceChangeListener(updateHandler); - getPreferenceScreen().addItemFromInflater(pass); - - // AlertFinished - alarmFinished = new TransdroidCheckBoxPreference(this); - alarmFinished.setDefaultValue(true); - alarmFinished.setTitle(R.string.pref_alarmfinished); - alarmFinished.setSummary(R.string.pref_alarmfinished_info); - alarmFinished.setKey(Preferences.KEY_PREF_XALARMFINISHED + serverPostfix); - alarmFinished.setOnPreferenceChangeListener(updateHandler); - getPreferenceScreen().addItemFromInflater(alarmFinished); - // AlertNew - alarmNew = new TransdroidCheckBoxPreference(this); - alarmNew.setTitle(R.string.pref_alarmnew); - alarmNew.setSummary(R.string.pref_alarmnew_info); - alarmNew.setKey(Preferences.KEY_PREF_XALARMNEW + serverPostfix); - alarmNew.setOnPreferenceChangeListener(updateHandler); - getPreferenceScreen().addItemFromInflater(alarmNew); - - updateDescriptionTexts(); - - } - - private OnPreferenceChangeListener updateHandler = new OnPreferenceChangeListener() { + private String passValue = null; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // For which server? + serverPostfix = getIntent().getStringExtra(PREFERENCES_XSERVER_KEY); + // Create the preferences screen here: this takes care of saving/loading, but also contains the + // ListView adapter, etc. + setPreferenceScreen(getPreferenceManager().createPreferenceScreen(this)); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + + nameValue = prefs.getString(Preferences.KEY_PREF_XNAME + serverPostfix, null); + typeValue = prefs.getString(Preferences.KEY_PREF_XTYPE + serverPostfix, null); + serverValue = prefs.getString(Preferences.KEY_PREF_XSERVER + serverPostfix, null); + folderValue = prefs.getString(Preferences.KEY_PREF_XFOLDER + serverPostfix, null); + userValue = prefs.getString(Preferences.KEY_PREF_XUSER + serverPostfix, null); + passValue = prefs.getString(Preferences.KEY_PREF_XPASS + serverPostfix, null); + + // Create preference objects + getPreferenceScreen().setTitle(R.string.xirvik_pref_title); + // Name + name = new TransdroidEditTextPreference(this); + name.setTitle(R.string.pref_name); + name.setKey(Preferences.KEY_PREF_XNAME + serverPostfix); + name.getEditText().setSingleLine(); + name.setDialogTitle(R.string.pref_name); + name.setOnPreferenceChangeListener(updateHandler); + getPreferenceScreen().addItemFromInflater(name); + // Type + type = new TransdroidListPreference(this); + type.setTitle(R.string.xirvik_pref_type); + type.setKey(Preferences.KEY_PREF_XTYPE + serverPostfix); + type.setEntries(R.array.pref_xirvik_types); + type.setEntryValues(R.array.pref_xirvik_values); + type.setDialogTitle(R.string.xirvik_pref_type); + type.setOnPreferenceChangeListener(updateHandler); + getPreferenceScreen().addItemFromInflater(type); + // Server + server = new TransdroidEditTextPreference(this); + server.setTitle(R.string.xirvik_pref_server); + server.setKey(Preferences.KEY_PREF_XSERVER + serverPostfix); + server.getEditText().setSingleLine(); + server.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI); + server.setDialogTitle(R.string.xirvik_pref_server); + server.setOnPreferenceChangeListener(updateHandler); + getPreferenceScreen().addItemFromInflater(server); + // Folder + folder = new TransdroidEditTextPreference(this); + folder.setTitle(R.string.xirvik_pref_folder); + folder.setKey(Preferences.KEY_PREF_XFOLDER + serverPostfix); + folder.setEnabled(false); + folder.setSummary(R.string.xirvik_pref_setautomatically); + getPreferenceScreen().addItemFromInflater(folder); + // User + user = new TransdroidEditTextPreference(this); + user.setTitle(R.string.pref_user); + user.setKey(Preferences.KEY_PREF_XUSER + serverPostfix); + user.getEditText().setSingleLine(); + user.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_FILTER); + user.setDialogTitle(R.string.pref_user); + user.setOnPreferenceChangeListener(updateHandler); + getPreferenceScreen().addItemFromInflater(user); + // Pass + pass = new TransdroidEditTextPreference(this); + pass.setTitle(R.string.pref_pass); + pass.setKey(Preferences.KEY_PREF_XPASS + serverPostfix); + pass.getEditText().setSingleLine(); + pass.getEditText().setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD); + pass.getEditText().setTransformationMethod(new PasswordTransformationMethod()); + pass.setDialogTitle(R.string.pref_pass); + pass.setOnPreferenceChangeListener(updateHandler); + getPreferenceScreen().addItemFromInflater(pass); + + // AlertFinished + alarmFinished = new TransdroidCheckBoxPreference(this); + alarmFinished.setDefaultValue(true); + alarmFinished.setTitle(R.string.pref_alarmfinished); + alarmFinished.setSummary(R.string.pref_alarmfinished_info); + alarmFinished.setKey(Preferences.KEY_PREF_XALARMFINISHED + serverPostfix); + alarmFinished.setOnPreferenceChangeListener(updateHandler); + getPreferenceScreen().addItemFromInflater(alarmFinished); + // AlertNew + alarmNew = new TransdroidCheckBoxPreference(this); + alarmNew.setTitle(R.string.pref_alarmnew); + alarmNew.setSummary(R.string.pref_alarmnew_info); + alarmNew.setKey(Preferences.KEY_PREF_XALARMNEW + serverPostfix); + alarmNew.setOnPreferenceChangeListener(updateHandler); + getPreferenceScreen().addItemFromInflater(alarmNew); + + updateDescriptionTexts(); + + } + + private OnPreferenceChangeListener updateHandler = new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (preference == name) { @@ -158,48 +180,107 @@ public class PreferencesXirvikServer extends PreferenceActivity { validEnd |= newServer.endsWith(validAddressEnding[i]); } if (!valid || !validEnd) { - Toast.makeText(getApplicationContext(), R.string.xirvik_error_invalid_servername, Toast.LENGTH_LONG).show(); + Toast + .makeText(getApplicationContext(), R.string.xirvik_error_invalid_servername, Toast.LENGTH_LONG) + .show(); return false; } serverValue = newServer; } else if (preference == user) { userValue = (String) newValue; } else if (preference == pass) { - //passValue = (String) newValue; + passValue = (String) newValue; } + updateDescriptionTexts(); + updateScgiMountFolder(); // Set the value as usual return true; } - }; - - @Override - protected void onListItemClick(ListView l, View v, int position, long id) { - - // Perform click action, which always is a Preference - Preference item = (Preference) getListAdapter().getItem(position); - + }; + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + + // Perform click action, which always is a Preference + Preference item = (Preference) getListAdapter().getItem(position); + // Let the Preference open the right dialog if (item instanceof TransdroidListPreference) { - ((TransdroidListPreference)item).click(); + ((TransdroidListPreference) item).click(); } else if (item instanceof TransdroidCheckBoxPreference) { - ((TransdroidCheckBoxPreference)item).click(); + ((TransdroidCheckBoxPreference) item).click(); } else if (item instanceof TransdroidEditTextPreference) { - ((TransdroidEditTextPreference)item).click(); + if (((TransdroidEditTextPreference) item).isEnabled()) { + ((TransdroidEditTextPreference) item).click(); + } + } + + } + + private void updateScgiMountFolder() { + if (typeValue != null && XirvikServerType.fromCode(typeValue) == XirvikServerType.SharedRtorrent) { + new AsyncTask() { + @Override + protected String doInBackground(Void... params) { + try { + // Get, from the server, the RPC SCGI mount address + DefaultHttpClient httpclient = HttpHelper.createStandardHttpClient(true, userValue, passValue, + true, null, HttpHelper.DEFAULT_CONNECTION_TIMEOUT, serverValue, 443); + String url = "https://" + serverValue + ":443/browsers_addons/transdroid_autoconf.txt"; + HttpResponse request = httpclient.execute(new HttpGet(url)); + InputStream stream = request.getEntity().getContent(); + String folderVal = HttpHelper.ConvertStreamToString(stream).trim(); + if (folderVal.startsWith(" lookup = new HashMap(); @@ -48,6 +49,9 @@ public enum XirvikServerType { if (code.equals("type_shared")) { return Shared; } + if (code.equals("type_sharedrt")) { + return SharedRtorrent; + } return null; } } diff --git a/android/src/com/xirvik/transdroid/preferences/XirvikSettings.java b/android/src/com/xirvik/transdroid/preferences/XirvikSettings.java index 2aff675b..5792b7a5 100644 --- a/android/src/com/xirvik/transdroid/preferences/XirvikSettings.java +++ b/android/src/com/xirvik/transdroid/preferences/XirvikSettings.java @@ -16,25 +16,26 @@ public class XirvikSettings { private static final int TFB4RT_PORT = 443; private static final String TFB4RT_FOLDER = "/tfx"; private static final int RTORRENT_PORT = 443; - private static final String RTORRENT_FOLDER_DEDI = "/RPC2"; - private static final String RTORRENT_FOLDER_SEMI = "/RPC2"; + public static final String RTORRENT_FOLDER = "/RPC2"; private static final int UTORRENT_PORT = 5010; final private String name; final private XirvikServerType type; final private String server; + final private String folder; final private String username; final private String password; final private boolean alarmOnFinishedDownload; final private boolean alarmOnNewTorrent; final private String idString; - public XirvikSettings(String name, XirvikServerType type, String server, String username, + public XirvikSettings(String name, XirvikServerType type, String server, String folder, String username, String password, boolean alarmOnFinishedDownload, boolean alarmOnNewTorrent, String idString) { this.name = name; this.type = type; this.server = server; + this.folder = folder; this.username = username; this.password = password; this.alarmOnFinishedDownload = alarmOnFinishedDownload; @@ -51,6 +52,9 @@ public class XirvikSettings { public String getServer() { return server; } + public String getFolder() { + return folder; + } public String getUsername() { return username; } @@ -94,13 +98,13 @@ public class XirvikSettings { getPassword(), HttpHelper.DEFAULT_CONNECTION_TIMEOUT, shouldAlarmOnFinishedDownload(), shouldAlarmOnNewTorrent(), "" + startID++, true)); } - if (getType() == XirvikServerType.SemiDedicated || isDedi) { + if (getType() == XirvikServerType.SharedRtorrent || getType() == XirvikServerType.SemiDedicated || isDedi) { daemons.add( new DaemonSettings( getName() + (isDedi? " rTorrent": ""), Daemon.rTorrent, getServer(), RTORRENT_PORT, true, true, null, - (isDedi? RTORRENT_FOLDER_DEDI: getSemiFoldername()), true, getUsername(), getPassword(), + getFolder(), true, getUsername(), getPassword(), OS.Linux, "/", "ftp://" + getName() + ":" + getServer() + "/", getPassword(), HttpHelper.DEFAULT_CONNECTION_TIMEOUT, shouldAlarmOnFinishedDownload(), shouldAlarmOnNewTorrent(), "" + startID++, true)); @@ -119,29 +123,4 @@ public class XirvikSettings { return daemons; } - /** - * Returns the rTorrent folder name for a semi-dedicated server, based on the server, i.e. 'store001a.xirvik.com' - * @return The full folder name, i.e. '/RPC2' - */ - private String getSemiFoldername() { - /*int nr = 0; - if (getServer().length() > 1) { - switch (getServer().charAt(getServer().indexOf(".") - 1)) { - case 'a': - nr = 1; - break; - case 'b': - nr = 2; - break; - case 'c': - nr = 3; - break; - case 'd': - nr = 4; - break; - } - }*/ - return RTORRENT_FOLDER_SEMI; - } - } diff --git a/android/src/org/transdroid/preferences/Preferences.java b/android/src/org/transdroid/preferences/Preferences.java index d645a615..2f463c38 100644 --- a/android/src/org/transdroid/preferences/Preferences.java +++ b/android/src/org/transdroid/preferences/Preferences.java @@ -64,6 +64,7 @@ public class Preferences { public static final String KEY_PREF_XNAME = "transdroid_xserver_name"; public static final String KEY_PREF_XTYPE = "transdroid_xserver_type"; public static final String KEY_PREF_XSERVER = "transdroid_xserver_server"; + public static final String KEY_PREF_XFOLDER = "transdroid_xserver_folder"; public static final String KEY_PREF_XUSER = "transdroid_xserver_user"; public static final String KEY_PREF_XPASS = "transdroid_xserver_pass"; public static final String KEY_PREF_XALARMFINISHED = "transdroid_xserver_alarmfinished"; @@ -240,7 +241,8 @@ public class Preferences { String toId = (id == 0? "": Integer.toString(id)); editor.putString(KEY_PREF_XNAME + toId, prefs.getString(KEY_PREF_XNAME + fromId, null)); editor.putString(KEY_PREF_XTYPE + toId, prefs.getString(KEY_PREF_XTYPE + fromId, null)); - editor.putString(KEY_PREF_XSERVER+ toId, prefs.getString(KEY_PREF_XSERVER + fromId, null)); + editor.putString(KEY_PREF_XSERVER + toId, prefs.getString(KEY_PREF_XSERVER + fromId, null)); + editor.putString(KEY_PREF_XFOLDER + toId, prefs.getString(KEY_PREF_XFOLDER + fromId, null)); editor.putString(KEY_PREF_XUSER + toId, prefs.getString(KEY_PREF_XUSER + fromId, null)); editor.putString(KEY_PREF_XPASS + toId, prefs.getString(KEY_PREF_XPASS + fromId, null)); editor.putString(KEY_PREF_XALARMFINISHED + toId, prefs.getString(KEY_PREF_XALARMFINISHED + fromId, null)); @@ -817,6 +819,7 @@ public class Preferences { prefs.getString(KEY_PREF_XNAME + postfix, null), XirvikServerType.fromCode(prefType), prefs.getString(KEY_PREF_XSERVER + postfix, null), + prefs.getString(KEY_PREF_XFOLDER + postfix, XirvikSettings.RTORRENT_FOLDER), prefs.getString(KEY_PREF_XUSER + postfix, null), prefs.getString(KEY_PREF_XPASS + postfix, null), prefs.getBoolean(KEY_PREF_XALARMFINISHED + postfix, true), diff --git a/lib/src/org/transdroid/daemon/Rtorrent/RtorrentAdapter.java b/lib/src/org/transdroid/daemon/Rtorrent/RtorrentAdapter.java index 29698822..48a80b26 100644 --- a/lib/src/org/transdroid/daemon/Rtorrent/RtorrentAdapter.java +++ b/lib/src/org/transdroid/daemon/Rtorrent/RtorrentAdapter.java @@ -240,7 +240,7 @@ public class RtorrentAdapter implements IDaemonAdapter { */ private void initialise() throws DaemonException { - this.rpcclient = new XMLRPCClient(HttpHelper.createStandardHttpClient(settings, true), buildWebUIUrl()); + this.rpcclient = new XMLRPCClient(HttpHelper.createStandardHttpClient(settings, true), buildWebUIUrl().trim()); } diff --git a/lib/src/org/transdroid/daemon/util/HttpHelper.java b/lib/src/org/transdroid/daemon/util/HttpHelper.java index 36267e7e..6fa419ac 100644 --- a/lib/src/org/transdroid/daemon/util/HttpHelper.java +++ b/lib/src/org/transdroid/daemon/util/HttpHelper.java @@ -68,30 +68,42 @@ public class HttpHelper { * The 'User-Agent' name to send to the server */ public static String userAgent = null; - + /** * Creates a standard Apache HttpClient that is thread safe, supports different * SSL auth methods and basic authentication * @param settings The server settings to adhere - * @param connectionTimeout The connection timeout for all requests * @return An HttpClient that should be stored locally and reused for every new request * @throws DaemonException Thrown when information (such as username/password) is missing */ public static DefaultHttpClient createStandardHttpClient(DaemonSettings settings, boolean userBasicAuth) throws DaemonException { + return createStandardHttpClient(userBasicAuth && settings.shouldUseAuthentication(), settings.getUsername(), settings.getPassword(), settings.getSslTrustAll(), settings.getSslTrustKey(), settings.getTimeoutInMilliseconds(), settings.getAddress(), settings.getPort()); + } + + /** + * Creates a standard Apache HttpClient that is thread safe, supports different + * SSL auth methods and basic authentication + * @param sslTrustAll Whether to trust all SSL certificates + * @param sslTrustkey A specific SSL key to accept exclusively + * @param timeout The connection timeout for all requests + * @param authAddress The authentication domain address + * @param authPort The authentication domain port number + * @return An HttpClient that should be stored locally and reused for every new request + * @throws DaemonException Thrown when information (such as username/password) is missing + */ + public static DefaultHttpClient createStandardHttpClient(boolean userBasicAuth, String username, String password, boolean sslTrustAll, String sslTrustkey, int timeout, String authAddress, int authPort) throws DaemonException { // Register http and https sockets SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", new PlainSocketFactory(), 80)); - SocketFactory https_socket = - settings.getSslTrustAll() ? new FakeSocketFactory() - : settings.getSslTrustKey() != null ? new FakeSocketFactory(settings.getSslTrustKey()) - : SSLSocketFactory.getSocketFactory(); + SocketFactory https_socket = sslTrustAll ? new FakeSocketFactory() + : sslTrustkey != null ? new FakeSocketFactory(sslTrustkey) : SSLSocketFactory.getSocketFactory(); registry.register(new Scheme("https", https_socket, 443)); // Standard parameters HttpParams httpparams = new BasicHttpParams(); - HttpConnectionParams.setConnectionTimeout(httpparams, settings.getTimeoutInMilliseconds()); - HttpConnectionParams.setSoTimeout(httpparams, settings.getTimeoutInMilliseconds()); + HttpConnectionParams.setConnectionTimeout(httpparams, timeout); + HttpConnectionParams.setSoTimeout(httpparams, timeout); if (userAgent != null) { HttpProtocolParams.setUserAgent(httpparams, userAgent); } @@ -99,13 +111,13 @@ public class HttpHelper { DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpparams, registry), httpparams); // Authentication credentials - if (userBasicAuth && settings.shouldUseAuthentication()) { - if (settings.getUsername() == null || settings.getPassword() == null) { + if (userBasicAuth) { + if (username == null || password == null) { throw new InvalidParameterException("No username or password was provided while we hadauthentication enabled"); } httpclient.getCredentialsProvider().setCredentials( - new AuthScope(settings.getAddress(), settings.getPort(), AuthScope.ANY_REALM), - new UsernamePasswordCredentials(settings.getUsername(), settings.getPassword())); + new AuthScope(authAddress, authPort, AuthScope.ANY_REALM), + new UsernamePasswordCredentials(username, password)); } return httpclient;