Browse Source

Support for new style Xirvik shared servers.

pull/11/head
Eric Kok 13 years ago
parent
commit
23c7adb9f1
  1. 2
      android/AndroidManifest.xml
  2. 2
      android/project.properties
  3. 4
      android/res/values/arrays.xml
  4. 3
      android/res/values/strings.xml
  5. 91
      android/src/com/xirvik/transdroid/preferences/PreferencesXirvikServer.java
  6. 6
      android/src/com/xirvik/transdroid/preferences/XirvikServerType.java
  7. 39
      android/src/com/xirvik/transdroid/preferences/XirvikSettings.java
  8. 3
      android/src/org/transdroid/preferences/Preferences.java
  9. 2
      lib/src/org/transdroid/daemon/Rtorrent/RtorrentAdapter.java
  10. 34
      lib/src/org/transdroid/daemon/util/HttpHelper.java

2
android/AndroidManifest.xml

@ -22,7 +22,7 @@
android:versionCode="145" android:versionCode="145"
android:installLocation="auto"> android:installLocation="auto">
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="13" /> <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />
<supports-screens smallScreens="true" normalScreens="true" largeScreens="true" xlargeScreens="true" android:anyDensity="true" /> <supports-screens smallScreens="true" normalScreens="true" largeScreens="true" xlargeScreens="true" android:anyDensity="true" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2
android/project.properties

@ -10,6 +10,6 @@
# Indicates whether an apk should be generated for each density. # Indicates whether an apk should be generated for each density.
split.density=false split.density=false
# Project target. # Project target.
target=android-13 target=android-14
apk-configurations= apk-configurations=
android.library.reference.1=../external/JakeWharton-ActionBarSherlock/library android.library.reference.1=../external/JakeWharton-ActionBarSherlock/library

4
android/res/values/arrays.xml

@ -43,12 +43,14 @@
</string-array> </string-array>
<string-array name="pref_xirvik_types"> <string-array name="pref_xirvik_types">
<item>Shared</item> <item>Shared (Torrentflux)</item>
<item>Shared (rTorrent)</item>
<item>Semi-dedicated</item> <item>Semi-dedicated</item>
<item>Dedicated</item> <item>Dedicated</item>
</string-array> </string-array>
<string-array name="pref_xirvik_values"> <string-array name="pref_xirvik_values">
<item>type_shared</item> <item>type_shared</item>
<item>type_sharedrt</item>
<item>type_semi</item> <item>type_semi</item>
<item>type_dedicated</item> <item>type_dedicated</item>
</string-array> </string-array>

3
android/res/values/strings.xml

@ -363,7 +363,10 @@
<string name="xirvik_pref_type_info">Shared, semi- or dedicated</string> <string name="xirvik_pref_type_info">Shared, semi- or dedicated</string>
<string name="xirvik_pref_server">Server name</string> <string name="xirvik_pref_server">Server name</string>
<string name="xirvik_pref_server_info">Like dedi000.xirvik.com</string> <string name="xirvik_pref_server_info">Like dedi000.xirvik.com</string>
<string name="xirvik_pref_folder">SCGI mount</string>
<string name="xirvik_pref_setautomatically">Set automatically</string>
<string name="xirvik_error_invalid_servername">Invalid server (use the full host name, like dedi000.xirvik.com)</string> <string name="xirvik_error_invalid_servername">Invalid server (use the full host name, like dedi000.xirvik.com)</string>
<string name="xirvik_error_nofolder">Cannot retrieve the Xirvik SCGI folder setting; please try again later ro correct your server name setting</string>
<string name="seedm8_info">SeedM8 offers unmetered GBit seedbox hosting. Transdroid provides easy setup for SeedM8 servers.\n\nRead more at www.seedm8.com</string> <string name="seedm8_info">SeedM8 offers unmetered GBit seedbox hosting. Transdroid provides easy setup for SeedM8 servers.\n\nRead more at www.seedm8.com</string>
<string name="seedm8_add_new_xserver">Add SeedM8 server</string> <string name="seedm8_add_new_xserver">Add SeedM8 server</string>

91
android/src/com/xirvik/transdroid/preferences/PreferencesXirvikServer.java

