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; public static final int PROP_FLAG_BOOLEAN = 1 << 3;
/** Is this field a serialized JSON object? */ /** Is this field a serialized JSON object? */
public static final int PROP_FLAG_JSON = 1 << 4; 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; public int flags = 0;

@ -63,7 +63,7 @@ public final class TagData extends RemoteModel {
/** Project picture */ /** Project picture */
public static final StringProperty PICTURE = new StringProperty( 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) */ /** Tag team array (JSON) */
@Deprecated public static final StringProperty MEMBERS = new StringProperty( @Deprecated public static final StringProperty MEMBERS = new StringProperty(

@ -54,7 +54,7 @@ public class UserActivity extends RemoteModel {
/** Picture */ /** Picture */
public static final StringProperty PICTURE = new StringProperty( 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 */ /** Target id */
public static final StringProperty TARGET_ID = new StringProperty( public static final StringProperty TARGET_ID = new StringProperty(

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

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

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

@ -4,6 +4,8 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -80,9 +82,9 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
} }
@Override @Override
protected boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException { protected boolean serializeExtrasToJSON(JSONObject serializeTo, MultipartEntity entity) throws JSONException {
// Process changes list and serialize to JSON // Process changes list and serialize to JSON
JSONArray changesJson = changesToJSON(); JSONArray changesJson = changesToJSON(entity);
if (changesJson == null || changesJson.length() == 0) if (changesJson == null || changesJson.length() == 0)
return false; return false;
serializeTo.put(CHANGES_KEY, changesJson); serializeTo.put(CHANGES_KEY, changesJson);
@ -98,7 +100,7 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes; return changes;
} }
private JSONArray changesToJSON() { private JSONArray changesToJSON(MultipartEntity entity) {
if (!RemoteModel.NO_UUID.equals(uuid)) if (!RemoteModel.NO_UUID.equals(uuid))
populateChanges(); populateChanges();
@ -146,8 +148,21 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
if (value == null) if (value == null)
changeJson.put("value", JSONObject.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("value", value);
}
} }
changeJson.put("column", serverColumn); changeJson.put("column", serverColumn);

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

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

Loading…
Cancel
Save