/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
*
* 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, either version 3 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, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package fr.gotorennes.remote;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.util.Log;
public abstract class RemoteService<T> {
private static final String TAG = "GoToRennes.RemoteService";
private static final String apiUrl = "http://data.explore.star.fr/api/records/1.0/search/?apikey=050bf390daf3dcd58962faa939c0572552b200bb4fab710fdb3c56bf&dataset=";
private static HttpClient httpclient = new DefaultHttpClient();
protected abstract T populate(JSONObject jsonObject) throws JSONException;
protected abstract String getDataSet();
protected T loadObject(String query) {
List<T> list = loadList(query);
return list.isEmpty() ? null : list.get(0);
}
protected List<T> loadList() {
return loadList(null);
}
protected List<T> loadList(String query) {
return loadList(query, null, 1000);
}
protected List<T> loadProximityList(double latitude, double longitude, int count) {
return loadList(null, String.valueOf(latitude) + "," + String.valueOf(longitude) + "," + 5000, count);
}
protected List<T> loadList(String query, String geofilter, int count) {
List<T> result = new ArrayList<T>();
JSONArray data = getData(query, geofilter, count);
if (data != null) {
try {
for (int i = 0; i < data.length(); i++) {
JSONObject jsonObject = data.getJSONObject(i);
onDataLoad(jsonObject);
result.add(populate(jsonObject.getJSONObject("fields")));
}
} catch (Exception e) {
Log.e(TAG, "Echec de lecture de la réponse du service distant : " + e.getMessage());
}
}
return result;
}
protected void onDataLoad(JSONObject data) throws JSONException {
// Nothing
}
protected JSONArray getData(String query, String geofilter, int count) {
String url = apiUrl + getDataSet();
if (query != null) {
url += "&" + query;
}
if (geofilter != null) {
url += "&geofilter.distance=" + encode(geofilter);
}
url += "&rows=" + count;
//Log.d("GoToRennes", url);
HttpEntity entity = getEntity(url);
if (entity == null) {
return null;
}
String json = readFully(entity);
if (json == null) {
return null;
}
try {
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
return object.optJSONArray("records");
}
catch (Exception e) {
Log.e(TAG, "Echec de lecture de la réponse du service distant : " + e.getMessage());
}
return null;
}
public static String encode(String query) {
try {
return URLEncoder.encode(query, "UTF-8");
}
catch(Exception ex) {
return query;
}
}
public static HttpEntity getEntity(String url) {
HttpGet httpget = new HttpGet(url);
try {
HttpResponse response = httpclient.execute(httpget);
if (response != null) {
return response.getEntity();
}
} catch (Exception e) {
Log.e(TAG, "Echec d'accès au service distant : " + e.getMessage());
}
return null;
}
public static String readFully(String url) {
HttpEntity entity = getEntity(url);
if (entity == null) {
return null;
}
return readFully(entity);
}
public static String readFully(HttpEntity entity) {
InputStream instream = null;
try {
instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder json = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
json.append(line);
}
return json.toString();
} catch (Exception e) {
Log.e(TAG, "Echec de lecture de la réponse du service distant : " + e.getMessage());
} finally {
if (instream != null) {
try {
instream.close();
} catch (Exception e) {
Log.e(TAG, "Echec de fermeture du flux du service distant : " + e.getMessage());
}
}
}
return null;
}
}