@ -17,13 +17,24 @@
*/ */
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.R;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.util.HttpHelper;
import org.transdroid.preferences.Preferences; import org.transdroid.preferences.Preferences;
import org.transdroid.preferences.TransdroidCheckBoxPreference; import org.transdroid.preferences.TransdroidCheckBoxPreference;
import org.transdroid.preferences.TransdroidEditTextPreference; import org.transdroid.preferences.TransdroidEditTextPreference;
import org.transdroid.preferences.TransdroidListPreference; import org.transdroid.preferences.TransdroidListPreference;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
@ -47,6 +58,7 @@ public class PreferencesXirvikServer extends PreferenceActivity {
private TransdroidEditTextPreference name; private TransdroidEditTextPreference name;
private TransdroidListPreference type; private TransdroidListPreference type;
private TransdroidEditTextPreference server; private TransdroidEditTextPreference server;
private TransdroidEditTextPreference folder;
private TransdroidEditTextPreference user; private TransdroidEditTextPreference user;
private TransdroidEditTextPreference pass; private TransdroidEditTextPreference pass;
private TransdroidCheckBoxPreference alarmFinished; private TransdroidCheckBoxPreference alarmFinished;
@ -55,8 +67,9 @@ public class PreferencesXirvikServer extends PreferenceActivity {
private String nameValue = null; private String nameValue = null;
private String typeValue = null; private String typeValue = null;
private String serverValue = null; private String serverValue = null;
private String folderValue = null;
private String userValue = null; private String userValue = null;
//private String passValue = null; private String passValue = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -64,15 +77,17 @@ public class PreferencesXirvikServer extends PreferenceActivity {
// For which server? // For which server?
serverPostfix = getIntent().getStringExtra(PREFERENCES_XSERVER_KEY); serverPostfix = getIntent().getStringExtra(PREFERENCES_XSERVER_KEY);
// Create the preferences screen here: this takes care of saving/loading, but also contains the ListView adapter, etc. // Create the preferences screen here: this takes care of saving/loading, but also contains the
// ListView adapter, etc.
setPreferenceScreen(getPreferenceManager().createPreferenceScreen(this)); setPreferenceScreen(getPreferenceManager().createPreferenceScreen(this));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
nameValue = prefs.getString(Preferences.KEY_PREF_XNAME + serverPostfix, null); nameValue = prefs.getString(Preferences.KEY_PREF_XNAME + serverPostfix, null);
typeValue = prefs.getString(Preferences.KEY_PREF_XTYPE + serverPostfix, null); typeValue = prefs.getString(Preferences.KEY_PREF_XTYPE + serverPostfix, null);
serverValue = prefs.getString(Preferences.KEY_PREF_XSERVER + 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); userValue = prefs.getString(Preferences.KEY_PREF_XUSER + serverPostfix, null);
//passValue = prefs.getString(Preferences.KEY_PREF_XPASS + serverPostfix, null); passValue = prefs.getString(Preferences.KEY_PREF_XPASS + serverPostfix, null);
// Create preference objects // Create preference objects
getPreferenceScreen().setTitle(R.string.xirvik_pref_title); getPreferenceScreen().setTitle(R.string.xirvik_pref_title);
@ -102,6 +117,13 @@ public class PreferencesXirvikServer extends PreferenceActivity {
server.setDialogTitle(R.string.xirvik_pref_server); server.setDialogTitle(R.string.xirvik_pref_server);
server.setOnPreferenceChangeListener(updateHandler); server.setOnPreferenceChangeListener(updateHandler);
getPreferenceScreen().addItemFromInflater(server); 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
user = new TransdroidEditTextPreference(this); user = new TransdroidEditTextPreference(this);
user.setTitle(R.string.pref_user); user.setTitle(R.string.pref_user);
@ -158,16 +180,20 @@ public class PreferencesXirvikServer extends PreferenceActivity {
validEnd |= newServer.endsWith(validAddressEnding[i]); validEnd |= newServer.endsWith(validAddressEnding[i]);
} }
if (!valid || !validEnd) { 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; return false;
} }
serverValue = newServer; serverValue = newServer;
} else if (preference == user) { } else if (preference == user) {
userValue = (String) newValue; userValue = (String) newValue;
} else if (preference == pass) { } else if (preference == pass) {
//passValue = (String) newValue; passValue = (String) newValue;
} }
updateDescriptionTexts(); updateDescriptionTexts();
updateScgiMountFolder();
// Set the value as usual // Set the value as usual
return true; return true;
} }
@ -185,9 +211,63 @@ public class PreferencesXirvikServer extends PreferenceActivity {
} else if (item instanceof TransdroidCheckBoxPreference) { } else if (item instanceof TransdroidCheckBoxPreference) {
((TransdroidCheckBoxPreference) item).click(); ((TransdroidCheckBoxPreference) item).click();
} else if (item instanceof TransdroidEditTextPreference) { } else if (item instanceof TransdroidEditTextPreference) {
if (((TransdroidEditTextPreference) item).isEnabled()) {
((TransdroidEditTextPreference) item).click(); ((TransdroidEditTextPreference) item).click();
} }
}
}
private void updateScgiMountFolder() {
if (typeValue != null && XirvikServerType.fromCode(typeValue) == XirvikServerType.SharedRtorrent) {
new AsyncTask<Void, Void, String>() {
@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("<?xml")) {
folderVal = null;
}
stream.close();
return folderVal;
} catch (DaemonException e) {
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
@Override
protected void onPostExecute(String result) {
storeScgiMountFolder(result);
}
}.execute();
} else {
// No need to retrieve this value
storeScgiMountFolder(XirvikSettings.RTORRENT_FOLDER);
}
}
protected void storeScgiMountFolder(String result) {
if (result == null) {
// The RPC SCGI mount folder address couldn't be retrieved, so we cannot continue: show an error
Toast.makeText(getApplicationContext(), R.string.xirvik_error_nofolder, Toast.LENGTH_LONG).show();
folder.setSummary(R.string.xirvik_error_nofolder);
result = "";
}
// Store the new folder setting
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor edit = prefs.edit();
edit.putString(Preferences.KEY_PREF_XFOLDER + serverPostfix, result);
edit.commit();
folderValue = result;
updateDescriptionTexts();
} }
private void updateDescriptionTexts() { private void updateDescriptionTexts() {
@ -199,6 +279,7 @@ public class PreferencesXirvikServer extends PreferenceActivity {
type.setSummary(typeType == null ? getText(R.string.xirvik_pref_type_info) : typeType.toString()); type.setSummary(typeType == null ? getText(R.string.xirvik_pref_type_info) : typeType.toString());
server.setSummary(serverValue == null ? getText(R.string.xirvik_pref_server_info) : serverValue); server.setSummary(serverValue == null ? getText(R.string.xirvik_pref_server_info) : serverValue);
user.setSummary(userValue == null ? "" : userValue); user.setSummary(userValue == null ? "" : userValue);
folder.setSummary(folderValue == null ? "" : folderValue);
} }

6
android/src/com/xirvik/transdroid/preferences/XirvikServerType.java

@ -8,7 +8,8 @@ public enum XirvikServerType {
Dedicated (1), Dedicated (1),
SemiDedicated (2), SemiDedicated (2),
Shared (3); Shared (3),
SharedRtorrent (4);
private int code; private int code;
private static final Map<Integer,XirvikServerType> lookup = new HashMap<Integer,XirvikServerType>(); private static final Map<Integer,XirvikServerType> lookup = new HashMap<Integer,XirvikServerType>();
@ -48,6 +49,9 @@ public enum XirvikServerType {
if (code.equals("type_shared")) { if (code.equals("type_shared")) {
return Shared; return Shared;
} }
if (code.equals("type_sharedrt")) {
return SharedRtorrent;
}
return null; return null;
} }
} }

