android/app/src/main/java/org/openlp/android2/common/OpenLPHttpClient.java

127 lines
5.3 KiB
Java

/******************************************************************************
* OpenLP - Open Source Lyrics Projection *
* --------------------------------------------------------------------------- *
* Copyright (c) 2011-2014 Raoul Snyman *
* Portions copyright (c) 2011-2014 Tim Bentley, Johan Mynhardt *
* --------------------------------------------------------------------------- *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the Free *
* Software Foundation; version 2 of the License. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *
* more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., 59 *
* Temple Place, Suite 330, Boston, MA 02111-1307 USA *
*******************************************************************************/
package org.openlp.android2.common;
import java.security.KeyStore;
import java.util.*;
import android.content.Context;
import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.MySSLSocketFactory;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.openlp.android2.R;
/**
* Personalised HttpClient to be used throughout OpenLP with customisable
* parameters.
*/
public class OpenLPHttpClient {
private final String LOG_TAG = OpenLPHttpClient.class.getName();
private Context context;
private Boolean useSSL = Boolean.FALSE;
public OpenLPHttpClient(Context context) {
this.context = context;
}
private String getPreference(Map preferences, String key, String defaultValue) {
if (preferences.containsKey(key)) {
return preferences.get(key).toString();
} else {
return defaultValue;
}
}
private Integer getPreference(Map preferences, String key, Integer defaultValue) {
if (preferences.containsKey(key)) {
return Integer.valueOf(preferences.get(key).toString());
} else {
return defaultValue;
}
}
private Boolean getPreference(Map preferences, String key, Boolean defaultValue) {
if (preferences.containsKey(key)) {
return Boolean.valueOf(preferences.get(key).toString());
} else {
return defaultValue;
}
}
public String getAbsoluteUrl(AsyncHttpClient client) {
String urlBase = getBaseUrl();
Map<String, ?> preferences = context.getSharedPreferences(context.getString(R.string.keySharedPreferences),
Context.MODE_PRIVATE).getAll();
String userid = getPreference(preferences, context.getString(R.string.key_userid), "openlp");
String password = getPreference(preferences, context.getString(R.string.key_password), "password");
Log.d(LOG_TAG, "Credentials set to " + userid + " : " + password);
client.setBasicAuth(userid,password);
Credentials creds = new UsernamePasswordCredentials(userid, password);
int connectionTimeout = context.getResources().getInteger(
R.integer.connectionTimeoutDefaultValue);
if (getPreference(preferences, context.getString(R.string.keyEnableCustomTimeout), false)) {
Log.d(LOG_TAG, "Overriding Connection and Socket timeouts");
connectionTimeout = getPreference(preferences,
context.getString(R.string.keyConnectionTimeout),
context.getResources().getInteger(
R.integer.connectionTimeoutDefaultValue)
);
}
client.setTimeout(connectionTimeout);
if (useSSL){
try {
KeyStore trustStore = KeyStore.getInstance((KeyStore.getDefaultType()));
trustStore.load(null, null);
OpenLPSSLSocketFactory sf = new OpenLPSSLSocketFactory(trustStore);
sf.setHostnameVerifier((OpenLPSSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
client.setSSLSocketFactory(sf);
}
catch (Exception e){
}
}
return urlBase;
}
public String getBaseUrl(){
Map<String, ?> preferences = context.getSharedPreferences(context.getString(R.string.keySharedPreferences), Context.MODE_PRIVATE).getAll();
useSSL = getPreference(preferences, context.getString(R.string.key_ssl_use), false);
String host = getPreference(preferences, context.getString(R.string.keyHost),
context.getString(R.string.hostDefaultValue));
String port = getPreference(preferences, context.getString(R.string.keyPort), "4316");
return String.format("http%s://%s:%s", useSSL ? "s" : "", host, port);
}
}