Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge/upgrade json extn #65

Open
wants to merge 3 commits into
base: merge/upgrade-json-extn
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import org.odk.collect.android.gdrive.InstanceGoogleSheetsUploader
import org.odk.collect.android.upload.FormUploadException
import org.odk.collect.android.upload.InstanceServerUploader
import org.odk.collect.android.upload.InstanceUploader
import org.odk.collect.android.utilities.*
import org.odk.collect.android.utilities.FormsRepositoryProvider
import org.odk.collect.android.utilities.InstanceAutoDeleteChecker
import org.odk.collect.android.utilities.InstanceUploaderUtils
import org.odk.collect.android.utilities.InstancesRepositoryProvider
import org.odk.collect.android.utilities.WebCredentialsUtils
import org.odk.collect.forms.FormsRepository
import org.odk.collect.forms.instances.Instance
import org.odk.collect.metadata.PropertyManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.odk.collect.android.openrosa.okhttp;

import android.webkit.URLUtil;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.odk.collect.android.events.FormEventBus;
import org.odk.collect.android.openrosa.CaseInsensitiveEmptyHeaders;
import org.odk.collect.android.openrosa.CaseInsensitiveHeaders;
import org.odk.collect.android.openrosa.HttpCredentialsInterface;
Expand Down Expand Up @@ -206,36 +210,56 @@ private HttpPostResult executePostRequest(@NonNull URI uri, @Nullable HttpCreden

Response response;
HttpPostResult postResult;
HttpPostResult failedResult = new HttpPostResult(
"Form Upload Failed",
HttpURLConnection.HTTP_BAD_REQUEST,
"Problem with server configuration");

if(generalSettings.getString(ProjectKeys.KEY_CUSTOM_SERVER_IS_ENABLED).equals("true")){

JSONObject headersInfo = new JSONObject(generalSettings.getString(ProjectKeys.KEY_CUSTOM_SERVER_HEADERS));
OkHttpClient client = new OkHttpClient();
if(generalSettings.getBoolean(ProjectKeys.KEY_CUSTOM_SERVER_IS_ENABLED)){
String customHeaders = generalSettings.getString(ProjectKeys.KEY_CUSTOM_SERVER_HEADERS);
Headers.Builder builder = new Headers.Builder();

Iterator<String> keys = headersInfo.keys();

// Iterate through the keys
while (keys.hasNext()) {
String key = keys.next();
String value = headersInfo.getString(key);
builder.add(key, value);
if (customHeaders != null && !customHeaders.trim().isEmpty()) {
try {
JSONObject headersInfo = new JSONObject(customHeaders);
Iterator<String> keys = headersInfo.keys();
// Iterate through the keys
while (keys.hasNext()) {
String key = keys.next();
String value = headersInfo.getString(key);
builder.add(key, value);
}
}
catch (JSONException e) {
FormEventBus.INSTANCE.formUploadError(
// TODO: return formId
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible lets pass, instance id in error instead of form id, as multiple instances of same form can be there

"",
String.format("JSON parsing exception: %s.\nPlease check the value for `KEY_CUSTOM_SERVER_HEADERS` in settings.", e)
);
e.printStackTrace();
return failedResult;
}
}
Headers headers = builder.build();
String customServerUrl = generalSettings.getString(ProjectKeys.KEY_CUSTOM_SERVER_URL);
if (customServerUrl == null || customServerUrl.isEmpty() || !URLUtil.isValidUrl(customServerUrl)) {
FormEventBus.INSTANCE.formUploadError(
// TODO: return formId
"",
"Custom Server URL is invalid! Please check the value for `KEY_CUSTOM_SERVER_URL` in settings."
);
return failedResult;
}

Headers h = builder.build();

Request request = new Request.Builder()
.url(generalSettings.getString(ProjectKeys.KEY_CUSTOM_SERVER_URL))
.url(customServerUrl)
.post(multipartBody)
.headers(h)
.headers(headers)
.build();


OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
response = call.execute();

}else{

}
else {
OpenRosaServerClient httpClient = clientFactory.get(uri.getScheme(), userAgent, credentials);

Request request = new Request.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"type": "string"
},
"custom_server_enabled": {
"type": "string"
"type": "boolean"
},
"username": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ import org.odk.collect.android.utilities.ApplicationConstants

class ODKActivityHandler : ODKActivityInteractor {

override fun openDraftFormList(formToFilter: List<String>?, ctx: Context) {
override fun openDraftFormsList(formsToFilter: List<String>?, ctx: Context) {
val i = Intent(ctx, InstanceChooserList::class.java)
i.putExtra(
ApplicationConstants.BundleKeys.FORM_MODE,
ApplicationConstants.FormModes.EDIT_SAVED
)
if (formToFilter != null) {
i.putExtra(ApplicationConstants.BundleKeys.FORM_ID, formToFilter.toTypedArray())
if (formsToFilter != null) {
i.putExtra(ApplicationConstants.BundleKeys.FORM_ID, formsToFilter.toTypedArray())
}
ctx.startActivity(i)
}

override fun openUnSubmittedFormList(ctx: Context) {
override fun openFinalizedFormsList(ctx: Context) {
val i = Intent(ctx, InstanceUploaderListActivity::class.java)
ctx.startActivity(i)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ interface ODKActivityInteractor {

/**
* Opens the draft form list screen in ODK Collect.
* @param formToFilter A list of form names to filter the draft forms. Can be null to show all draft forms.
* @param formsToFilter A list of form names to filter the draft forms. Can be null to show all draft forms.
* @param ctx The context of the calling activity.
**/
fun openDraftFormList(formToFilter: List<String>?, ctx: Context)
fun openDraftFormsList(formsToFilter: List<String>?, ctx: Context)

/**
* Opens the list of un-submitted forms screen in ODK Collect.
* @param ctx The context of the calling activity.
*/
fun openUnSubmittedFormList(ctx: Context)
fun openFinalizedFormsList(ctx: Context)
}
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<activity
android:name=".FormsListActivity"
android:exported="false" />
<activity android:name="io.samagra.oce_sample.InBuiltScreensActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class InBuiltScreensActivity : AppCompatActivity() {
setContentView(R.layout.activity_in_built_screens)
setSupportActionBar(findViewById(R.id.toolbar))
findViewById<Button>(R.id.drafts_list_btn).setOnClickListener {
odkInteractor.openDraftFormList(null, this)
odkInteractor.openDraftFormsList(null, this)
}
findViewById<Button>(R.id.unsent_list_btn).setOnClickListener {
odkInteractor.openUnSubmittedFormList(this)
odkInteractor.openFinalizedFormsList(this)
}
}

Expand Down