39
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 int TFB4RT_PORT = 443;
private static final String TFB4RT_FOLDER = "/tfx"; private static final String TFB4RT_FOLDER = "/tfx";
private static final int RTORRENT_PORT = 443; private static final int RTORRENT_PORT = 443;
private static final String RTORRENT_FOLDER_DEDI = "/RPC2"; public static final String RTORRENT_FOLDER = "/RPC2";
private static final String RTORRENT_FOLDER_SEMI = "/RPC2";
private static final int UTORRENT_PORT = 5010; private static final int UTORRENT_PORT = 5010;
final private String name; final private String name;
final private XirvikServerType type; final private XirvikServerType type;
final private String server; final private String server;
final private String folder;
final private String username; final private String username;
final private String password; final private String password;
final private boolean alarmOnFinishedDownload; final private boolean alarmOnFinishedDownload;
final private boolean alarmOnNewTorrent; final private boolean alarmOnNewTorrent;
final private String idString; 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 password, boolean alarmOnFinishedDownload, boolean alarmOnNewTorrent,
String idString) { String idString) {
this.name = name; this.name = name;
this.type = type; this.type = type;
this.server = server; this.server = server;
this.folder = folder;
this.username = username; this.username = username;
this.password = password; this.password = password;
this.alarmOnFinishedDownload = alarmOnFinishedDownload; this.alarmOnFinishedDownload = alarmOnFinishedDownload;
@ -51,6 +52,9 @@ public class XirvikSettings {
public String getServer() { public String getServer() {
return server; return server;
} }
public String getFolder() {
return folder;
}
public String getUsername() { public String getUsername() {
return username; return username;
} }
@ -94,13 +98,13 @@ public class XirvikSettings {
getPassword(), HttpHelper.DEFAULT_CONNECTION_TIMEOUT, shouldAlarmOnFinishedDownload(), getPassword(), HttpHelper.DEFAULT_CONNECTION_TIMEOUT, shouldAlarmOnFinishedDownload(),
shouldAlarmOnNewTorrent(), "" + startID++, true)); shouldAlarmOnNewTorrent(), "" + startID++, true));
} }
if (getType() == XirvikServerType.SemiDedicated || isDedi) { if (getType() == XirvikServerType.SharedRtorrent || getType() == XirvikServerType.SemiDedicated || isDedi) {
daemons.add( daemons.add(
new DaemonSettings( new DaemonSettings(
getName() + (isDedi? " rTorrent": ""), getName() + (isDedi? " rTorrent": ""),
Daemon.rTorrent, getServer(), RTORRENT_PORT, Daemon.rTorrent, getServer(), RTORRENT_PORT,
true, true, null, true, true, null,
(isDedi? RTORRENT_FOLDER_DEDI: getSemiFoldername()), true, getUsername(), getPassword(), getFolder(), true, getUsername(), getPassword(),
OS.Linux, "/", "ftp://" + getName() + ":" + getServer() + "/", OS.Linux, "/", "ftp://" + getName() + ":" + getServer() + "/",
getPassword(), HttpHelper.DEFAULT_CONNECTION_TIMEOUT, shouldAlarmOnFinishedDownload(), getPassword(), HttpHelper.DEFAULT_CONNECTION_TIMEOUT, shouldAlarmOnFinishedDownload(),
shouldAlarmOnNewTorrent(), "" + startID++, true)); shouldAlarmOnNewTorrent(), "" + startID++, true));
@ -119,29 +123,4 @@ public class XirvikSettings {
return daemons; 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;
}
} }

