use of ch.cyberduck.core.transfer.TransferAction in project cyberduck by iterate-ch.
the class CryptoB2SingleTransferWorkerTest method testUpload.
@Test
public void testUpload() throws Exception {
final Path home = new Path("/test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path vault = new Path(home, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Path dir1 = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
final Local localDirectory1 = new Local(System.getProperty("java.io.tmpdir"), new AlphanumericRandomStringService().random());
new DefaultLocalDirectoryFeature().mkdir(localDirectory1);
final byte[] content = RandomUtils.nextBytes(62768);
final Path file1 = new Path(dir1, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final Local localFile1 = new Local(localDirectory1, file1.getName());
final OutputStream out1 = localFile1.getOutputStream(false);
IOUtils.write(content, out1);
out1.close();
final Path file2 = new Path(dir1, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final Local localFile2 = new Local(localDirectory1, file2.getName());
final OutputStream out2 = localFile2.getOutputStream(false);
IOUtils.write(content, out2);
out2.close();
final CryptoVault cryptomator = new CryptoVault(vault);
cryptomator.create(session, new VaultCredentials("test"), new DisabledPasswordStore(), vaultVersion);
session.withRegistry(new DefaultVaultRegistry(new DisabledPasswordStore(), new DisabledPasswordCallback(), cryptomator));
final Transfer t = new UploadTransfer(new Host(new TestProtocol()), Collections.singletonList(new TransferItem(dir1, localDirectory1)), new NullFilter<>());
assertTrue(new SingleTransferWorker(session, session, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledLoginCallback(), new DisabledNotificationService()) {
}.run(session));
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
assertTrue(new CryptoFindFeature(session, new B2FindFeature(session, fileid), cryptomator).find(dir1));
assertEquals(content.length, new CryptoAttributesFeature(session, new B2AttributesFinderFeature(session, fileid), cryptomator).find(file1).getSize());
{
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
final InputStream in = new CryptoReadFeature(session, new B2ReadFeature(session, fileid), cryptomator).read(file1, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(in, buffer);
assertArrayEquals(content, buffer.toByteArray());
}
assertEquals(content.length, new CryptoAttributesFeature(session, new B2AttributesFinderFeature(session, fileid), cryptomator).find(file2).getSize());
{
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
final InputStream in = new CryptoReadFeature(session, new B2ReadFeature(session, fileid), cryptomator).read(file1, new TransferStatus().withLength(content.length), new DisabledConnectionCallback());
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(in, buffer);
assertArrayEquals(content, buffer.toByteArray());
}
cryptomator.getFeature(session, Delete.class, new B2DeleteFeature(session, fileid)).delete(Arrays.asList(file1, file2, dir1, vault), new DisabledLoginCallback(), new Delete.DisabledCallback());
localFile1.delete();
localFile2.delete();
localDirectory1.delete();
}
use of ch.cyberduck.core.transfer.TransferAction in project cyberduck by iterate-ch.
the class AbstractTransferWorker method run.
@Override
public Boolean run(final Session<?> source) throws BackgroundException {
final String lock = sleep.lock();
final Session<?> destination = this.borrow(Connection.destination);
try {
if (log.isDebugEnabled()) {
log.debug(String.format("Start transfer with prompt %s and options %s", prompt, options));
}
// Determine the filter to match files against
final TransferAction action = transfer.action(source, destination, options.resumeRequested, options.reloadRequested, prompt, new DisabledListProgressListener() {
@Override
public void message(final String message) {
progress.message(message);
}
});
if (log.isDebugEnabled()) {
log.debug(String.format("Selected transfer action %s", action));
}
if (action.equals(TransferAction.cancel)) {
if (log.isInfoEnabled()) {
log.info(String.format("Transfer %s canceled by user", this));
}
throw new TransferCanceledException();
}
// Reset the cached size of the transfer and progress value
transfer.reset();
// Normalize Paths before preparing
transfer.normalize();
// Calculate information about the files in advance to give progress information
for (TransferItem next : transfer.getRoots()) {
this.prepare(next.remote, next.local, new TransferStatus().exists(true), action);
}
this.await();
meter.reset();
transfer.pre(source, destination, table, connect);
// Transfer all files sequentially
for (TransferItem next : transfer.getRoots()) {
this.transfer(next, action);
}
this.await();
transfer.post(source, destination, table, connect);
} finally {
this.release(source, Connection.source, null);
this.release(destination, Connection.destination, null);
if (transfer.isReset()) {
notification.notify(transfer.getName(), transfer.getUuid(), transfer.isComplete() ? String.format("%s complete", StringUtils.capitalize(transfer.getType().name())) : "Transfer incomplete", transfer.getName());
}
sleep.release(lock);
table.clear();
cache.clear();
}
return true;
}
use of ch.cyberduck.core.transfer.TransferAction in project cyberduck by iterate-ch.
the class SingleTransferWorkerTest method testUploadPrepareOverrideRootDoesNotExist.
@Test
public void testUploadPrepareOverrideRootDoesNotExist() throws Exception {
final Path child = new Path("/t/c", EnumSet.of(Path.Type.file));
final Path root = new Path("/t", EnumSet.of(Path.Type.directory)) {
@Override
public Path getParent() {
return new Path("/", EnumSet.of(Path.Type.directory));
}
};
final NullLocal local = new NullLocal("l") {
@Override
public AttributedList<Local> list() {
AttributedList<Local> l = new AttributedList<Local>();
l.add(new NullLocal(this.getAbsolute(), "c") {
@Override
public boolean exists() {
return true;
}
});
return l;
}
@Override
public boolean exists() {
return true;
}
};
final Transfer t = new UploadTransfer(new Host(new TestProtocol()), root, local) {
@Override
public void transfer(final Session<?> source, final Session<?> destination, final Path file, Local local, final TransferOptions options, final TransferStatus overall, final TransferStatus segment, final ConnectionCallback connectionCallback, final ProgressListener listener, final StreamListener streamListener) {
//
}
};
final NullSession session = new NullSession(new Host(new TestProtocol()));
final SingleTransferWorker worker = new SingleTransferWorker(session, session, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledLoginCallback(), new DisabledNotificationService()) {
@Override
public Future<TransferStatus> transfer(final TransferItem item, final TransferAction action) throws BackgroundException {
if (item.remote.equals(root)) {
assertTrue(this.getCache().isCached(new TransferItem(root, local)));
}
super.transfer(new TransferItem(item.remote, new NullLocal("l") {
@Override
public AttributedList<Local> list() {
AttributedList<Local> l = new AttributedList<Local>();
l.add(new NullLocal(this.getAbsolute(), "c"));
return l;
}
}), action);
assertFalse(this.getCache().isCached(new TransferItem(child, local)));
return null;
}
};
worker.run(session);
assertFalse(worker.getCache().isCached(new TransferItem(child, local)));
}
use of ch.cyberduck.core.transfer.TransferAction in project cyberduck by iterate-ch.
the class SingleTransferWorkerTest method testDownloadPrepareOverride.
@Test
public void testDownloadPrepareOverride() throws Exception {
final Path child = new Path("/t/c", EnumSet.of(Path.Type.file));
final Path root = new Path("/t", EnumSet.of(Path.Type.directory));
final NullLocal local = new NullLocal("l") {
@Override
public boolean exists() {
return true;
}
@Override
public boolean isDirectory() {
return true;
}
@Override
public boolean isFile() {
return false;
}
@Override
public AttributedList<Local> list() {
return AttributedList.emptyList();
}
};
final Transfer t = new DownloadTransfer(new Host(new TestProtocol()), root, local) {
@Override
public void transfer(final Session<?> source, final Session<?> destination, final Path file, Local local, final TransferOptions options, final TransferStatus overall, final TransferStatus segment, final ConnectionCallback connectionCallback, final ProgressListener listener, final StreamListener streamListener) {
if (file.equals(root)) {
assertTrue(segment.isExists());
} else {
assertFalse(segment.isExists());
}
}
@Override
public AbstractDownloadFilter filter(final Session<?> source, final Session<?> destination, final TransferAction action, final ProgressListener listener) {
return super.filter(source, destination, action, listener).withAttributes(new AttributesFinder() {
@Override
public PathAttributes find(final Path file, final ListProgressListener listener) {
return file.attributes();
}
});
}
};
final NullSession session = new NullSession(new Host(new TestProtocol())) {
@Override
public AttributedList<Path> list(final Path file, final ListProgressListener listener) {
final AttributedList<Path> children = new AttributedList<Path>();
children.add(child);
return children;
}
};
final SingleTransferWorker worker = new SingleTransferWorker(session, session, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledLoginCallback(), new DisabledNotificationService()) {
@Override
public Future<TransferStatus> transfer(final TransferItem item, final TransferAction action) throws BackgroundException {
if (item.remote.equals(root)) {
assertTrue(this.getCache().isCached(new TransferItem(root, local)));
}
super.transfer(new TransferItem(item.remote, new NullLocal("l")), action);
if (item.remote.equals(root)) {
assertFalse(this.getCache().isCached(new TransferItem(root, local)));
}
return null;
}
};
worker.run(session);
assertFalse(worker.getCache().isCached(new TransferItem(child, local)));
assertTrue(worker.getCache().isEmpty());
}
use of ch.cyberduck.core.transfer.TransferAction in project cyberduck by iterate-ch.
the class ConcurrentTransferWorkerTest method testConcurrentSessions.
@Test
public void testConcurrentSessions() throws Exception {
final int files = 20;
final int connections = 3;
final Set<Path> transferred = ConcurrentHashMap.newKeySet();
final List<TransferItem> list = new ArrayList<TransferItem>();
for (int i = 1; i <= files; i++) {
list.add(new TransferItem(new Path("/t" + i, EnumSet.of(Path.Type.file)), new NullLocal("/t" + i)));
}
final Host host = new Host(new TestProtocol(), "test.cyberduck.ch");
final Transfer t = new DownloadTransfer(host, list) {
@Override
public void transfer(final Session<?> source, final Session<?> destination, final Path file, final Local local, final TransferOptions options, final TransferStatus overall, final TransferStatus segment, final ConnectionCallback connectionCallback, final ProgressListener listener, final StreamListener streamListener) {
assertNotNull(source);
transferred.add(file);
}
@Override
public AbstractDownloadFilter filter(final Session<?> source, final Session<?> destination, final TransferAction action, final ProgressListener listener) {
return new AbstractDownloadFilter(new DisabledDownloadSymlinkResolver(), source, null) {
@Override
public boolean accept(final Path file, final Local local, final TransferStatus parent) {
assertFalse(transferred.contains(file));
return true;
}
@Override
public TransferStatus prepare(final Path file, final Local local, final TransferStatus parent, final ProgressListener progress) {
assertFalse(transferred.contains(file));
return new TransferStatus();
}
@Override
public void apply(final Path file, final Local local, final TransferStatus status, final ProgressListener listener) {
assertFalse(transferred.contains(file));
}
@Override
public void complete(final Path file, final Local local, final TransferOptions options, final TransferStatus status, final ProgressListener listener) {
assertTrue(transferred.contains(file));
}
};
}
};
final LoginConnectionService connection = new TestLoginConnectionService();
final DefaultSessionPool pool = new DefaultSessionPool(connection, new DisabledX509TrustManager(), new DefaultX509KeyManager(), new DefaultVaultRegistry(new DisabledPasswordCallback()), new DisabledTranscriptListener(), host);
final ConcurrentTransferWorker worker = new ConcurrentTransferWorker(pool, SessionPool.DISCONNECTED, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledLoginCallback(), new DisabledProgressListener(), new DisabledStreamListener(), new DisabledNotificationService());
pool.withMaxTotal(connections);
final Session<?> session = worker.borrow(ConcurrentTransferWorker.Connection.source);
assertTrue(worker.run(session));
worker.release(session, ConcurrentTransferWorker.Connection.source, null);
for (int i = 1; i <= files; i++) {
assertTrue(transferred.contains(new Path("/t" + i, EnumSet.of(Path.Type.file))));
}
worker.cleanup(true);
}
Aggregations