mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-10 14:20:55 +00:00
# resource handling and loading for events
This commit is contained in:
1 parent
3de188f98b
commit
594acb1743
13 files changed
+175
-133
No files matched your search
+9
@@ -7,6 +7,15 @@ import com.sap.sailing.server.gateway.deserialization.JsonDeserializationExcepti
|
||||
|
||||
public class Helpers {
|
||||
|
||||
public static JSONArray toJSONArraySafe(Object object)
|
||||
throws JsonDeserializationException {
|
||||
if (object instanceof JSONArray) {
|
||||
return (JSONArray) object;
|
||||
}
|
||||
throw new JsonDeserializationException(
|
||||
String.format("Expected a JSONArray, got %s.", object.getClass().getName()));
|
||||
}
|
||||
|
||||
public static JSONObject toJSONObjectSafe(Object object)
|
||||
throws JsonDeserializationException {
|
||||
if (object instanceof JSONObject) {
|
||||
|
||||
+23
-3
@@ -1,13 +1,23 @@
|
||||
package com.sap.sailing.racecommittee.app.data;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.sap.sailing.domain.base.Event;
|
||||
import com.sap.sailing.racecommittee.app.data.clients.EventsLoadClient;
|
||||
import com.sap.sailing.racecommittee.app.data.handlers.DataHandler;
|
||||
import com.sap.sailing.racecommittee.app.data.handlers.EventsDataHandler;
|
||||
import com.sap.sailing.racecommittee.app.data.loaders.DataLoader;
|
||||
import com.sap.sailing.racecommittee.app.data.parsers.DataParser;
|
||||
import com.sap.sailing.racecommittee.app.data.parsers.EventsDataParser;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.CourseAreaJsonDeserializer;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.EventJsonDeserializer;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.VenueJsonDeserializer;
|
||||
|
||||
/**
|
||||
* Enables accessing of all data
|
||||
* Enables accessing of data.
|
||||
*/
|
||||
public class DataManager {
|
||||
|
||||
@@ -32,8 +42,18 @@ public class DataManager {
|
||||
}
|
||||
|
||||
protected void loadEvents(EventsLoadClient client) {
|
||||
// TODO Auto-generated method stub
|
||||
DataHandler<Event> handler = null;
|
||||
DataParser<Collection<Event>> parser = new EventsDataParser(
|
||||
new EventJsonDeserializer(
|
||||
new VenueJsonDeserializer(
|
||||
new CourseAreaJsonDeserializer())));
|
||||
DataHandler<Collection<Event>> handler = new EventsDataHandler(this, client);
|
||||
|
||||
DataLoader<Collection<Event>> loader = new DataLoader<Collection<Event>>(
|
||||
context,
|
||||
URI.create("http://10.0.2.2:8888/sailingserver/rc/events"),
|
||||
parser,
|
||||
handler);
|
||||
loader.startLoading();
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -4,6 +4,6 @@ import java.util.Collection;
|
||||
|
||||
import com.sap.sailing.domain.base.Event;
|
||||
|
||||
public interface EventsLoadClient extends LoadClient<Event> {
|
||||
public interface EventsLoadClient extends LoadClient<Collection<Event>> {
|
||||
public void onEventsLoaded(Collection<Event> events);
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.sap.sailing.racecommittee.app.data.handlers;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import com.sap.sailing.domain.base.Event;
|
||||
import com.sap.sailing.racecommittee.app.data.DataManager;
|
||||
import com.sap.sailing.racecommittee.app.data.clients.EventsLoadClient;
|
||||
|
||||
public class EventsDataHandler extends DataHandler<Collection<Event>> {
|
||||
|
||||
public EventsDataHandler(DataManager manager, EventsLoadClient client) {
|
||||
super(manager, client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoaded(Collection<Event> data) {
|
||||
super.onLoaded(data);
|
||||
// TODO: add to manager!
|
||||
((EventsLoadClient)client).onEventsLoaded(data);
|
||||
}
|
||||
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package com.sap.sailing.racecommittee.app.data.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
|
||||
public class DeleteHttpRequest extends HttpRequest {
|
||||
|
||||
public HttpResponse delete(String service) throws ClientProtocolException, IOException {
|
||||
HttpDelete delete = new HttpDelete(service);
|
||||
HttpResponse response = executeRequest(delete, "DELETE request");
|
||||
return response;
|
||||
}
|
||||
}
|
||||
+7
-16
@@ -1,29 +1,20 @@
|
||||
package com.sap.sailing.racecommittee.app.data.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.JsonParser;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.TypeReference;
|
||||
|
||||
public class GetHttpRequest<T> extends HttpRequest {
|
||||
public class GetHttpRequest extends HttpRequest {
|
||||
|
||||
public T get(String url, TypeReference<?> typeref)
|
||||
throws JsonGenerationException, JsonMappingException, IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
/*mapper = */mapper
|
||||
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true)
|
||||
.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
HttpRequestBase get = new HttpGet(url);
|
||||
HttpResponse response = executeRequest(get, "GET request");
|
||||
public InputStream get(URI requestUri) throws IOException {
|
||||
HttpRequestBase get = new HttpGet(requestUri);
|
||||
HttpResponse response = executeRequest(get);
|
||||
HttpEntity entity = response.getEntity();
|
||||
return mapper.readValue(entity.getContent(), typeref);
|
||||
return entity.getContent();
|
||||
}
|
||||
}
|
||||
+6
-7
@@ -5,7 +5,6 @@ import java.io.IOException;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
@@ -19,19 +18,19 @@ public abstract class HttpRequest {
|
||||
private final static int lowestOkCode = HttpStatus.SC_OK;
|
||||
private final static int lowestRedirectCode = HttpStatus.SC_MULTIPLE_CHOICES;
|
||||
|
||||
private static void validateHttpResponse(HttpResponse response) {
|
||||
private static void validateHttpResponse(HttpResponse response) throws IOException {
|
||||
StatusLine status = response.getStatusLine();
|
||||
if (status != null) {
|
||||
int statusCode = status.getStatusCode();
|
||||
if (statusCode >= lowestOkCode && statusCode < lowestRedirectCode) {
|
||||
return;
|
||||
}
|
||||
throw new IllegalStateException(String.format("Request response had error code %d.", statusCode));
|
||||
throw new IOException(String.format("Request response had error code %d.", statusCode));
|
||||
}
|
||||
throw new IllegalStateException(String.format("Request response had no valid status."));
|
||||
throw new IOException(String.format("Request response had no valid status."));
|
||||
}
|
||||
|
||||
protected final static String JsonUTF8 = "application/json;charset=UTF-8";
|
||||
//protected final static String JsonUTF8 = "application/json;charset=UTF-8";
|
||||
|
||||
protected HttpClient client;
|
||||
|
||||
@@ -39,11 +38,11 @@ public abstract class HttpRequest {
|
||||
client = new DefaultHttpClient();
|
||||
}
|
||||
|
||||
protected HttpResponse executeRequest(HttpRequestBase request, String content) throws IOException, ClientProtocolException {
|
||||
protected HttpResponse executeRequest(HttpRequestBase request) throws IOException {
|
||||
ExLog.i(TAG, "Requesting " + request.getMethod() + " " + request.getURI().toString());
|
||||
HttpResponse response = client.execute(request);
|
||||
validateHttpResponse(response);
|
||||
ExLog.i(TAG, "Response " + response.getStatusLine().toString() + " for " + content);
|
||||
ExLog.i(TAG, "Response " + response.getStatusLine().toString());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package com.sap.sailing.racecommittee.app.data.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.message.BasicHeader;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
/**
|
||||
* @author Basil Hess
|
||||
*
|
||||
* Used for POST Request to a REST Service
|
||||
*
|
||||
* 'requestObjectAsString' is POSTed
|
||||
*/
|
||||
public class PostHttpRequest extends HttpRequest {
|
||||
|
||||
public HttpResponse post(String requestObjectAsString, String service) throws ClientProtocolException, IOException {
|
||||
String jsonString = requestObjectAsString;
|
||||
StringEntity entity = new StringEntity(jsonString);
|
||||
entity.setContentType(JsonUTF8);
|
||||
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, JsonUTF8));
|
||||
|
||||
HttpPost post = new HttpPost(service);
|
||||
post.setEntity(entity);
|
||||
HttpResponse response = executeRequest(post, requestObjectAsString);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
+42
-7
@@ -1,29 +1,64 @@
|
||||
package com.sap.sailing.racecommittee.app.data.loaders;
|
||||
|
||||
import com.sap.sailing.racecommittee.app.data.handlers.DataHandler;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.net.URI;
|
||||
|
||||
import android.content.AsyncTaskLoader;
|
||||
import android.content.Context;
|
||||
|
||||
public class DataLoader<T> extends AsyncTaskLoader<T> {
|
||||
|
||||
private DataHandler<T> dataHandler;
|
||||
import com.sap.sailing.racecommittee.app.data.handlers.DataHandler;
|
||||
import com.sap.sailing.racecommittee.app.data.http.GetHttpRequest;
|
||||
import com.sap.sailing.racecommittee.app.data.parsers.DataParser;
|
||||
import com.sap.sailing.racecommittee.app.logging.ExLog;
|
||||
|
||||
public DataLoader(Context context, DataHandler<T> dataHandler) {
|
||||
public class DataLoader<T> extends AsyncTaskLoader<T> {
|
||||
private static final String TAG = DataLoader.class.getName();
|
||||
|
||||
protected URI requestUri;
|
||||
protected DataParser<T> dataParser;
|
||||
protected DataHandler<T> dataHandler;
|
||||
protected volatile Exception lastException;
|
||||
|
||||
public DataLoader(Context context, URI requestUri,
|
||||
DataParser<T> dataParser, DataHandler<T> dataHandler) {
|
||||
super(context);
|
||||
this.requestUri = requestUri;
|
||||
this.dataParser = dataParser;
|
||||
this.dataHandler = dataHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T loadInBackground() {
|
||||
// TODO Auto-generated method stub
|
||||
lastException = null;
|
||||
try {
|
||||
return loadDataInBackground();
|
||||
} catch (Exception e) {
|
||||
lastException = e;
|
||||
ExLog.e(TAG, String.format("Exception while loading data:\n%s", e.toString()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private T loadDataInBackground() throws Exception {
|
||||
GetHttpRequest getRequest = new GetHttpRequest();
|
||||
InputStream inputStream = null;
|
||||
Reader inputReader = null;
|
||||
try {
|
||||
inputStream = getRequest.get(requestUri);
|
||||
inputReader = new InputStreamReader(inputStream);
|
||||
return dataParser.parse(inputReader);
|
||||
} finally {
|
||||
inputStream.close();
|
||||
inputReader.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deliverResult(T data) {
|
||||
if (data == null) {
|
||||
dataHandler.onFailed(null);
|
||||
dataHandler.onFailed(lastException);
|
||||
} else {
|
||||
dataHandler.onLoaded(data);
|
||||
}
|
||||
|
||||
-51
@@ -1,51 +0,0 @@
|
||||
package com.sap.sailing.racecommittee.app.data.loaders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import android.content.AsyncTaskLoader;
|
||||
import android.content.Context;
|
||||
|
||||
import com.sap.sailing.domain.common.Named;
|
||||
import com.sap.sailing.racecommittee.app.data.handlers.DataHandler;
|
||||
import com.sap.sailing.racecommittee.app.logging.ExLog;
|
||||
|
||||
public class NamedListLoader<T extends Named> extends AsyncTaskLoader<List<T>> {
|
||||
|
||||
private static final String TAG = NamedListLoader.class.getName();
|
||||
|
||||
private String requestUrl;
|
||||
private DataHandler<T> dataHandler;
|
||||
|
||||
public NamedListLoader(Context context, String requestUrl, DataHandler<T> dataHandler) {
|
||||
super(context);
|
||||
this.requestUrl = requestUrl;
|
||||
this.dataHandler = dataHandler;
|
||||
}
|
||||
|
||||
protected volatile Exception lastException;
|
||||
|
||||
protected List<T> loadDataInBackground() throws Exception {
|
||||
return new GetHttpRequest<List<T>>().get(requestUrl, typeref);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> loadInBackground() {
|
||||
lastException = null;
|
||||
try {
|
||||
return loadDataInBackground();
|
||||
} catch (Exception e) {
|
||||
lastException = e;
|
||||
ExLog.e(TAG, e.toString());
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public void deliverResult (List<T> data){
|
||||
if (data == null) {
|
||||
dataHandler.onFailed(lastException);
|
||||
} else {
|
||||
dataHandler.onLoaded(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.sap.sailing.racecommittee.app.data.parsers;
|
||||
|
||||
public class DataParseException extends Exception {
|
||||
private static final long serialVersionUID = 3079074496188715438L;
|
||||
|
||||
public DataParseException() { }
|
||||
|
||||
public DataParseException(String detailMessage) {
|
||||
super(detailMessage);
|
||||
}
|
||||
|
||||
public DataParseException(Throwable throwable) {
|
||||
super(throwable);
|
||||
}
|
||||
|
||||
public DataParseException(String detailMessage, Throwable throwable) {
|
||||
super(detailMessage, throwable);
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.sap.sailing.racecommittee.app.data.parsers;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
public interface DataParser<T> {
|
||||
|
||||
T parse(Reader reader) throws Exception;
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.sap.sailing.racecommittee.app.data.parsers;
|
||||
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
||||
import com.sap.sailing.domain.base.Event;
|
||||
import com.sap.sailing.server.gateway.deserialization.JsonDeserializer;
|
||||
import com.sap.sailing.server.gateway.deserialization.impl.Helpers;
|
||||
|
||||
public class EventsDataParser implements DataParser<Collection<Event>> {
|
||||
|
||||
private JsonDeserializer<Event> deserializer;
|
||||
|
||||
public EventsDataParser(JsonDeserializer<Event> deserializer) {
|
||||
this.deserializer = deserializer;
|
||||
}
|
||||
|
||||
public Collection<Event> parse(Reader reader) throws Exception {
|
||||
Object parsedResult = JSONValue.parse(reader);
|
||||
JSONArray jsonArray = Helpers.toJSONArraySafe(parsedResult);
|
||||
Collection<Event> events = new ArrayList<Event>();
|
||||
|
||||
for (Object element : jsonArray) {
|
||||
JSONObject json = Helpers.toJSONObjectSafe(element);
|
||||
events.add(deserializer.deserialize(json));
|
||||
}
|
||||
|
||||
return events;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user