Rough draft of how uploading files with multipart entity might look

pull/14/head
Sam Bosley 11 years ago
parent 6e0e86d6b7
commit b1da107c0d

@ -49,6 +49,8 @@ public abstract class Property<TYPE> extends Field implements Cloneable {
public static final int PROP_FLAG_BOOLEAN = 1 << 3;
/** Is this field a serialized JSON object? */
public static final int PROP_FLAG_JSON = 1 << 4;
/** Is this field for pictures? (usually as a json object containing "path" key or urls) */
public static final int PROP_FLAG_PICTURE = 1 << 5;
public int flags = 0;

@ -63,7 +63,7 @@ public final class TagData extends RemoteModel {
/** Project picture */
public static final StringProperty PICTURE = new StringProperty(
TABLE, "picture", Property.PROP_FLAG_JSON);
TABLE, "picture", Property.PROP_FLAG_JSON | Property.PROP_FLAG_PICTURE);
/** Tag team array (JSON) */
@Deprecated public static final StringProperty MEMBERS = new StringProperty(

@ -54,7 +54,7 @@ public class UserActivity extends RemoteModel {
/** Picture */
public static final StringProperty PICTURE = new StringProperty(
TABLE, "picture", Property.PROP_FLAG_JSON);
TABLE, "picture", Property.PROP_FLAG_JSON | Property.PROP_FLAG_PICTURE);
/** Target id */
public static final StringProperty TARGET_ID = new StringProperty(

@ -12,14 +12,12 @@ import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TimeZone;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -185,7 +183,7 @@ public class ActFmInvoker {
}
}
public JSONObject postSync(JSONArray data, String token) throws IOException,
public JSONObject postSync(JSONArray data, MultipartEntity entity, String token) throws IOException,
ActFmServiceException {
try {
String dataString = data.toString();
@ -194,11 +192,9 @@ public class ActFmInvoker {
String request = createFetchUrl("api/" + API_VERSION, "synchronize", "token", token, "data", dataString, "time", timeString);
if (SYNC_DEBUG)
Log.e("act-fm-post", request);
List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
pairs.add(new BasicNameValuePair("token", token));
pairs.add(new BasicNameValuePair("data", data.toString()));
pairs.add(new BasicNameValuePair("time", timeString));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, HTTP.UTF_8);
entity.addPart("token", new StringBody(token));
entity.addPart("data", new StringBody(data.toString()));
entity.addPart("time", new StringBody(timeString));
String response = restClient.post(request, entity);
JSONObject object = new JSONObject(response);

@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.entity.mime.MultipartEntity;
import org.json.JSONArray;
import org.json.JSONObject;
@ -295,8 +296,9 @@ public class ActFmSyncThread {
if (!messageBatch.isEmpty() && checkForToken()) {
JSONArray payload = new JSONArray();
MultipartEntity entity = new MultipartEntity();
for (ClientToServerMessage<?> message : messageBatch) {
JSONObject serialized = message.serializeToJSON();
JSONObject serialized = message.serializeToJSON(entity);
if (serialized != null) {
payload.put(serialized);
syncLog("Sending: " + serialized);
@ -309,7 +311,7 @@ public class ActFmSyncThread {
continue;
try {
JSONObject response = actFmInvoker.postSync(payload, token);
JSONObject response = actFmInvoker.postSync(payload, entity, token);
// process responses
JSONArray serverMessagesJson = response.optJSONArray("messages");
if (serverMessagesJson != null) {

@ -1,5 +1,6 @@
package com.todoroo.astrid.actfm.sync.messages;
import org.apache.http.entity.mime.MultipartEntity;
import org.json.JSONException;
import org.json.JSONObject;
@ -34,7 +35,7 @@ public class BriefMe<TYPE extends RemoteModel> extends ClientToServerMessage<TYP
}
@Override
protected boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
protected boolean serializeExtrasToJSON(JSONObject serializeTo, MultipartEntity entity) throws JSONException {
if (extraParameters != null && extraParameters.length > 0) {
for (int i = 0; i < extraParameters.length - 1; i++) {
try {

@ -4,6 +4,8 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -80,9 +82,9 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
}
@Override
protected boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
protected boolean serializeExtrasToJSON(JSONObject serializeTo, MultipartEntity entity) throws JSONException {
// Process changes list and serialize to JSON
JSONArray changesJson = changesToJSON();
JSONArray changesJson = changesToJSON(entity);
if (changesJson == null || changesJson.length() == 0)
return false;
serializeTo.put(CHANGES_KEY, changesJson);
@ -98,7 +100,7 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes;
}
private JSONArray changesToJSON() {
private JSONArray changesToJSON(MultipartEntity entity) {
if (!RemoteModel.NO_UUID.equals(uuid))
populateChanges();
@ -146,8 +148,21 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
if (value == null)
changeJson.put("value", JSONObject.NULL);
else
else {
if (localProperty.checkFlag(Property.PROP_FLAG_PICTURE) && value instanceof JSONObject) {
JSONObject json = (JSONObject) value;
if (json.has("path")) {
String path = json.optString("path");
String name = json.optString("name");
File f = new File(path);
if (f.exists() && !TextUtils.isEmpty(name)) {
json.remove("path");
entity.addPart(name, new FileBody(f));
}
}
}
changeJson.put("value", value);
}
}
changeJson.put("column", serverColumn);

@ -1,5 +1,6 @@
package com.todoroo.astrid.actfm.sync.messages;
import org.apache.http.entity.mime.MultipartEntity;
import org.json.JSONException;
import org.json.JSONObject;
@ -61,7 +62,7 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
return pushedAt;
}
public final JSONObject serializeToJSON() {
public final JSONObject serializeToJSON(MultipartEntity entity) {
JSONObject json = new JSONObject();
try {
json.put(TYPE_KEY, getTypeString());
@ -69,7 +70,7 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
json.put(UUID_KEY, uuid);
String dateValue = DateUtilities.timeToIso8601(pushedAt, true);
json.put(PUSHED_AT_KEY, dateValue != null ? dateValue : 0);
if (serializeExtrasToJSON(json))
if (serializeExtrasToJSON(json, entity))
return json;
else
return null;
@ -109,6 +110,6 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
return true;
}
protected abstract boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException;
protected abstract boolean serializeExtrasToJSON(JSONObject serializeTo, MultipartEntity entity) throws JSONException;
protected abstract String getTypeString();
}

@ -1,5 +1,6 @@
package com.todoroo.astrid.actfm.sync.messages;
import org.apache.http.entity.mime.MultipartEntity;
import org.json.JSONException;
import org.json.JSONObject;
@ -13,7 +14,7 @@ public class RequestDoubleCheck<TYPE extends RemoteModel> extends ClientToServer
}
@Override
protected boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
protected boolean serializeExtrasToJSON(JSONObject serializeTo, MultipartEntity entity) throws JSONException {
// No extras
return true;
}

Loading…
Cancel
Save