use of org.opendatakit.httpclientandroidlib.client.HttpClient in project collect by opendatakit.
the class DownloadFormListTask method doInBackground.
@Override
protected HashMap<String, FormDetails> doInBackground(Void... values) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(Collect.getInstance().getBaseContext());
String downloadListUrl = settings.getString(PreferenceKeys.KEY_SERVER_URL, Collect.getInstance().getString(R.string.default_server_url));
// NOTE: /formlist must not be translated! It is the well-known path on the server.
String formListUrl = Collect.getInstance().getApplicationContext().getString(R.string.default_odk_formlist);
String downloadPath = settings.getString(PreferenceKeys.KEY_FORMLIST_URL, formListUrl);
downloadListUrl += downloadPath;
Collect.getInstance().getActivityLogger().logAction(this, formListUrl, downloadListUrl);
// We populate this with available forms from the specified server.
// <formname, details>
HashMap<String, FormDetails> formList = new HashMap<String, FormDetails>();
// get shared HttpContext so that authentication and cookies are retained.
HttpContext localContext = Collect.getInstance().getHttpContext();
HttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
DocumentFetchResult result = WebUtils.getXmlDocument(downloadListUrl, localContext, httpclient);
// If we can't get the document, return the error, cancel the task
if (result.errorMessage != null) {
if (result.responseCode == 401) {
formList.put(DL_AUTH_REQUIRED, new FormDetails(result.errorMessage));
} else {
formList.put(DL_ERROR_MSG, new FormDetails(result.errorMessage));
}
return formList;
}
if (result.isOpenRosaResponse) {
// Attempt OpenRosa 1.0 parsing
Element xformsElement = result.doc.getRootElement();
if (!xformsElement.getName().equals("xforms")) {
String error = "root element is not <xforms> : " + xformsElement.getName();
Timber.e("Parsing OpenRosa reply -- %s", error);
formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
return formList;
}
String namespace = xformsElement.getNamespace();
if (!isXformsListNamespacedElement(xformsElement)) {
String error = "root element namespace is incorrect:" + namespace;
Timber.e("Parsing OpenRosa reply -- %s", error);
formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
return formList;
}
int elements = xformsElement.getChildCount();
for (int i = 0; i < elements; ++i) {
if (xformsElement.getType(i) != Element.ELEMENT) {
// e.g., whitespace (text)
continue;
}
Element xformElement = xformsElement.getElement(i);
if (!isXformsListNamespacedElement(xformElement)) {
// someone else's extension?
continue;
}
String name = xformElement.getName();
if (!name.equalsIgnoreCase("xform")) {
// someone else's extension?
continue;
}
// this is something we know how to interpret
String formId = null;
String formName = null;
String version = null;
String majorMinorVersion = null;
String description = null;
String downloadUrl = null;
String manifestUrl = null;
String hash = null;
// don't process descriptionUrl
int fieldCount = xformElement.getChildCount();
for (int j = 0; j < fieldCount; ++j) {
if (xformElement.getType(j) != Element.ELEMENT) {
// whitespace
continue;
}
Element child = xformElement.getElement(j);
if (!isXformsListNamespacedElement(child)) {
// someone else's extension?
continue;
}
String tag = child.getName();
switch(tag) {
case "formID":
formId = XFormParser.getXMLText(child, true);
if (formId != null && formId.length() == 0) {
formId = null;
}
break;
case "name":
formName = XFormParser.getXMLText(child, true);
if (formName != null && formName.length() == 0) {
formName = null;
}
break;
case "version":
version = XFormParser.getXMLText(child, true);
if (version != null && version.length() == 0) {
version = null;
}
break;
case "majorMinorVersion":
majorMinorVersion = XFormParser.getXMLText(child, true);
if (majorMinorVersion != null && majorMinorVersion.length() == 0) {
majorMinorVersion = null;
}
break;
case "descriptionText":
description = XFormParser.getXMLText(child, true);
if (description != null && description.length() == 0) {
description = null;
}
break;
case "downloadUrl":
downloadUrl = XFormParser.getXMLText(child, true);
if (downloadUrl != null && downloadUrl.length() == 0) {
downloadUrl = null;
}
break;
case "manifestUrl":
manifestUrl = XFormParser.getXMLText(child, true);
if (manifestUrl != null && manifestUrl.length() == 0) {
manifestUrl = null;
}
break;
case "hash":
hash = XFormParser.getXMLText(child, true);
if (hash != null && hash.length() == 0) {
hash = null;
}
break;
}
}
if (formId == null || downloadUrl == null || formName == null) {
String error = "Forms list entry " + Integer.toString(i) + " has missing or empty tags: formID, name, or downloadUrl";
Timber.e("Parsing OpenRosa reply -- %s", error);
formList.clear();
formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_openrosa_formlist_failed, error)));
return formList;
}
boolean isNewerFormVersionAvailable = false;
boolean areNewerMediaFilesAvailable = false;
if (isThisFormAlreadyDownloaded(formId)) {
isNewerFormVersionAvailable = isNewerFormVersionAvailable(DownloadFormsTask.getMd5Hash(hash));
if (!isNewerFormVersionAvailable && manifestUrl != null) {
List<MediaFile> newMediaFiles = downloadMediaFileList(manifestUrl);
if (newMediaFiles != null && newMediaFiles.size() > 0) {
areNewerMediaFilesAvailable = areNewerMediaFilesAvailable(formId, version, newMediaFiles);
}
}
}
formList.put(formId, new FormDetails(formName, downloadUrl, manifestUrl, formId, (version != null) ? version : majorMinorVersion, isNewerFormVersionAvailable, areNewerMediaFilesAvailable));
}
} else {
// Aggregate 0.9.x mode...
// populate HashMap with form names and urls
Element formsElement = result.doc.getRootElement();
int formsCount = formsElement.getChildCount();
String formId = null;
for (int i = 0; i < formsCount; ++i) {
if (formsElement.getType(i) != Element.ELEMENT) {
// whitespace
continue;
}
Element child = formsElement.getElement(i);
String tag = child.getName();
if (tag.equals("formID")) {
formId = XFormParser.getXMLText(child, true);
if (formId != null && formId.length() == 0) {
formId = null;
}
}
if (tag.equalsIgnoreCase("form")) {
String formName = XFormParser.getXMLText(child, true);
if (formName != null && formName.length() == 0) {
formName = null;
}
String downloadUrl = child.getAttributeValue(null, "url");
downloadUrl = downloadUrl.trim();
if (downloadUrl != null && downloadUrl.length() == 0) {
downloadUrl = null;
}
if (downloadUrl == null || formName == null) {
String error = "Forms list entry " + Integer.toString(i) + " is missing form name or url attribute";
Timber.e("Parsing OpenRosa reply -- %s", error);
formList.clear();
formList.put(DL_ERROR_MSG, new FormDetails(Collect.getInstance().getString(R.string.parse_legacy_formlist_failed, error)));
return formList;
}
formList.put(formName, new FormDetails(formName, downloadUrl, null, formId, null, false, false));
formId = null;
}
}
}
return formList;
}
Aggregations