@Test(expected = NotfoundException.class) public void testWriteNotFound() throws Exception { final Path test = new Path(new SFTPHomeDirectoryService(session).find().getAbsolute() + "/nosuchdirectory/" + UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); new SFTPWriteFeature(session).write(test, new TransferStatus(), new DisabledConnectionCallback()); }
@Override public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final EnumSet<OpenMode> flags; if(status.isAppend()) { if(status.isExists()) { flags = EnumSet.of(OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE); } } else { if(status.isExists()) { if(file.isSymbolicLink()) { session.sftp().remove(file.getAbsolute()); flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE); } } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } } final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags); final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status); if(log.isInfoEnabled()) { log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites)); } if(log.isInfoEnabled()) { log.info(String.format("Skipping %d bytes", status.getOffset())); } return new VoidStatusOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) { private final AtomicBoolean close = new AtomicBoolean(); @Override public void close() throws IOException { if(close.get()) { log.warn(String.format("Skip double close of stream %s", this)); return; } try { super.close(); } finally { handle.close(); close.set(true); } } }); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file); } }
SFTPWriteFeature extends AppendWriteFeature<Void> { @Override public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final EnumSet<OpenMode> flags; if(status.isAppend()) { if(status.isExists()) { flags = EnumSet.of(OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE); } } else { if(status.isExists()) { if(file.isSymbolicLink()) { session.sftp().remove(file.getAbsolute()); flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE); } } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } } final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags); final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status); if(log.isInfoEnabled()) { log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites)); } if(log.isInfoEnabled()) { log.info(String.format("Skipping %d bytes", status.getOffset())); } return new VoidStatusOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) { private final AtomicBoolean close = new AtomicBoolean(); @Override public void close() throws IOException { if(close.get()) { log.warn(String.format("Skip double close of stream %s", this)); return; } try { super.close(); } finally { handle.close(); close.set(true); } } }); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file); } } }
SFTPWriteFeature extends AppendWriteFeature<Void> { @Override public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final EnumSet<OpenMode> flags; if(status.isAppend()) { if(status.isExists()) { flags = EnumSet.of(OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE); } } else { if(status.isExists()) { if(file.isSymbolicLink()) { session.sftp().remove(file.getAbsolute()); flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE); } } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } } final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags); final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status); if(log.isInfoEnabled()) { log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites)); } if(log.isInfoEnabled()) { log.info(String.format("Skipping %d bytes", status.getOffset())); } return new VoidStatusOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) { private final AtomicBoolean close = new AtomicBoolean(); @Override public void close() throws IOException { if(close.get()) { log.warn(String.format("Skip double close of stream %s", this)); return; } try { super.close(); } finally { handle.close(); close.set(true); } } }); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file); } } SFTPWriteFeature(final SFTPSession session); }
SFTPWriteFeature extends AppendWriteFeature<Void> { @Override public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final EnumSet<OpenMode> flags; if(status.isAppend()) { if(status.isExists()) { flags = EnumSet.of(OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE); } } else { if(status.isExists()) { if(file.isSymbolicLink()) { session.sftp().remove(file.getAbsolute()); flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE); } } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } } final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags); final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status); if(log.isInfoEnabled()) { log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites)); } if(log.isInfoEnabled()) { log.info(String.format("Skipping %d bytes", status.getOffset())); } return new VoidStatusOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) { private final AtomicBoolean close = new AtomicBoolean(); @Override public void close() throws IOException { if(close.get()) { log.warn(String.format("Skip double close of stream %s", this)); return; } try { super.close(); } finally { handle.close(); close.set(true); } } }); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file); } } SFTPWriteFeature(final SFTPSession session); @Override StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean temporary(); @Override boolean random(); }
SFTPWriteFeature extends AppendWriteFeature<Void> { @Override public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final EnumSet<OpenMode> flags; if(status.isAppend()) { if(status.isExists()) { flags = EnumSet.of(OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.WRITE); } } else { if(status.isExists()) { if(file.isSymbolicLink()) { session.sftp().remove(file.getAbsolute()); flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } else { flags = EnumSet.of(OpenMode.TRUNC, OpenMode.WRITE); } } else { flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); } } final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags); final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status); if(log.isInfoEnabled()) { log.info(String.format("Using %d unconfirmed writes", maxUnconfirmedWrites)); } if(log.isInfoEnabled()) { log.info(String.format("Skipping %d bytes", status.getOffset())); } return new VoidStatusOutputStream(handle.new RemoteFileOutputStream(status.getOffset(), maxUnconfirmedWrites) { private final AtomicBoolean close = new AtomicBoolean(); @Override public void close() throws IOException { if(close.get()) { log.warn(String.format("Skip double close of stream %s", this)); return; } try { super.close(); } finally { handle.close(); close.set(true); } } }); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Upload {0} failed", e, file); } } SFTPWriteFeature(final SFTPSession session); @Override StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean temporary(); @Override boolean random();  }