Skip to content

Commit

Permalink
Do not mark forms as failed to send before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed May 25, 2023
1 parent db626c3 commit 227adcd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public InstanceGoogleSheetsUploader(DriveApi driveApi, SheetsApi sheetsApi) {

@Override
public String uploadOneSubmission(Instance instance, String spreadsheetUrl) throws FormUploadException {
markSubmissionFailed(instance);

File instanceFile = new File(instance.getInstanceFilePath());
if (!instanceFile.exists()) {
throw new FormUploadException(FAIL + "instance XML file does not exist!");
Expand All @@ -106,6 +104,7 @@ public String uploadOneSubmission(Instance instance, String spreadsheetUrl) thro

Form form = forms.get(0);
if (form.getBASE64RSAPublicKey() != null) {
markSubmissionFailed(instance);
throw new FormUploadException(getLocalizedString(Collect.getInstance(), R.string.google_sheets_encrypted_message));
}

Expand All @@ -123,6 +122,7 @@ public String uploadOneSubmission(Instance instance, String spreadsheetUrl) thro
}
insertRows(instance, instanceElement, null, key, instanceFile, spreadsheet.getSheets().get(0).getProperties().getTitle());
} catch (GoogleJsonResponseException e) {
markSubmissionFailed(instance);
throw new FormUploadException(getErrorMessageFromGoogleJsonResponseException(e));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public InstanceServerUploader(OpenRosaHttpInterface httpInterface,
*/
@Override
public String uploadOneSubmission(Instance instance, String urlString) throws FormUploadException {
markSubmissionFailed(instance);

Uri submissionUri = Uri.parse(urlString);

long contentLength = 10000000L;
Expand All @@ -86,13 +84,15 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
submissionUri.toString());
} else {
if (submissionUri.getHost() == null) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + "Host name may not be null");
}

URI uri;
try {
uri = URI.create(submissionUri.toString());
} catch (IllegalArgumentException e) {
markSubmissionFailed(instance);
Timber.d(e.getMessage() != null ? e.getMessage() : e.toString());
throw new FormUploadException(getLocalizedString(Collect.getInstance(), R.string.url_error));
}
Expand All @@ -113,11 +113,13 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
}

} catch (Exception e) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL
+ (e.getMessage() != null ? e.getMessage() : e.toString()));
}

if (headResult.getStatusCode() == HttpsURLConnection.HTTP_UNAUTHORIZED) {
markSubmissionFailed(instance);
throw new FormUploadAuthRequestedException(getLocalizedString(Collect.getInstance(), R.string.server_auth_credentials, submissionUri.getHost()),
submissionUri);
} else if (headResult.getStatusCode() == HttpsURLConnection.HTTP_NO_CONTENT) {
Expand All @@ -138,17 +140,20 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
} else {
// Don't follow a redirection attempt to a different host.
// We can't tell if this is a spoof or not.
markSubmissionFailed(instance);
throw new FormUploadException(FAIL
+ "Unexpected redirection attempt to a different host: "
+ newURI.toString());
}
} catch (Exception e) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + urlString + " " + e.toString());
}
}
} else {
if (headResult.getStatusCode() >= HttpsURLConnection.HTTP_OK
&& headResult.getStatusCode() < HttpsURLConnection.HTTP_MULT_CHOICE) {
markSubmissionFailed(instance);
throw new FormUploadException("Failed to send to " + uri + ". Is this an OpenRosa " +
"submission endpoint? If you have a web proxy you may need to log in to " +
"your network.\n\nHEAD request result status code: " + headResult.getStatusCode());
Expand All @@ -170,6 +175,7 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
}

if (!instanceFile.exists() && !submissionFile.exists()) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + "instance XML file does not exist!");
}

Expand Down Expand Up @@ -210,10 +216,12 @@ public String uploadOneSubmission(Instance instance, String urlString) throws Fo
}

}
markSubmissionFailed(instance);
throw exception;
}

} catch (Exception e) {
markSubmissionFailed(instance);
throw new FormUploadException(FAIL + "Generic Exception: "
+ (e.getMessage() != null ? e.getMessage() : e.toString()));
}
Expand Down

0 comments on commit 227adcd

Please sign in to comment.