3
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_XNAME = "transdroid_xserver_name";
public static final String KEY_PREF_XTYPE = "transdroid_xserver_type"; 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_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_XUSER = "transdroid_xserver_user";
public static final String KEY_PREF_XPASS = "transdroid_xserver_pass"; public static final String KEY_PREF_XPASS = "transdroid_xserver_pass";
public static final String KEY_PREF_XALARMFINISHED = "transdroid_xserver_alarmfinished"; public static final String KEY_PREF_XALARMFINISHED = "transdroid_xserver_alarmfinished";
@ -241,6 +242,7 @@ public class Preferences {
editor.putString(KEY_PREF_XNAME + toId, prefs.getString(KEY_PREF_XNAME + fromId, null)); 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_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_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_XPASS + toId, prefs.getString(KEY_PREF_XPASS + fromId, null));
editor.putString(KEY_PREF_XALARMFINISHED + toId, prefs.getString(KEY_PREF_XALARMFINISHED + 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), prefs.getString(KEY_PREF_XNAME + postfix, null),
XirvikServerType.fromCode(prefType), XirvikServerType.fromCode(prefType),
prefs.getString(KEY_PREF_XSERVER + postfix, null), 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_XUSER + postfix, null),
prefs.getString(KEY_PREF_XPASS + postfix, null), prefs.getString(KEY_PREF_XPASS + postfix, null),
prefs.getBoolean(KEY_PREF_XALARMFINISHED + postfix, true), prefs.getBoolean(KEY_PREF_XALARMFINISHED + postfix, true),

