2011-09-08 10:23:44 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
2011-11-29 04:03:38 +00:00
|
|
|
import java.io.IOException;
|
2011-09-08 10:23:44 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2011-11-27 13:57:05 +00:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2011-09-08 10:23:44 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2011-11-27 13:57:05 +00:00
|
|
|
import org.apache.http.HttpHost;
|
2011-09-08 10:23:44 +00:00
|
|
|
import org.apache.http.HttpResponse;
|
2011-11-27 13:57:05 +00:00
|
|
|
import org.apache.http.auth.AuthScope;
|
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
2012-05-02 04:35:53 +00:00
|
|
|
import org.apache.http.client.CredentialsProvider;
|
2011-09-08 10:23:44 +00:00
|
|
|
import org.apache.http.client.methods.HttpPost;
|
2012-05-02 04:35:53 +00:00
|
|
|
import org.apache.http.client.protocol.ClientContext;
|
2011-11-27 07:04:28 +00:00
|
|
|
import org.apache.http.conn.scheme.Scheme;
|
2011-09-08 10:23:44 +00:00
|
|
|
import org.apache.http.entity.StringEntity;
|
2012-05-02 04:35:53 +00:00
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
|
import org.apache.http.protocol.BasicHttpContext;
|
|
|
|
import org.apache.http.protocol.HttpContext;
|
2011-09-08 10:23:44 +00:00
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2012-05-02 04:35:53 +00:00
|
|
|
import android.net.http.AndroidHttpClient;
|
2011-11-22 12:49:21 +00:00
|
|
|
import android.os.AsyncTask;
|
2011-11-27 10:18:04 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2011-09-08 10:23:44 +00:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonElement;
|
2011-11-29 04:03:38 +00:00
|
|
|
import com.google.gson.JsonObject;
|
2011-09-08 10:23:44 +00:00
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
2011-11-22 12:49:21 +00:00
|
|
|
public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonElement> {
|
2011-09-08 10:23:44 +00:00
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
public enum ApiError { NO_ERROR, HTTP_UNAUTHORIZED, HTTP_FORBIDDEN, HTTP_NOT_FOUND,
|
2011-12-06 11:29:57 +00:00
|
|
|
HTTP_SERVER_ERROR, HTTP_OTHER_ERROR, SSL_REJECTED, PARSE_ERROR, IO_ERROR, OTHER_ERROR, API_DISABLED, API_UNKNOWN, LOGIN_FAILED, INVALID_URL, INCORRECT_USAGE };
|
2011-11-22 12:49:21 +00:00
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
public static final int API_STATUS_OK = 0;
|
|
|
|
public static final int API_STATUS_ERR = 1;
|
|
|
|
|
2011-11-22 12:49:21 +00:00
|
|
|
private String m_api;
|
2011-11-27 07:04:28 +00:00
|
|
|
private boolean m_trustAny = false;
|
2011-11-27 10:18:04 +00:00
|
|
|
private boolean m_transportDebugging = false;
|
2011-11-29 04:03:38 +00:00
|
|
|
protected int m_httpStatusCode = 0;
|
|
|
|
protected int m_apiStatusCode = 0;
|
2011-11-29 05:25:13 +00:00
|
|
|
protected Context m_context;
|
2011-11-27 13:57:05 +00:00
|
|
|
private SharedPreferences m_prefs;
|
2011-11-29 04:03:38 +00:00
|
|
|
|
|
|
|
protected ApiError m_lastError;
|
2011-11-22 12:49:21 +00:00
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
public ApiRequest(Context context) {
|
|
|
|
m_context = context;
|
2011-11-22 12:49:21 +00:00
|
|
|
|
2011-11-27 13:57:05 +00:00
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(m_context);
|
2011-11-27 10:18:04 +00:00
|
|
|
|
2011-12-01 06:58:19 +00:00
|
|
|
m_api = m_prefs.getString("ttrss_url", null).trim();
|
2011-11-27 13:57:05 +00:00
|
|
|
m_trustAny = m_prefs.getBoolean("ssl_trust_any", false);
|
|
|
|
m_transportDebugging = m_prefs.getBoolean("transport_debugging", false);
|
2011-11-29 04:03:38 +00:00
|
|
|
m_lastError = ApiError.NO_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int getErrorMessage() {
|
|
|
|
switch (m_lastError) {
|
|
|
|
case NO_ERROR:
|
|
|
|
return R.string.error_unknown;
|
|
|
|
case HTTP_UNAUTHORIZED:
|
|
|
|
return R.string.error_http_unauthorized;
|
|
|
|
case HTTP_FORBIDDEN:
|
|
|
|
return R.string.error_http_forbidden;
|
|
|
|
case HTTP_NOT_FOUND:
|
|
|
|
return R.string.error_http_not_found;
|
|
|
|
case HTTP_SERVER_ERROR:
|
|
|
|
return R.string.error_http_server_error;
|
|
|
|
case HTTP_OTHER_ERROR:
|
|
|
|
return R.string.error_http_other_error;
|
|
|
|
case SSL_REJECTED:
|
|
|
|
return R.string.error_ssl_rejected;
|
|
|
|
case PARSE_ERROR:
|
|
|
|
return R.string.error_parse_error;
|
|
|
|
case IO_ERROR:
|
|
|
|
return R.string.error_io_error;
|
|
|
|
case OTHER_ERROR:
|
|
|
|
return R.string.error_other_error;
|
|
|
|
case API_DISABLED:
|
|
|
|
return R.string.error_api_disabled;
|
|
|
|
case API_UNKNOWN:
|
|
|
|
return R.string.error_api_unknown;
|
|
|
|
case LOGIN_FAILED:
|
|
|
|
return R.string.error_login_failed;
|
2011-12-01 06:58:19 +00:00
|
|
|
case INVALID_URL:
|
|
|
|
return R.string.error_invalid_api_url;
|
2011-12-06 11:29:57 +00:00
|
|
|
case INCORRECT_USAGE:
|
|
|
|
return R.string.error_api_incorrect_usage;
|
2011-11-29 04:03:38 +00:00
|
|
|
default:
|
|
|
|
Log.d(TAG, "getErrorMessage: unknown error code=" + m_lastError);
|
|
|
|
return R.string.error_unknown;
|
|
|
|
}
|
2011-11-27 07:04:28 +00:00
|
|
|
}
|
|
|
|
|
2011-11-22 12:49:21 +00:00
|
|
|
@Override
|
|
|
|
protected JsonElement doInBackground(HashMap<String, String>... params) {
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
String requestStr = gson.toJson(new HashMap<String,String>(params[0]));
|
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
if (m_transportDebugging) Log.d(TAG, ">>> (" + requestStr + ") " + m_api);
|
2011-11-27 07:04:28 +00:00
|
|
|
|
2012-04-30 11:24:25 +00:00
|
|
|
AndroidHttpClient client = AndroidHttpClient.newInstance("Tiny Tiny RSS");
|
2011-11-22 12:49:21 +00:00
|
|
|
|
2011-11-27 07:04:28 +00:00
|
|
|
if (m_trustAny) {
|
2012-04-30 11:24:25 +00:00
|
|
|
client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", new EasySSLSocketFactory(), 443));
|
2011-11-27 07:04:28 +00:00
|
|
|
}
|
|
|
|
|
2011-12-01 06:58:19 +00:00
|
|
|
try {
|
2011-11-27 13:57:05 +00:00
|
|
|
|
2011-12-01 06:58:19 +00:00
|
|
|
HttpPost httpPost;
|
|
|
|
|
2011-11-27 13:57:05 +00:00
|
|
|
try {
|
2011-12-01 06:58:19 +00:00
|
|
|
httpPost = new HttpPost(m_api + "/api/");
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
m_lastError = ApiError.INVALID_URL;
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
} catch (Exception e) {
|
|
|
|
m_lastError = ApiError.OTHER_ERROR;
|
2011-11-27 13:57:05 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
2011-11-22 12:49:21 +00:00
|
|
|
}
|
2011-12-01 06:58:19 +00:00
|
|
|
|
2012-04-30 11:24:25 +00:00
|
|
|
HttpContext context = null;
|
|
|
|
|
2011-12-01 06:58:19 +00:00
|
|
|
String httpLogin = m_prefs.getString("http_login", "").trim();
|
|
|
|
String httpPassword = m_prefs.getString("http_password", "").trim();
|
2011-11-22 12:49:21 +00:00
|
|
|
|
2011-12-01 06:58:19 +00:00
|
|
|
if (httpLogin.length() > 0) {
|
|
|
|
if (m_transportDebugging) Log.d(TAG, "Using HTTP Basic authentication.");
|
|
|
|
|
|
|
|
URL targetUrl;
|
|
|
|
try {
|
|
|
|
targetUrl = new URL(m_api);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
m_lastError = ApiError.INVALID_URL;
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpHost targetHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());
|
2012-04-30 11:24:25 +00:00
|
|
|
CredentialsProvider cp = new BasicCredentialsProvider();
|
|
|
|
context = new BasicHttpContext();
|
2011-12-01 06:58:19 +00:00
|
|
|
|
2012-04-30 11:24:25 +00:00
|
|
|
cp.setCredentials(
|
2011-12-01 06:58:19 +00:00
|
|
|
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
|
|
|
|
new UsernamePasswordCredentials(httpLogin, httpPassword));
|
2012-04-30 11:24:25 +00:00
|
|
|
|
|
|
|
context.setAttribute(ClientContext.CREDS_PROVIDER, cp);
|
2011-12-01 06:58:19 +00:00
|
|
|
}
|
2011-11-22 12:49:21 +00:00
|
|
|
|
2011-09-08 10:23:44 +00:00
|
|
|
httpPost.setEntity(new StringEntity(requestStr, "utf-8"));
|
2012-04-30 11:24:25 +00:00
|
|
|
HttpResponse execute = client.execute(httpPost, context);
|
2011-09-08 10:23:44 +00:00
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
m_httpStatusCode = execute.getStatusLine().getStatusCode();
|
2011-09-08 10:23:44 +00:00
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
switch (m_httpStatusCode) {
|
|
|
|
case 200:
|
|
|
|
InputStream content = execute.getEntity().getContent();
|
|
|
|
|
|
|
|
BufferedReader buffer = new BufferedReader(
|
|
|
|
new InputStreamReader(content), 8192);
|
|
|
|
|
|
|
|
String s = "";
|
|
|
|
String response = "";
|
|
|
|
|
|
|
|
while ((s = buffer.readLine()) != null) {
|
|
|
|
response += s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_transportDebugging) Log.d(TAG, "<<< " + response);
|
|
|
|
|
|
|
|
JsonParser parser = new JsonParser();
|
|
|
|
|
|
|
|
JsonElement result = parser.parse(response);
|
|
|
|
JsonObject resultObj = result.getAsJsonObject();
|
|
|
|
|
|
|
|
m_apiStatusCode = resultObj.get("status").getAsInt();
|
|
|
|
|
2012-05-02 04:35:53 +00:00
|
|
|
client.close();
|
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
switch (m_apiStatusCode) {
|
|
|
|
case API_STATUS_OK:
|
|
|
|
return result.getAsJsonObject().get("content");
|
|
|
|
case API_STATUS_ERR:
|
|
|
|
JsonObject contentObj = resultObj.get("content").getAsJsonObject();
|
|
|
|
String error = contentObj.get("error").getAsString();
|
|
|
|
|
|
|
|
if (error.equals("LOGIN_ERROR")) {
|
|
|
|
m_lastError = ApiError.LOGIN_FAILED;
|
|
|
|
} else if (error.equals("API_DISABLED")) {
|
2011-12-12 10:56:38 +00:00
|
|
|
m_lastError = ApiError.API_DISABLED;
|
2011-12-02 15:41:11 +00:00
|
|
|
} else if (error.equals("NOT_LOGGED_IN")) {
|
|
|
|
m_lastError = ApiError.LOGIN_FAILED;
|
2011-12-06 11:29:57 +00:00
|
|
|
} else if (error.equals("INCORRECT_USAGE")) {
|
|
|
|
m_lastError = ApiError.INCORRECT_USAGE;
|
2011-11-29 04:03:38 +00:00
|
|
|
} else {
|
2011-12-01 08:47:21 +00:00
|
|
|
Log.d(TAG, "Unknown API error: " + error);
|
2011-11-29 04:03:38 +00:00
|
|
|
m_lastError = ApiError.API_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 04:35:53 +00:00
|
|
|
|
|
|
|
client.close();
|
2011-11-29 04:03:38 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
case 401:
|
|
|
|
m_lastError = ApiError.HTTP_UNAUTHORIZED;
|
|
|
|
break;
|
|
|
|
case 403:
|
|
|
|
m_lastError = ApiError.HTTP_FORBIDDEN;
|
|
|
|
break;
|
|
|
|
case 404:
|
|
|
|
m_lastError = ApiError.HTTP_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
case 500:
|
|
|
|
m_lastError = ApiError.HTTP_SERVER_ERROR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
m_lastError = ApiError.HTTP_OTHER_ERROR;
|
|
|
|
break;
|
2011-09-08 10:23:44 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
return null;
|
|
|
|
} catch (javax.net.ssl.SSLPeerUnverifiedException e) {
|
|
|
|
m_lastError = ApiError.SSL_REJECTED;
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
m_lastError = ApiError.IO_ERROR;
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (com.google.gson.JsonSyntaxException e) {
|
|
|
|
m_lastError = ApiError.PARSE_ERROR;
|
|
|
|
e.printStackTrace();
|
2011-09-08 10:23:44 +00:00
|
|
|
} catch (Exception e) {
|
2011-11-29 04:03:38 +00:00
|
|
|
m_lastError = ApiError.OTHER_ERROR;
|
2011-09-08 10:23:44 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|