use of org.odk.collect.forms.ManifestFile in project collect by opendatakit.
the class OpenRosaFormSource method fetchManifest.
@Override
public ManifestFile fetchManifest(String manifestURL) throws FormSourceException {
if (manifestURL == null) {
return null;
}
DocumentFetchResult result = mapException(() -> openRosaXMLFetcher.getXML(manifestURL));
if (result.errorMessage != null) {
if (result.responseCode != HttpURLConnection.HTTP_OK) {
throw new FormSourceException.ServerError(result.responseCode, serverURL);
} else {
throw new FormSourceException.FetchError();
}
}
if (!result.isOpenRosaResponse) {
throw new FormSourceException.ParseError(serverURL);
}
List<MediaFile> mediaFiles = openRosaResponseParser.parseManifest(result.doc);
if (mediaFiles != null) {
return new ManifestFile(result.getHash(), mediaFiles);
} else {
throw new FormSourceException.ParseError(serverURL);
}
}
use of org.odk.collect.forms.ManifestFile in project collect by opendatakit.
the class ServerFormDownloaderTest method whenFormHasMediaFiles_downloadsAndSavesFormAndMediaFiles_beforeParsingForm.
/**
* Form parsing might need access to media files (external secondary instances) for example
* so we need to make sure we've got those files in the right place before we parse.
*/
@Test
public void whenFormHasMediaFiles_downloadsAndSavesFormAndMediaFiles_beforeParsingForm() throws Exception {
String xform = createXFormBody("id", "version");
ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, new ManifestFile("", asList(new MediaFile("file1", "hash-1", "http://file1"), new MediaFile("file2", "hash-2", "http://file2"))));
FormSource formSource = mock(FormSource.class);
when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
when(formSource.fetchMediaFile("http://file1")).thenReturn(new ByteArrayInputStream("contents1".getBytes()));
when(formSource.fetchMediaFile("http://file2")).thenReturn(new ByteArrayInputStream("contents2".getBytes()));
FormMetadataParser formMetadataParser = new FormMetadataParser() {
@Override
public Map<String, String> parse(File file, File mediaDir) {
File[] mediaFiles = mediaDir.listFiles();
assertThat(mediaFiles.length, is(2));
assertThat(stream(mediaFiles).map(File::getName).collect(toList()), containsInAnyOrder("file1", "file2"));
return super.parse(file, mediaDir);
}
};
ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), formMetadataParser, mock(Analytics.class));
downloader.downloadForm(serverFormDetails, null, null);
}
use of org.odk.collect.forms.ManifestFile in project collect by opendatakit.
the class ServerFormDownloaderTest method whenFormAlreadyDownloaded_andFormHasNewMediaFiles_updatesMediaFiles.
@Test
public void whenFormAlreadyDownloaded_andFormHasNewMediaFiles_updatesMediaFiles() throws Exception {
String xform = createXFormBody("id", "version");
ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, new ManifestFile("", asList(new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents".getBytes())), "http://file1"))));
FormSource formSource = mock(FormSource.class);
when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
when(formSource.fetchMediaFile("http://file1")).thenReturn(new ByteArrayInputStream("contents".getBytes()));
ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
// Initial download
downloader.downloadForm(serverFormDetails, null, null);
ServerFormDetails serverFormDetailsUpdatedMediaFile = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), false, false, new ManifestFile("", asList(new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents-updated".getBytes())), "http://file1"))));
when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
when(formSource.fetchMediaFile("http://file1")).thenReturn(new ByteArrayInputStream("contents-updated".getBytes()));
// Second download
downloader.downloadForm(serverFormDetailsUpdatedMediaFile, null, null);
List<Form> allForms = formsRepository.getAll();
assertThat(allForms.size(), is(1));
Form form = allForms.get(0);
assertThat(form.getFormId(), is("id"));
File formFile = new File(getAbsoluteFilePath(formsDir.getAbsolutePath(), form.getFormFilePath()));
assertThat(formFile.exists(), is(true));
assertThat(new String(read(formFile)), is(xform));
File mediaFile1 = new File(form.getFormMediaPath() + "/file1");
assertThat(mediaFile1.exists(), is(true));
assertThat(new String(read(mediaFile1)), is("contents-updated"));
}
use of org.odk.collect.forms.ManifestFile in project collect by opendatakit.
the class ServerFormDownloaderTest method afterDownloadingMediaFile_cancelling_throwsDownloadingInterruptedExceptionAndDoesNotSaveAnything.
@Test
public void afterDownloadingMediaFile_cancelling_throwsDownloadingInterruptedExceptionAndDoesNotSaveAnything() throws Exception {
String xform = createXFormBody("id", "version");
ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, new ManifestFile("", asList(new MediaFile("file1", "hash-1", "http://file1"), new MediaFile("file2", "hash-2", "http://file2"))));
CancelAfterMediaFileDownloadFormSource formListApi = new CancelAfterMediaFileDownloadFormSource(xform);
ServerFormDownloader downloader = new ServerFormDownloader(formListApi, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
try {
downloader.downloadForm(serverFormDetails, null, formListApi);
fail("Excepted exception");
} catch (FormDownloadException.DownloadingInterrupted e) {
assertThat(formsRepository.getAll(), is(empty()));
assertThat(asList(new File(getCacheFilesPath()).listFiles()), is(empty()));
assertThat(asList(new File(getFormFilesPath()).listFiles()), is(empty()));
}
}
use of org.odk.collect.forms.ManifestFile in project collect by opendatakit.
the class ServerFormDownloaderTest method whenFormHasMediaFiles_downloadsAndSavesFormAndMediaFiles.
@Test
public void whenFormHasMediaFiles_downloadsAndSavesFormAndMediaFiles() throws Exception {
String xform = createXFormBody("id", "version");
ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, new ManifestFile("", asList(new MediaFile("file1", "hash-1", "http://file1"), new MediaFile("file2", "hash-2", "http://file2"))));
FormSource formSource = mock(FormSource.class);
when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
when(formSource.fetchMediaFile("http://file1")).thenReturn(new ByteArrayInputStream("contents1".getBytes()));
when(formSource.fetchMediaFile("http://file2")).thenReturn(new ByteArrayInputStream("contents2".getBytes()));
ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
downloader.downloadForm(serverFormDetails, null, null);
List<Form> allForms = formsRepository.getAll();
assertThat(allForms.size(), is(1));
Form form = allForms.get(0);
assertThat(form.getFormId(), is("id"));
File formFile = new File(getAbsoluteFilePath(formsDir.getAbsolutePath(), form.getFormFilePath()));
assertThat(formFile.exists(), is(true));
assertThat(new String(read(formFile)), is(xform));
File mediaFile1 = new File(form.getFormMediaPath() + "/file1");
assertThat(mediaFile1.exists(), is(true));
assertThat(new String(read(mediaFile1)), is("contents1"));
File mediaFile2 = new File(form.getFormMediaPath() + "/file2");
assertThat(mediaFile2.exists(), is(true));
assertThat(new String(read(mediaFile2)), is("contents2"));
}
Aggregations