use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.
the class RemoteProfilesFinder method find.
@Override
public Set<ProfileDescription> find(final Visitor visitor) throws BackgroundException {
if (log.isInfoEnabled()) {
log.info(String.format("Fetch profiles from %s", session.getHost()));
}
final ProfileFilter filter = new ProfileFilter();
final AttributedList<Path> list = session.getFeature(ListService.class).list(new DelegatingHomeFeature(new DefaultPathHomeFeature(session.getHost())).find(), new DisabledListProgressListener());
return list.filter(filter).toStream().map(file -> visitor.visit(new RemoteProfileDescription(protocols, file, new LazyInitializer<Local>() {
@Override
protected Local initialize() throws ConcurrentException {
try {
final Read read = session.getFeature(Read.class);
if (log.isInfoEnabled()) {
log.info(String.format("Download profile %s", file));
}
final InputStream in = read.read(file.withAttributes(new PathAttributes(file.attributes()).withVersionId(null)), new TransferStatus().withLength(TransferStatus.UNKNOWN_LENGTH), new DisabledConnectionCallback());
final Local temp = TemporaryFileServiceFactory.get().create(file.getName());
new DefaultLocalTouchFeature().touch(temp);
final OutputStream out = temp.getOutputStream(false);
try {
IOUtils.copy(in, out);
} finally {
in.close();
out.close();
}
return temp;
} catch (BackgroundException | IOException e) {
throw new ConcurrentException(e);
}
}
}))).collect(Collectors.toSet());
}
use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.
the class CryptoVaultTest method testLoadInvalidPassphrase.
@Test
public void testLoadInvalidPassphrase() throws Exception {
final NullSession session = new NullSession(new Host(new TestProtocol())) {
@Override
@SuppressWarnings("unchecked")
public <T> T _getFeature(final Class<T> type) {
if (type == Read.class) {
return (T) new Read() {
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) {
final String masterKey = "{\n" + " \"version\": 999,\n" + " \"scryptSalt\": \"RVAAirkArDU=\",\n" + " \"scryptCostParam\": 32768,\n" + " \"scryptBlockSize\": 8,\n" + " \"primaryMasterKey\": \"+03NkJNWVsJ9Tb1CTpKhXyfINzjDirFFI+iJLOWIOySyxB+abpx34Q==\",\n" + " \"hmacMasterKey\": \"aMoDtn7Y6kIXxyHo2zl47p5jCYTlRnfx3l3AMgULmIDSYAxVAraSgg==\",\n" + " \"versionMac\": \"FzirA8UhwCmS5RsC4JvxbO+ZBxaCbIkzqD2Ocagd+A8=\"\n" + "}";
return IOUtils.toInputStream(masterKey, Charset.defaultCharset());
}
@Override
public boolean offset(final Path file) {
return false;
}
};
}
return super._getFeature(type);
}
};
final AtomicBoolean prompt = new AtomicBoolean();
final CryptoVault vault = new CryptoVault(new Path("/", EnumSet.of(Path.Type.directory)));
try {
vault.load(session, new DisabledPasswordCallback() {
@Override
public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) throws LoginCanceledException {
if (!prompt.get()) {
assertEquals("Provide your passphrase to unlock the Cryptomator Vault /", reason);
prompt.set(true);
return new VaultCredentials("null");
} else {
assertEquals("Failure to decrypt master key file. Provide your passphrase to unlock the Cryptomator Vault /.", reason);
throw new LoginCanceledException();
}
}
}, new DisabledPasswordStore());
fail();
} catch (LoginCanceledException e) {
//
}
assertTrue(prompt.get());
}
use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.
the class CryptoVaultTest method testLoadEmptyPassword.
@Test
public void testLoadEmptyPassword() throws Exception {
final NullSession session = new NullSession(new Host(new TestProtocol())) {
@Override
@SuppressWarnings("unchecked")
public <T> T _getFeature(final Class<T> type) {
if (type == Read.class) {
return (T) new Read() {
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) {
final String masterKey = "{\n" + " \"scryptSalt\": \"NrC7QGG/ouc=\",\n" + " \"scryptCostParam\": 16384,\n" + " \"scryptBlockSize\": 8,\n" + " \"primaryMasterKey\": \"Q7pGo1l0jmZssoQh9rXFPKJE9NIXvPbL+HcnVSR9CHdkeR8AwgFtcw==\",\n" + " \"hmacMasterKey\": \"xzBqT4/7uEcQbhHFLC0YmMy4ykVKbuvJEA46p1Xm25mJNuTc20nCbw==\",\n" + " \"versionMac\": \"hlNr3dz/CmuVajhaiGyCem9lcVIUjDfSMLhjppcXOrM=\",\n" + " \"version\": 5\n" + "}";
return IOUtils.toInputStream(masterKey, Charset.defaultCharset());
}
@Override
public boolean offset(final Path file) {
return false;
}
};
}
return super._getFeature(type);
}
};
final CryptoVault vault = new CryptoVault(new Path("/", EnumSet.of(Path.Type.directory)));
try {
vault.load(session, new DisabledPasswordCallback() {
@Override
public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) {
return new VaultCredentials(null);
}
}, new DisabledPasswordStore());
fail();
} catch (LoginCanceledException e) {
//
}
}
use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.
the class IRODSWriteFeatureTest method testWriteThreaded.
@Test
public void testWriteThreaded() throws Exception {
final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
final Profile profile = new ProfilePlistReader(factory).read(new Local("../profiles/iRODS (iPlant Collaborative).cyberduckprofile"));
final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(System.getProperties().getProperty("irods.key"), System.getProperties().getProperty("irods.secret")));
final IRODSSession session1 = new IRODSSession(host);
session1.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
session1.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
final IRODSSession session2 = new IRODSSession(host);
session2.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
session2.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
final CountDownLatch cw1 = new CountDownLatch(1);
final CountDownLatch cw2 = new CountDownLatch(1);
final Path test1 = new Path(new IRODSHomeFinderService(session1).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final Path test2 = new Path(new IRODSHomeFinderService(session2).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final byte[] content = RandomUtils.nextBytes(68400);
final OutputStream out1 = new IRODSWriteFeature(session1).write(test1, new TransferStatus().append(false).withLength(content.length), new DisabledConnectionCallback());
final OutputStream out2 = new IRODSWriteFeature(session2).write(test2, new TransferStatus().append(false).withLength(content.length), new DisabledConnectionCallback());
new Thread(new Runnable() {
@Override
public void run() {
try {
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out2);
} catch (BackgroundException e) {
fail();
} finally {
cw1.countDown();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out1);
} catch (BackgroundException e) {
fail();
} finally {
cw2.countDown();
}
}
}).start();
cw1.await();
cw2.await();
final CountDownLatch cr1 = new CountDownLatch(1);
final CountDownLatch cr2 = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
try {
final InputStream in1 = session1.getFeature(Read.class).read(test1, new TransferStatus(), new DisabledConnectionCallback());
final byte[] buffer1 = new byte[content.length];
IOUtils.readFully(in1, buffer1);
in1.close();
assertArrayEquals(content, buffer1);
} catch (Exception e) {
fail();
} finally {
cr1.countDown();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
final InputStream in2 = session2.getFeature(Read.class).read(test2, new TransferStatus(), new DisabledConnectionCallback());
final byte[] buffer2 = new byte[content.length];
IOUtils.readFully(in2, buffer2);
in2.close();
assertArrayEquals(content, buffer2);
} catch (Exception e) {
fail();
} finally {
cr2.countDown();
}
}
}).start();
cr1.await();
cr2.await();
session1.close();
session2.close();
}
use of ch.cyberduck.core.features.Read in project cyberduck by iterate-ch.
the class IRODSWriteFeatureTest method testWriteAppend.
@Test
public void testWriteAppend() throws Exception {
final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
final Profile profile = new ProfilePlistReader(factory).read(new Local("../profiles/iRODS (iPlant Collaborative).cyberduckprofile"));
final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(System.getProperties().getProperty("irods.key"), System.getProperties().getProperty("irods.secret")));
final IRODSSession session = new IRODSSession(host);
session.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
session.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback());
final Path test = new Path(new IRODSHomeFinderService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
assertFalse(session.getFeature(Find.class).find(test));
final byte[] content = RandomUtils.nextBytes(100);
final TransferStatus status = new TransferStatus();
status.setAppend(true);
status.setLength(content.length);
assertEquals(0L, new IRODSWriteFeature(session).append(test, status).size, 0L);
final OutputStream out = new IRODSWriteFeature(session).write(test, status, new DisabledConnectionCallback());
assertNotNull(out);
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
assertTrue(session.getFeature(Find.class).find(test));
final PathAttributes attributes = new IRODSAttributesFinderFeature(session).find(test);
assertEquals(content.length, attributes.getSize());
final InputStream in = session.getFeature(Read.class).read(test, new TransferStatus(), new DisabledConnectionCallback());
final byte[] buffer = new byte[content.length];
IOUtils.readFully(in, buffer);
in.close();
assertArrayEquals(content, buffer);
// Append
final byte[] content_append = RandomUtils.nextBytes(100);
final TransferStatus status_append = new TransferStatus();
status_append.setAppend(true);
status_append.setLength(content_append.length);
assertTrue(new IRODSWriteFeature(session).append(test, status_append).append);
assertEquals(status.getLength(), new IRODSWriteFeature(session).append(test, status_append).size, 0L);
final OutputStream out_append = new IRODSWriteFeature(session).write(test, status_append, new DisabledConnectionCallback());
assertNotNull(out_append);
new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content_append), out_append);
assertTrue(session.getFeature(Find.class).find(test));
final PathAttributes attributes_complete = new IRODSAttributesFinderFeature(session).find(test);
assertEquals(content.length + content_append.length, attributes_complete.getSize());
final InputStream in_append = session.getFeature(Read.class).read(test, new TransferStatus(), new DisabledConnectionCallback());
final byte[] buffer_complete = new byte[content.length + content_append.length];
IOUtils.readFully(in_append, buffer_complete);
in_append.close();
byte[] complete = new byte[content.length + content_append.length];
System.arraycopy(content, 0, complete, 0, content.length);
System.arraycopy(content_append, 0, complete, content.length, content_append.length);
assertArrayEquals(complete, buffer_complete);
session.getFeature(Delete.class).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
assertFalse(session.getFeature(Find.class).find(test));
session.close();
}
Aggregations