Skip to content

Commit

Permalink
Removed additional manifest file downloading since in most cases the …
Browse files Browse the repository at this point in the history
…file is downloaded before so then we should prepare the md5hash we need
  • Loading branch information
grzesiek2010 committed Apr 16, 2018
1 parent f8d232f commit 5c8276d
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class FormDetails implements Serializable {
private String formID;
private String formVersion;
private String hash;
private String manifestFileHash;
private boolean isNewerFormVersionAvailable;
private boolean areNewerMediaFilesAvailable;

Expand All @@ -34,14 +35,15 @@ public FormDetails(String error) {
}

public FormDetails(String formName, String downloadUrl, String manifestUrl, String formID,
String formVersion, String hash, boolean isNewerFormVersionAvailable,
boolean areNewerMediaFilesAvailable) {
String formVersion, String hash, String manifestFileHash,
boolean isNewerFormVersionAvailable, boolean areNewerMediaFilesAvailable) {
this.formName = formName;
this.downloadUrl = downloadUrl;
this.manifestUrl = manifestUrl;
this.formID = formID;
this.formVersion = formVersion;
this.hash = hash;
this.manifestFileHash = manifestFileHash;
this.isNewerFormVersionAvailable = isNewerFormVersionAvailable;
this.areNewerMediaFilesAvailable = areNewerMediaFilesAvailable;
}
Expand Down Expand Up @@ -74,6 +76,10 @@ public String getHash() {
return hash;
}

public String getManifestFileHash() {
return manifestFileHash;
}

public boolean isNewerFormVersionAvailable() {
return isNewerFormVersionAvailable;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018 Nafundi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.odk.collect.android.logic;

import java.util.List;

public class ManifestFile {
private String hash;
private List<MediaFile> mediaFiles;

public ManifestFile(String hash, List<MediaFile> mediaFiles) {
this.hash = hash;
this.mediaFiles = mediaFiles;
}

public String getHash() {
return hash;
}

public List<MediaFile> getMediaFiles() {
return mediaFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DownloadFormListTask extends AsyncTask<Void, String, HashMap<String

@Override
protected HashMap<String, FormDetails> doInBackground(Void... values) {
return DownloadFormListUtils.downloadFormList();
return DownloadFormListUtils.downloadFormList(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ protected Result onRunJob(@NonNull Params params) {
return Result.FAILURE;
} else {
GeneralSharedPreferences.getInstance().reset(POLL_SERVER_IMMEDIATELY_AFTER_RECEIVING_NETWORK);
HashMap<String, FormDetails> formList = DownloadFormListUtils.downloadFormList();
HashMap<String, FormDetails> formList = DownloadFormListUtils.downloadFormList(true);

if (formList != null && !formList.containsKey(DL_ERROR_MSG)) {
if (formList.containsKey(DL_AUTH_REQUIRED)) {
AuthDialogUtility.setWebCredentialsFromPreferences();
formList = DownloadFormListUtils.downloadFormList();
formList = DownloadFormListUtils.downloadFormList(true);

if (formList == null || formList.containsKey(DL_AUTH_REQUIRED) || formList.containsKey(DL_ERROR_MSG)) {
return Result.FAILURE;
Expand All @@ -87,9 +87,7 @@ protected Result onRunJob(@NonNull Params params) {
List<FormDetails> newDetectedForms = new ArrayList<>();
for (FormDetails formDetails : formList.values()) {
if (formDetails.isNewerFormVersionAvailable() || formDetails.areNewerMediaFilesAvailable()) {
String manifestFileHash = formDetails.getManifestUrl() != null ? FileUtils.getMd5Hash(WebUtils.getFileInputStream(formDetails.getManifestUrl())) : "";

String formVersionHash = DownloadFormsTask.getMd5Hash(formDetails.getHash()) + manifestFileHash;
String formVersionHash = DownloadFormsTask.getMd5Hash(formDetails.getHash()) + formDetails.getManifestFileHash();
if (!wasThisNewerFormVersionAlreadyDetected(formVersionHash)) {
newDetectedForms.add(formDetails);
updateLastDetectedFormVersionHash(formDetails.getFormID(), formVersionHash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class DocumentFetchResult {
public final int responseCode;
public final Document doc;
public final boolean isOpenRosaResponse;
private String md5Hash;


public DocumentFetchResult(String msg, int response) {
Expand All @@ -31,10 +32,15 @@ public DocumentFetchResult(String msg, int response) {
}


public DocumentFetchResult(Document doc, boolean isOpenRosaResponse) {
public DocumentFetchResult(Document doc, boolean isOpenRosaResponse, String md5Hash) {
responseCode = 0;
errorMessage = null;
this.doc = doc;
this.isOpenRosaResponse = isOpenRosaResponse;
this.md5Hash = md5Hash;
}

public String getMd5Hash() {
return md5Hash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.dao.FormsDao;
import org.odk.collect.android.logic.FormDetails;
import org.odk.collect.android.logic.ManifestFile;
import org.odk.collect.android.logic.MediaFile;
import org.odk.collect.android.preferences.PreferenceKeys;
import org.odk.collect.android.tasks.DownloadFormsTask;
Expand Down Expand Up @@ -54,7 +55,7 @@ private static boolean isXformsListNamespacedElement(Element e) {
private DownloadFormListUtils() {
}

public static HashMap<String, FormDetails> downloadFormList() {
public static HashMap<String, FormDetails> downloadFormList(boolean alwaysCheckMediaFiles) {
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(
Collect.getInstance().getBaseContext());
Expand Down Expand Up @@ -214,17 +215,23 @@ public static HashMap<String, FormDetails> downloadFormList() {
}
boolean isNewerFormVersionAvailable = false;
boolean areNewerMediaFilesAvailable = false;
ManifestFile manifestFile = null;
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);
if ((!isNewerFormVersionAvailable || alwaysCheckMediaFiles) && manifestUrl != null) {
manifestFile = getManifestFile(manifestUrl);
if (manifestFile != null) {
List<MediaFile> newMediaFiles = manifestFile.getMediaFiles();
if (newMediaFiles != null && newMediaFiles.size() > 0) {
areNewerMediaFilesAvailable = areNewerMediaFilesAvailable(formId, version, newMediaFiles);
}
}
}
}
formList.put(formId, new FormDetails(formName, downloadUrl, manifestUrl, formId,
(version != null) ? version : majorMinorVersion, hash, isNewerFormVersionAvailable, areNewerMediaFilesAvailable));
(version != null) ? version : majorMinorVersion, hash,
manifestFile != null && manifestFile.getHash() != null ? manifestFile.getHash() : "",
isNewerFormVersionAvailable, areNewerMediaFilesAvailable));
}
} else {
// Aggregate 0.9.x mode...
Expand Down Expand Up @@ -268,7 +275,7 @@ public static HashMap<String, FormDetails> downloadFormList() {
return formList;
}
formList.put(formName,
new FormDetails(formName, downloadUrl, null, formId, null, null, false, false));
new FormDetails(formName, downloadUrl, null, formId, null, null, null, false, false));

formId = null;
}
Expand All @@ -281,7 +288,7 @@ private static boolean isThisFormAlreadyDownloaded(String formId) {
return new FormsDao().getFormsCursorForFormId(formId).getCount() > 0;
}

private static List<MediaFile> downloadMediaFileList(String manifestUrl) {
private static ManifestFile getManifestFile(String manifestUrl) {
if (manifestUrl == null) {
return null;
}
Expand Down Expand Up @@ -382,7 +389,8 @@ private static List<MediaFile> downloadMediaFileList(String manifestUrl) {
files.add(new MediaFile(filename, hash, downloadUrl));
}
}
return files;

return new ManifestFile(result.getMd5Hash(), files);
}

private static boolean isNewerFormVersionAvailable(String md5Hash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.net.Uri;
import android.text.format.DateFormat;

import org.apache.commons.io.IOUtils;
import org.kxml2.io.KXmlParser;
import org.kxml2.kdom.Document;
import org.odk.collect.android.BuildConfig;
Expand Down Expand Up @@ -47,6 +48,7 @@
import org.opendatakit.httpclientandroidlib.protocol.HttpContext;
import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -336,11 +338,14 @@ public static DocumentFetchResult getXmlDocument(String urlString,
}
// parse response
Document doc = null;
String hash;
try {
InputStream is = null;
InputStreamReader isr = null;
try {
is = entity.getContent();
byte[] bytes = IOUtils.toByteArray(entity.getContent());
is = new ByteArrayInputStream(bytes);
hash = FileUtils.getMd5Hash(new ByteArrayInputStream(bytes));
Header contentEncoding = entity.getContentEncoding();
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase(
WebUtils.GZIP_CONTENT_ENCODING)) {
Expand Down Expand Up @@ -413,7 +418,7 @@ public static DocumentFetchResult getXmlDocument(String urlString,
Timber.w("%s unrecognized version(s): %s", WebUtils.OPEN_ROSA_VERSION_HEADER, b.toString());
}
}
return new DocumentFetchResult(doc, isOR);
return new DocumentFetchResult(doc, isOR, hash);
} catch (Exception e) {
String cause;
Throwable c = e;
Expand All @@ -428,72 +433,4 @@ public static DocumentFetchResult getXmlDocument(String urlString,
return new DocumentFetchResult(error, 0);
}
}

public static InputStream getFileInputStream(String urlString) {
URI u;
try {
URL url = new URL(urlString);
u = url.toURI();
} catch (URISyntaxException | MalformedURLException e) {
Timber.i(e, "Error converting URL %s to uri", urlString);
return null;
}

if (u.getHost() == null) {
return null;
}

// if https then enable preemptive basic auth...
if (u.getScheme().equals("https")) {
enablePreemptiveBasicAuth(Collect.getInstance().getHttpContext(), u.getHost());
}

// set up request...
HttpGet req = WebUtils.createOpenRosaHttpGet(u);
req.addHeader(WebUtils.ACCEPT_ENCODING_HEADER, WebUtils.GZIP_CONTENT_ENCODING);

HttpResponse response;
try {
response = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT).execute(req, Collect.getInstance().getHttpContext());
int statusCode = response.getStatusLine().getStatusCode();

HttpEntity entity = response.getEntity();

if (statusCode != HttpStatus.SC_OK) {
WebUtils.discardEntityBytes(response);
if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
// clear the cookies -- should not be necessary?
Collect.getInstance().getCookieStore().clear();
}
return null;
}

if (entity == null) {
Timber.e("No entity body returned from: %s", u.toString());
return null;
}

if (!entity.getContentType().getValue().toLowerCase(Locale.ENGLISH)
.contains(WebUtils.HTTP_CONTENT_TYPE_TEXT_XML)) {
WebUtils.discardEntityBytes(response);
Timber.e("ContentType: "
+ entity.getContentType().getValue()
+ " returned from: "
+ u.toString()
+ " is not text/xml. This is often caused a network proxy. Do you need "
+ "to login to your network?");
return null;
}

return entity.getContent();
} catch (Exception e) {
Throwable c = e;
while (c.getCause() != null) {
c = c.getCause();
}

Timber.w("Error: " + c.toString() + " while accessing " + u.toString());
return null;
}
}
}

0 comments on commit 5c8276d

Please sign in to comment.