use of ch.cyberduck.core.Session in project cyberduck by iterate-ch.
the class MoveWorker method run.
@Override
public Map<Path, Path> run(final Session<?> session) throws BackgroundException {
final Session<?> destination = target.borrow(new BackgroundActionState() {
@Override
public boolean isCanceled() {
return MoveWorker.this.isCanceled();
}
@Override
public boolean isRunning() {
return true;
}
});
try {
final Move feature = session.getFeature(Move.class).withTarget(destination);
if (log.isDebugEnabled()) {
log.debug(String.format("Run with feature %s", feature));
}
final ListService list = session.getFeature(ListService.class);
// sort ascending by timestamp to move older versions first
final Map<Path, Path> sorted = new TreeMap<>(new VersionsComparator(true));
sorted.putAll(files);
final Map<Path, Path> result = new HashMap<>();
for (Map.Entry<Path, Path> entry : sorted.entrySet()) {
if (this.isCanceled()) {
throw new ConnectionCanceledException();
}
final Map<Path, Path> recursive = this.compile(feature, list, entry.getKey(), entry.getValue());
if (log.isDebugEnabled()) {
log.debug(String.format("Compiled recursive list %s", recursive));
}
for (Map.Entry<Path, Path> r : recursive.entrySet()) {
if (r.getKey().isDirectory() && !feature.isRecursive(r.getKey(), r.getValue())) {
log.warn(String.format("Move operation is not recursive. Create directory %s", r.getValue()));
// Create directory unless copy implementation is recursive
result.put(r.getKey(), session.getFeature(Directory.class).mkdir(r.getValue(), new TransferStatus().withRegion(r.getKey().attributes().getRegion())));
} else {
final TransferStatus status = this.status(session, r);
result.put(r.getKey(), feature.move(r.getKey(), r.getValue(), status, new Delete.Callback() {
@Override
public void delete(final Path file) {
listener.message(MessageFormat.format(LocaleFactory.localizedString("Deleting {0}", "Status"), file.getName()));
}
}, callback));
}
}
// Find previous folders to be deleted
final List<Path> folders = recursive.entrySet().stream().filter(f -> !feature.isRecursive(f.getKey(), f.getValue())).collect(Collectors.toCollection(ArrayList::new)).stream().map(Map.Entry::getKey).filter(Path::isDirectory).collect(Collectors.toCollection(ArrayList::new));
if (!folders.isEmpty()) {
// Must delete inverse
Collections.reverse(folders);
final Delete delete = session.getFeature(Delete.class);
for (Path folder : folders) {
log.warn(String.format("Delete source directory %s", folder));
final TransferStatus status = new TransferStatus().withLockId(this.getLockId(folder));
delete.delete(Collections.singletonMap(folder, status), callback, new Delete.DisabledCallback());
}
}
}
return result;
} finally {
target.release(destination, null);
}
}
use of ch.cyberduck.core.Session in project cyberduck by iterate-ch.
the class DefaultSessionPoolTest method testCheckReconnectApplicationFailure.
@Test
public void testCheckReconnectApplicationFailure() throws Exception {
final AtomicBoolean interrupt = new AtomicBoolean();
final Host bookmark = new Host(new TestProtocol());
final TestLoginConnectionService connect = new TestLoginConnectionService() {
@Override
public boolean check(final Session<?> session, final CancelCallback callback) {
return true;
}
};
final DefaultSessionPool pool = new DefaultSessionPool(connect, new DefaultVaultRegistry(new DisabledPasswordCallback()), new DisabledTranscriptListener(), bookmark, new GenericObjectPool<Session>(new PooledSessionFactory(connect, new DisabledX509TrustManager(), new DefaultX509KeyManager(), bookmark, new DefaultVaultRegistry(new DisabledPasswordCallback())) {
@Override
public Session create() {
return new NullSession(bookmark) {
@Override
public void interrupt() throws BackgroundException {
interrupt.set(true);
super.interrupt();
}
};
}
}));
final Session<?> session = pool.borrow(BackgroundActionState.running);
pool.release(session, new BackgroundException("m", "d"));
assertFalse(interrupt.get());
}
use of ch.cyberduck.core.Session in project cyberduck by iterate-ch.
the class DefaultSessionPoolTest method testCheckReconnectSocketFailure.
@Test
public void testCheckReconnectSocketFailure() throws Exception {
final AtomicBoolean interrupt = new AtomicBoolean();
final Host bookmark = new Host(new TestProtocol());
final TestLoginConnectionService connect = new TestLoginConnectionService() {
@Override
public boolean check(final Session<?> session, final CancelCallback callback) {
return true;
}
};
final DefaultSessionPool pool = new DefaultSessionPool(connect, new DefaultVaultRegistry(new DisabledPasswordCallback()), new DisabledTranscriptListener(), bookmark, new GenericObjectPool<Session>(new PooledSessionFactory(connect, new DisabledX509TrustManager(), new DefaultX509KeyManager(), bookmark, new DefaultVaultRegistry(new DisabledPasswordCallback())) {
@Override
public Session create() {
return new NullSession(bookmark) {
@Override
public void interrupt() throws BackgroundException {
interrupt.set(true);
super.interrupt();
}
};
}
}));
final Session<?> session = pool.borrow(BackgroundActionState.running);
pool.release(session, new BackgroundException("m", new SocketException("m")));
assertTrue(interrupt.get());
}
use of ch.cyberduck.core.Session 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.Session 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());
}
Aggregations