FileSystemSyncEndpoint implements SyncEndpoint { public boolean syncFile(MonitoredFile syncFile, File watchDir) { boolean success = false; File syncToFile = getSyncToFile(syncFile, watchDir); logger.info("Syncing file: " + syncFile.getAbsolutePath() + "\n to " + syncToFile.getAbsolutePath()); if (syncFile.exists()) { InputStream inStream = null; OutputStream outStream = null; try { if (!syncToFile.getParentFile().exists()) { createParentDir(syncToFile.getParentFile()); } inStream = syncFile.getStream(); outStream = new FileOutputStream(syncToFile); IOUtils.copy(inStream, outStream); success = true; } catch (IOException e) { logger.error("Unable to sync updated file " + syncFile.getAbsolutePath() + " to " + syncToFile.getAbsolutePath() + " due to " + e.getMessage(), e); success = false; } finally { IOUtils.closeQuietly(inStream); IOUtils.closeQuietly(outStream); } } else { if (syncDeletes) { success = syncToFile.delete(); } else { success = true; } } return success; } FileSystemSyncEndpoint(File syncToDir, boolean syncDeletes); boolean syncFile(MonitoredFile syncFile, File watchDir); @Override SyncResultType syncFileAndReturnDetailedResult(MonitoredFile monitoredFile,
                                                          File watchDir); Iterator<String> getFilesList(); @Override void addEndPointListener(EndPointListener listener); @Override void removeEndPointListener(EndPointListener listener); @Override void deleteContent(String spaceId, String contentId);  }
@Test public void testSyncToFileSystem() throws Exception { File syncToDir = createTempDir("syncToDir"); FileSystemSyncEndpoint syncEndpoint = new FileSystemSyncEndpoint(syncToDir, true); File watchDir = createTempDir("watchDir"); MonitoredFile syncFile = new MonitoredFile(File.createTempFile("sync", "file", watchDir)); assertTrue(syncFile.exists()); File syncToFile = new File(syncToDir, syncFile.getName()); assertFalse(syncToFile.exists()); syncEndpoint.syncFile(syncFile, watchDir); assertTrue(syncToFile.exists()); FileUtils.deleteDirectory(syncToDir); FileUtils.deleteDirectory(watchDir); }
@Test public void testSyncFileNoWatchDir() throws Exception { File syncToDir = createTempDir("syncToDir"); FileSystemSyncEndpoint syncEndpoint = new FileSystemSyncEndpoint(syncToDir, true); File baseFile = File.createTempFile("sync", "file"); MonitoredFile syncFile = new MonitoredFile(baseFile); assertTrue(syncFile.exists()); File syncToFile = new File(syncToDir, syncFile.getName()); assertFalse(syncToFile.exists()); syncEndpoint.syncFile(syncFile, null); assertTrue(syncToFile.exists()); FileUtils.deleteDirectory(syncToDir); FileUtils.deleteQuietly(baseFile); }