2
lib/src/org/transdroid/daemon/Rtorrent/RtorrentAdapter.java

@ -240,7 +240,7 @@ public class RtorrentAdapter implements IDaemonAdapter {
*/ */
private void initialise() throws DaemonException { private void initialise() throws DaemonException {
this.rpcclient = new XMLRPCClient(HttpHelper.createStandardHttpClient(settings, true), buildWebUIUrl()); this.rpcclient = new XMLRPCClient(HttpHelper.createStandardHttpClient(settings, true), buildWebUIUrl().trim());
} }

34
lib/src/org/transdroid/daemon/util/HttpHelper.java

@ -73,25 +73,37 @@ public class HttpHelper {
* Creates a standard Apache HttpClient that is thread safe, supports different * Creates a standard Apache HttpClient that is thread safe, supports different
* SSL auth methods and basic authentication * SSL auth methods and basic authentication
* @param settings The server settings to adhere * @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 * @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 * @throws DaemonException Thrown when information (such as username/password) is missing
*/ */
public static DefaultHttpClient createStandardHttpClient(DaemonSettings settings, boolean userBasicAuth) throws DaemonException { 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 // Register http and https sockets
SchemeRegistry registry = new SchemeRegistry(); SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", new PlainSocketFactory(), 80)); registry.register(new Scheme("http", new PlainSocketFactory(), 80));
SocketFactory https_socket = SocketFactory https_socket = sslTrustAll ? new FakeSocketFactory()
settings.getSslTrustAll() ? new FakeSocketFactory() : sslTrustkey != null ? new FakeSocketFactory(sslTrustkey) : SSLSocketFactory.getSocketFactory();
: settings.getSslTrustKey() != null ? new FakeSocketFactory(settings.getSslTrustKey())
: SSLSocketFactory.getSocketFactory();
registry.register(new Scheme("https", https_socket, 443)); registry.register(new Scheme("https", https_socket, 443));
// Standard parameters // Standard parameters
HttpParams httpparams = new BasicHttpParams(); HttpParams httpparams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpparams, settings.getTimeoutInMilliseconds()); HttpConnectionParams.setConnectionTimeout(httpparams, timeout);
HttpConnectionParams.setSoTimeout(httpparams, settings.getTimeoutInMilliseconds()); HttpConnectionParams.setSoTimeout(httpparams, timeout);
if (userAgent != null) { if (userAgent != null) {
HttpProtocolParams.setUserAgent(httpparams, userAgent); HttpProtocolParams.setUserAgent(httpparams, userAgent);
} }
@ -99,13 +111,13 @@ public class HttpHelper {
DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpparams, registry), httpparams); DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpparams, registry), httpparams);
// Authentication credentials // Authentication credentials
if (userBasicAuth && settings.shouldUseAuthentication()) { if (userBasicAuth) {
if (settings.getUsername() == null || settings.getPassword() == null) { if (username == null || password == null) {
throw new InvalidParameterException("No username or password was provided while we hadauthentication enabled"); throw new InvalidParameterException("No username or password was provided while we hadauthentication enabled");
} }
httpclient.getCredentialsProvider().setCredentials( httpclient.getCredentialsProvider().setCredentials(
new AuthScope(settings.getAddress(), settings.getPort(), AuthScope.ANY_REALM), new AuthScope(authAddress, authPort, AuthScope.ANY_REALM),
new UsernamePasswordCredentials(settings.getUsername(), settings.getPassword())); new UsernamePasswordCredentials(username, password));
} }
return httpclient; return httpclient;

Loading…
Cancel
Save