in aws-android-sdk-appsync/src/main/java/com/amazonaws/mobileconnectors/appsync/AppSyncCustomNetworkInvoker.java [104:278]
public void executeRequest(final PersistentOfflineMutationObject persistentOfflineMutationObject) {
dispatcher.execute(new Runnable() {
@Override
public void run() {
//Upload S3 Object if present in the mutation. The S3 Object is uploaded first before the mutation is executed.
if (!persistentOfflineMutationObject.bucket.equals("")) {
//Fail if S3 Object Manager has not been provided
if (s3ObjectManager == null) {
//Invoke callback
if (persistentMutationsCallback != null ) {
persistentMutationsCallback.onFailure(new PersistentMutationsError(persistentOfflineMutationObject.responseClassName,
persistentOfflineMutationObject.recordIdentifier, new ApolloNetworkException("S3 upload failed.", new IllegalArgumentException("S3ObjectManager not provided."))));
}
//Remove mutation from queue and mark state as completed.
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
//Trigger next mutation
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
return;
}
try {
s3ObjectManager.upload(new S3InputObjectInterface() {
@Override
public String localUri() {
return persistentOfflineMutationObject.localURI;
}
@Override
public String mimeType() {
return persistentOfflineMutationObject.mimeType;
}
@Override
public String bucket() {
return persistentOfflineMutationObject.bucket;
}
@Override
public String key() {
return persistentOfflineMutationObject.key;
}
@Override
public String region() {
return persistentOfflineMutationObject.region;
}
});
}
catch (AmazonClientException e) {
if ( e.getCause() instanceof IOException ) {
//IO Exception occured indicating that there was a network issue.
//Set mutationInProgress status to false and return without removing the mutation from the queue.
queueHandler.setMutationInProgressStatusToFalse();
return;
}
//Not a network error.
if (persistentMutationsCallback != null ) {
persistentMutationsCallback.onFailure(new PersistentMutationsError(persistentOfflineMutationObject.responseClassName,
persistentOfflineMutationObject.recordIdentifier, new ApolloNetworkException("S3 upload failed.", e)));
}
//Remove mutation from queue and mark state as completed.
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
//Trigger next mutation
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
return;
}
catch (Exception e) {
//Invoke callback
if (persistentMutationsCallback != null ) {
persistentMutationsCallback.onFailure(new PersistentMutationsError(persistentOfflineMutationObject.responseClassName,
persistentOfflineMutationObject.recordIdentifier, new ApolloNetworkException("S3 upload failed.", e)));
}
//Remove mutation from queue and mark state as completed.
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
//Trigger next mutation
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
return;
}
}
httpCall = httpCall(persistentOfflineMutationObject);
//Execute the mutation
httpCall.enqueue(new Callback() {
@Override
public void onFailure(@Nonnull Call call, @Nonnull IOException e) {
Log.e(TAG, "Thread:[" + Thread.currentThread().getId() + "]: Failed to execute http call for [" +
persistentOfflineMutationObject.recordIdentifier +"]. Exception is [" + e + "]");
//If this request has been canceled.
if (disposed) {
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
return;
}
//An I/O Exception indicates that the request didn't hit the server, likely due to a network issue.
//Mark this current execution as complete and wait for the next network event to resume queue processing
queueHandler.setMutationInProgressStatusToFalse();
return;
}
@Override
public void onResponse(@Nonnull Call call, @Nonnull Response response) throws IOException {
if (disposed) {
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
return;
}
if (response.isSuccessful()) {
String responseString = response.body().string();
try {
JSONObject jsonObject = new JSONObject(responseString);
//If conflict was present, route it through for conflict handling
if ( ConflictResolutionHandler.conflictPresent(responseString) ) {
JSONArray errors = jsonObject.optJSONArray("errors");
MutationInterceptorMessage interceptorMessage = new MutationInterceptorMessage();
interceptorMessage.requestIdentifier = persistentOfflineMutationObject.recordIdentifier;
interceptorMessage.clientState = persistentOfflineMutationObject.clientState;
interceptorMessage.requestClassName = persistentOfflineMutationObject.responseClassName;
interceptorMessage.serverState = new JSONObject(errors.getJSONObject(0).getString("data")).toString();
Message message = new Message();
message.obj = interceptorMessage;
message.what = MessageNumberUtil.RETRY_EXEC;
queueHandler.sendMessage(message);
return;
}
if (persistentMutationsCallback != null) {
persistentMutationsCallback.onResponse(new PersistentMutationsResponse(
jsonObject.optJSONObject("data"),
jsonObject.optJSONArray("errors"),
persistentOfflineMutationObject.responseClassName,
persistentOfflineMutationObject.recordIdentifier));
}
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
queueHandler.sendEmptyMessage(MessageNumberUtil.SUCCESSFUL_EXEC);
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, "Thread:[" + Thread.currentThread().getId() + "]: JSON Parse error" + e.toString());
if (persistentMutationsCallback != null) {
persistentMutationsCallback.onFailure(
new PersistentMutationsError(persistentOfflineMutationObject.responseClassName,
persistentOfflineMutationObject.recordIdentifier,
new ApolloParseException("Failed to parse http response", e)));
}
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
}
} else {
//Invoke onFailure on the callback.
if (persistentMutationsCallback != null) {
persistentMutationsCallback.onFailure(
new PersistentMutationsError(persistentOfflineMutationObject.responseClassName,
persistentOfflineMutationObject.recordIdentifier,
new ApolloNetworkException("Failed to execute http call with error code and message: " + response.code() + response.message())));
}
setMutationExecutionAsCompletedAndRemoveFromQueue(persistentOfflineMutationObject);
queueHandler.sendEmptyMessage(MessageNumberUtil.FAIL_EXEC);
}
}
});
}
}
);
}