summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/ApiRequest.java
blob: a33f07da85b832e72fed2523977d9e127594b13d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package org.fox.ttrss;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonElement> {
	private final String TAG = this.getClass().getSimpleName();

	protected static final int STATUS_LOGIN_FAILED = 0;
	protected static final int STATUS_OK = 1;
	protected static final int STATUS_API_DISABLED = 2;
	protected static final int STATUS_OTHER_ERROR = 3;
	
	private String m_api;
	private boolean m_trustAny = false;
	private boolean m_transportDebugging = false;
	private Context m_context;

	public ApiRequest(Context context) {
		m_context = context;

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(m_context);
		
		m_api = prefs.getString("ttrss_url", null);
		m_trustAny = prefs.getBoolean("ssl_trust_any", false);
		m_transportDebugging = prefs.getBoolean("transport_debugging", false);
	}
	
	@Override
	protected JsonElement doInBackground(HashMap<String, String>... params) {

		Gson gson = new Gson();
		
		String requestStr = gson.toJson(new HashMap<String,String>(params[0]));
		
		if (m_transportDebugging) Log.d(TAG, ">>> (" + requestStr + ") " + m_api);
		
		DefaultHttpClient client;
		
		if (m_trustAny) {
			SchemeRegistry schemeRegistry = new SchemeRegistry();
			schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
			schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
        
			HttpParams httpParams = new BasicHttpParams();

			client = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);
		} else {
			client = new DefaultHttpClient();
		}

		HttpPost httpPost = new HttpPost(m_api + "/api/");
		
		try {
			httpPost.setEntity(new StringEntity(requestStr, "utf-8"));
			HttpResponse execute = client.execute(httpPost);
			
			InputStream content = execute.getEntity().getContent();

			BufferedReader buffer = new BufferedReader(
					new InputStreamReader(content));

			String s = "";				
			String response = "";

			while ((s = buffer.readLine()) != null) {
				response += s;
			}

			if (m_transportDebugging) Log.d(TAG, "<<< " + response);

			JsonParser parser = new JsonParser();
			
			return parser.parse(response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}


	/* protected String m_sessionId;
	protected String m_apiEndpoint;
	protected String m_login;
	protected String m_password;
	protected int m_authStatus;
	protected Gson m_gson = new Gson();

	protected static final int STATUS_LOGIN_FAILED = 0;
	protected static final int STATUS_OK = 1;
	protected static final int STATUS_API_DISABLED = 2;
	protected static final int STATUS_OTHER_ERROR = 3;
	
	protected ApiRequest(String sessionId, String apiEndpoint, String login, String password) {
		super();
		m_sessionId = sessionId;
		m_apiEndpoint = apiEndpoint;
		m_authStatus = STATUS_OK;
		m_login = login;
		m_password = password;
		
		//Log.d(TAG, "initial SID=" + sessionId);
	}
	
	protected int tryAuthenticate() {
		JsonElement result = _sendRequest(new HashMap<String,String>() {   
			{
				put("op", "login");
				put("user", m_login);
				put("password", m_password);
			}			 
		});
		
		if (result != null) {
			try {			
				JsonObject rv = result.getAsJsonObject();

				int status = rv.get("status").getAsInt();
				
				if (status == 0) {
					JsonObject content = rv.get("content").getAsJsonObject();
					if (content != null) {
						m_sessionId = content.get("session_id").getAsString();
						
						Log.d(TAG, "<<< Authentified, sessionId=" + m_sessionId);
						
						return STATUS_OK;
					}
				} else {
					JsonObject content = rv.get("content").getAsJsonObject();
					
					if (content != null) {
						String error = content.get("error").getAsString();

						if (error.equals("LOGIN_ERROR")) {
							m_sessionId = null;
							return STATUS_LOGIN_FAILED;
						} else if (error.equals("API_DISABLED")) {
							m_sessionId = null;
							return STATUS_API_DISABLED;
						}								
					}							
				}
			} catch (Exception e) {
				e.printStackTrace();						
			}
		}
		m_sessionId = null;
		return STATUS_OTHER_ERROR;
	}
	
	protected String getSessionId() {
		return m_sessionId;
	}
	
	protected int getAuthStatus() {
		return m_authStatus;
	}
	
	protected JsonElement _sendRequest(HashMap<String,String> param) {

		HashMap<String,String> tmp = new HashMap<String,String>(param);

		if (m_sessionId != null)
			tmp.put("sid", m_sessionId);

		String requestStr = m_gson.toJson(tmp);
		
		Log.d(TAG, ">>> (" + requestStr + ") " + m_apiEndpoint);
		
		DefaultHttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(m_apiEndpoint + "/api/");
		
		try {
			httpPost.setEntity(new StringEntity(requestStr, "utf-8"));
			HttpResponse execute = client.execute(httpPost);
			
			InputStream content = execute.getEntity().getContent();

			BufferedReader buffer = new BufferedReader(
					new InputStreamReader(content));

			String s = "";				
			String response = "";

			while ((s = buffer.readLine()) != null) {
				response += s;
			}

			Log.d(TAG, "<<< " + response);

			JsonParser parser = new JsonParser();
			
			return parser.parse(response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public JsonElement sendRequest(HashMap<String, String> params) {
		
		JsonElement result = _sendRequest(params);

		try {
			JsonElement content = result.getAsJsonObject().get("content");
			int status = result.getAsJsonObject().get("status").getAsInt();

			if (status == 1) {
				String error = content.getAsJsonObject().get("error").getAsString();

				if (error.equals("NOT_LOGGED_IN")) {
					Log.d(TAG, "<<< Session invalid, trying to authenticate...");
					
					m_sessionId = null;
					m_authStatus = tryAuthenticate();
					
					if (m_authStatus == STATUS_OK) {
						result = _sendRequest(params);
						
						return result.getAsJsonObject().get("content");						
					}
				}
			} else {
				return content;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	} */ 
}