RetrievalWorker implements Runnable { protected Map<String, String> retrieveToFile(File localFile, RetrievalListener listener) throws IOException { try { contentStream = new Retrier(5, 4000, 3).execute(() -> { return source.getSourceContent(contentItem, listener); }); } catch (Exception ex) { throw new IOException(ex); } try ( InputStream inStream = contentStream.getStream(); OutputStream outStream = new FileOutputStream(localFile); ) { IOUtils.copyLarge(inStream, outStream); } catch (IOException e) { try { deleteFile(localFile); } catch (IOException ioe) { logger.error("Exception deleting local file " + localFile.getAbsolutePath() + " due to: " + ioe.getMessage()); } throw e; } if (!checksumsMatch(localFile, contentStream.getChecksum())) { deleteFile(localFile); throw new IOException("Calculated checksum value for retrieved " + "file does not match properties checksum."); } if (applyTimestamps) { applyTimestamps(contentStream, localFile); } return contentStream.getProperties(); } RetrievalWorker(ContentItem contentItem,
                           RetrievalSource source,
                           File contentDir,
                           boolean overwrite,
                           OutputWriter outWriter,
                           boolean createSpaceDir,
                           boolean applyTimestamps); void run(); Map<String, String> retrieveFile(); Map<String, String> retrieveFile(RetrievalListener listener); File getLocalFile();  }
@Test public void testRetrieveToFile() throws Exception { RetrievalWorker worker = createRetrievalWorker(true); File localFile = new File(tempDir, "retrieve-to-file-test"); assertFalse(localFile.exists()); worker.retrieveToFile(localFile, null); assertTrue(localFile.exists()); BasicFileAttributes fileAttributes = Files.readAttributes(localFile.toPath(), BasicFileAttributes.class); assertEquals(testTime, fileAttributes.creationTime().toMillis()); assertEquals(testTime, fileAttributes.lastAccessTime().toMillis()); assertEquals(testTime, fileAttributes.lastModifiedTime().toMillis()); String fileValue = FileUtils.readFileToString(localFile); assertEquals(fileValue, contentValue); worker = createBrokenRetrievalWorker(true); localFile = new File(tempDir, "retrieve-to-file-failure-test"); assertFalse(localFile.exists()); try { worker.retrieveToFile(localFile, null); fail("Exception expected with non-matching checksum"); } catch (IOException expected) { assertNotNull(expected); } }